Xpath In Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandrashekhar maral
    New Member
    • Apr 2007
    • 19

    Xpath In Javascript

    hi all,
    im using xml file its structure is lik>>
    <data>
    <book>
    <title> </title>
    <pages> </pages>
    <isbn> </isbn>

    </book>
    </data>
    -------------------------------------------------------------------------------------------------------------
    now i want to use xpath and xquery like
    pages>30 only thosae records to be seen
    can u tell me how shld i go furhte...
    im able to get whole recordset(no problem atall)
    bt not able to use xpath/xquery in thiscan anyone tell me

    thnxs
    byeee
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Welcome to TheScripts TSDN....

    What programming language are you using? ASP, PHP, XSL...?

    Comment

    • chandrashekhar maral
      New Member
      • Apr 2007
      • 19

      #3
      Originally posted by dorinbogdan
      Welcome to TheScripts TSDN....

      What programming language are you using? ASP, PHP, XSL...?

      m using JAVASCRIPT to parse the xml..
      pls reply me....
      thnx for reply

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #4
        You should create an XSL file an apply it on the source XML.
        See this example.
        The XSL should contain in a for-each loop (by book), something like:

        Code:
        <xsl:if test="pages[number(.) &gt; 30]">
        	<xsl:value-of select="pages" />
        </xsl:if>

        Comment

        • dorinbogdan
          Recognized Expert Contributor
          • Feb 2007
          • 839

          #5
          See also these useful examples, that don't require xsl.

          Comment

          • dorinbogdan
            Recognized Expert Contributor
            • Feb 2007
            • 839

            #6
            Example:
            Select all books with pages>30:
            Code:
            oXMLNodeList = oXMLDoc.selectNodes("//book/pages[number(.)  &gt; 30]")

            Comment

            • chandrashekhar maral
              New Member
              • Apr 2007
              • 19

              #7
              var xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
              xmlDoc.async="f alse";
              xmlDoc.load("na meoffilel");

              var oXMLNodeList = xmlDoc.selectNo des("//Book/Pages[<30]");
              //alert(oXMLNodeL ist);
              document.write( "hello"+oXMLNod eList);


              this giving nothing.....bla nk even not a hello...
              if i remove last 3 lines then it work fine even by xmlDoc.document Element.childNo des; im able to get all the record list
              can u tell me wht is the proble

              Comment

              • dorinbogdan
                Recognized Expert Contributor
                • Feb 2007
                • 839

                #8
                Try this example (I could not get time to test it):
                Code:
                var oXMLNodeList = xmlDoc.selectNodes("//Book/Pages[number(.)<30]");  
                for( var idx = 0; idx < oXMLNodeList.length; idx++ ){
                	document.write("title: " + oXMLNodeList.item(idx).selectSingleNode("title").text);
                	document.write("   pages: " +oXMLNodeList.item(idx).selectSingleNode("pages").text);
                	document.write("   isbn: " +oXMLNodeList.item(idx).selectSingleNode("isbn").text);
                }

                Comment

                • doss
                  New Member
                  • Mar 2007
                  • 2

                  #9
                  Originally posted by chandrashekhar maral
                  hi all,
                  im using xml file its structure is lik>>
                  <data>
                  <book>
                  <title> </title>
                  <pages> </pages>
                  <isbn> </isbn>

                  </book>
                  </data>
                  -------------------------------------------------------------------------------------------------------------
                  now i want to use xpath and xquery like
                  pages>30 only thosae records to be seen
                  can u tell me how shld i go furhte...
                  im able to get whole recordset(no problem atall)
                  bt not able to use xpath/xquery in thiscan anyone tell me

                  thnxs
                  byeee

                  May I konw what scripting language you use and what db u use ?
                  u can explain the problem with more understanding of the others
                  Thank you

                  Comment

                  • dorinbogdan
                    Recognized Expert Contributor
                    • Feb 2007
                    • 839

                    #10
                    Originally posted by doss
                    May I konw what scripting language you use and what db u use ?
                    u can explain the problem with more understanding of the others
                    Thank you
                    Welcome to TheScripts TSDN....

                    It is Javascript, like the thread title says.
                    Please read all posts, they should answer your questions.

                    Thanks,
                    Dorin.

                    Comment

                    • chandrashekhar maral
                      New Member
                      • Apr 2007
                      • 19

                      #11
                      thnxs for the response.....
                      now
                      1>im using Javascript..... .
                      2.>no DB as such i only want to use records which r in the xml.(for structure of xml pls see above)..
                      3.>now if i want to read the records from xml w/o XSLT with pages of books >30 then wht to do knw...
                      i used above code bt it soesnt seem to work nicely.....
                      can u get ....
                      pls tell me im just stuck ,due to which my headache started ....
                      so pls tell me..........
                      thnxs for reply

                      Comment

                      • dorinbogdan
                        Recognized Expert Contributor
                        • Feb 2007
                        • 839

                        #12
                        What is the expected output result?
                        What problem appears now?

                        Thanks,
                        Dorin.

                        Comment

                        • chandrashekhar maral
                          New Member
                          • Apr 2007
                          • 19

                          #13
                          Hi dorinbogdan,
                          Firstly thank you for all your help and being so patient with me.
                          Unfortunately both the code fragments did not yield the desired result... so i am taking the liberty of troubling you once again.
                          Lets look at it from start..
                          I have a XML file... its goes like this: (10-11 records here.)
                          [html]<?xml version="1.0" ?>
                          <data>
                          <Book>
                          <Title>Harry potter-1</Title>
                          <Pages>0</Pages>
                          <ISBN>158</ISBN>
                          </Book>
                          <Book>
                          <Title>Harry potter-2</Title>
                          <Pages>0</Pages>
                          <ISBN>158</ISBN>
                          </Book>
                          <Book>
                          <Title>Harry potter-3</Title>
                          <Pages>0</Pages>
                          <ISBN>158X</ISBN>
                          </Book>
                          <Book>
                          <Title>Harry potter-4</Title>
                          <Pages>0</Pages>
                          <ISBN>1586</ISBN>
                          </Book>
                          <Book>
                          <Title>Harry potter-5</Title>
                          <Pages>0</Pages>
                          <ISBN>159</ISBN>
                          </Book>
                          <Book>
                          <Title>Harry potter-6</Title>
                          <Pages>0</Pages>
                          <ISBN>1588</ISBN>
                          </Book>
                          <Book>
                          <Title>operatin g systems-Gelvin</Title>
                          <Pages>0</Pages>
                          <ISBN>15890</ISBN>
                          </Book>
                          <Book>
                          <Title>Indian culture-1</Title>
                          <Pages>0</Pages>
                          <ISBN>1586</ISBN>
                          </Book>
                          <Book>
                          <Title>Gandhi </Title>
                          <Pages>0</Pages>
                          <ISBN>15812</ISBN>
                          </Book>
                          <Book>
                          <Title>Pakist an and terrorism</Title>
                          <Pages>0</Pages>
                          <ISBN>1587</ISBN>
                          </Book>
                          <Book>
                          <Title>C-dac and cray computing in India</Title>
                          <Pages>0</Pages>
                          <ISBN>759</ISBN>
                          </Book>
                          </data>[/html]
                          When i do not use x-path (using general x-query and javascript) i get proper result... see the attached image : http://img74.imageshack.us/my.php?image=opph7.jpg

                          Now how do i derive the same result by using X-path in Javascript. ???

                          Comment

                          • dorinbogdan
                            Recognized Expert Contributor
                            • Feb 2007
                            • 839

                            #14
                            I updated the code, however it works on IE only.
                            Currently, selectNodes and selectSingleNod es methods are not supported in Firefox. It is required to implement these methods manually (see this link)
                            Code:
                            		function loadXML()
                            		{
                            			var xmlDoc = null;
                            			// code for IE
                            			if (window.ActiveXObject)
                            			  {
                            			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                            			  xmlDoc.async=false;
                            			  xmlDoc.load("books.xml");
                            			  return xmlDoc;
                            			  }
                            			// code for Mozilla, Firefox, Opera, etc.
                            			else if (document.implementation && document.implementation.createDocument)
                            			  {
                            			  xmlDoc=document.implementation.createDocument("","",null);
                            			  xmlDoc.load("books.xml");
                            			  return xmlDoc;
                            			  }
                            			else
                            			  {
                            			  	alert('Your browser cannot handle this script');
                            			  }
                            		}
                            
                            		function readXmlUsingXPath(){
                            			var xmlDoc = loadXML();    
                            			if (xmlDoc == null) {alert("XML DOM object is null");return};
                            			var xmlNodeList = xmlDoc.selectNodes("//Book[number(Pages)<30]");
                            			for(var idx = 0; idx < xmlNodeList.length; idx++ ){
                            				document.write("Title: " + xmlNodeList.item(idx).selectSingleNode("Title").text );
                            				document.write("   Pages: " + xmlNodeList.item(idx).selectSingleNode("Pages").text);
                            				document.write("   ISBN: " + xmlNodeList.item(idx).selectSingleNode("ISBN").text );
                            				document.write("<br />")
                            			}
                            		}

                            Comment

                            • chandrashekhar maral
                              New Member
                              • Apr 2007
                              • 19

                              #15
                              WOW! you are fast. okay - i will test and revert

                              Comment

                              Working...