Xpath In Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #16
    Maybe you have seen some syntax errors in the previous code.
    I corrected them there.

    Thanks,
    Dorin.

    Comment

    • chandrashekhar maral
      New Member
      • Apr 2007
      • 19

      #17
      excellent....
      thanx dorinbogdan's
      its working now in my code i think i had mistake with declration gud no...
      thnx again & sorry for disturbance & als for so late reply...
      can u tell me other functions/xpath used with this one...
      or from whr did i get all the xquery or xpath functions......
      pls tell me....
      thnx buddy

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #18
        I'm regularly using W3schools tutorials.
        You find almost all you need there.

        The XPath functions are from there too, and other related topics.

        God bless you,
        Dorin.

        Comment

        • chandrashekhar maral
          New Member
          • Apr 2007
          • 19

          #19
          hi and very much thanku for the all answers.....
          here again now...
          im using the xpath,where i want to selectthe book whose title is "indian".
          so i tried this one...

          var xmlNodeList = xmlDoc.selectNo des("//Book[starts-with(Title,'Ind ian')]");
          alert(xmlNodeLi st.item(idx).se lectSingleNode( "Pages").te xt);

          but its not working why???
          thnx again for reply...
          Take Care

          Comment

          • dorinbogdan
            Recognized Expert Contributor
            • Feb 2007
            • 839

            #20
            Try:
            Code:
            xmlDoc.setProperty("SelectionLanguage", "XPath");
            var xmlNodeList = xmlDoc.selectNodes("//Book[starts-with(Title,'Indian')]");

            Comment

            • newToAjax
              New Member
              • Jun 2007
              • 3

              #21
              I have created an ajax application which retrievs an xml file and fills in the tab fields on the form.The code works fine in IE while its does not in Mozilla. Can you please let me know if i have to install some plugins to use XPATH?

              Code:
              <html>
              <head>
              <title>SilverLine </title>
              <link rel="stylesheet" href="example.css" TYPE="text/css" MEDIA="screen">
              <script type="text/javascript">
              
              /* Optional: Temporarily hide the "tabber" class so it does not "flash"
                 on the page as plain HTML. After tabber runs, the class is changed
                 to "tabberlive" and it will appear.
              */
              document.write('<style type="text/css">.tabber{display:none;}<\/style>');
              
              var tabberOptions = {
              
                /* Optional: instead of letting tabber run during the onload event,
                   we'll start it up manually. This can be useful because the onload
                   even runs after all the images have finished loading, and we can
                   run tabber at the bottom of our page to start it up faster. See the
                   bottom of this page for more info. Note: this variable must be set
                   BEFORE you include tabber.js.
                */
                'manualStartup':true
              
               };
              
              </script>
              
              <!-- Load the tabber code -->
              <script type="text/javascript" src="tabber.js"></script>
              
              <script type="text/javascript">
              var request = false;
              function getRequestObject() { 
                
                 if (navigator.userAgent.indexOf("MSIE")>=0) { 
                 var strName="Msxml2.XMLHTTP"; 
                    if (navigator.appVersion.indexOf("MSIE 5.5")>=0) { 
                       strName="Microsoft.XMLHTTP"; 
                 } 
                     try { 
                        objXmlHttp=new ActiveXObject(strName); 
                        alert ("strName"+strName);
                        return objXmlHttp;
                 } catch(e) { 
                       alert("Error. Scripting for ActiveX might be disabled") ;
              
                         return; 
                   }  
                 } 
                 if (navigator.userAgent.indexOf("Mozilla")>=0) {
                    objXmlHttp=new XMLHttpRequest(); 
              	  alert("mozilla");
                     return objXmlHttp; 
                  } 
              } 
              
              function createDocument(){
                 
              	var aVersions = ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0",
              	 "MSXML2.DOMDocument","Microsoft.XmlDom"];
              	if (window.ActiveXObject)
                  {
              
              	for(var i = 0;i<aVersions.length ; i++){
              
              		try{
              
              			var oXmlDom = new ActiveXObject(aVersions[i]);
              			//alert("returning .."+i+"  "+oXmlDom);
              			return oXmlDom;
              		}catch(oError){
              
              		}
              	}
              	throw new Error ("MSXML  is not installed ");
              
              	}
              
              	else if (document.implementation && document.implementation.createDocument)
              	{
              		var xmlDoc=document.implementation.createDocument("","",null);
              		return xmlDoc;
              	}
              	else
              	{
              		alert ("no");
              		alert('Your browser cannot handle this script');
              	}
              
              }
              
              function updateTabInfo() {
              
              if ((request.readyState == 4) && (request.status == 200)) {
                 
              	var xmlDoc = request.responseXML;
                  var oXmlDom = createDocument();
              	alert ("oxml"+oXmlDom);
                  oXmlDom.load(xmlDoc);
              	oXmlDom.setProperty("SelectionLanguage", "XPath");
              
              	var noOfgroups  = oXmlDom.selectNodes("//user/groups/group").length;
              	var noOfChannels = oXmlDom.selectNodes("//user/channels/channel").length;
              	var noOfClouds = oXmlDom.selectNodes("//user/clouds/cloud").length;
                	
              
              
              	var oRoot = oXmlDom.documentElement;
              	var noOfChildren = oRoot.childNodes.length; //groups, channels, clouds
              
              	var groupRoot = oRoot.childNodes[0];
              		
              	alert ("noOfgroups"+noOfgroups);
              	document.getElementById("group").innerHTML = noOfgroups;
                }
                  
              } 
              function callServer(){
              
              	 // Build the URL to connect to
              	
              	 var url = "http://localhost/user/ABC";
              	
              	 request = getRequestObject();
              		
              	 alert ("req"+request);
              	 // Open a connection to the server
              	 request.open("GET", url, true);
              	
              	 // Set up a function for the server to run when it's done
              	 request.onreadystatechange = updateTabInfo;
              	 
              	  // Send the request
              	 request.send(null);
              	 
              
              
              }
              </script>
              
              </head>
              <body>
              <form name="Login" method="post" >
              
              <div class="tabber">
              
                   
                   <div class="tabbertab" id = "group">
              	  <h2>Groups</h2>
              	  <p>Group details.</p>
                   </div>
              
              
                   <div class="tabbertab" id = "channel">
              
              	  <h2>Channels</h2>
              	  <p>Channel details.</p>
                   </div>
              
              	 <div class="tabbertab" id = "cloud">
              
              	  <h2>Clouds</h2>
              	  <p>Cloud details.</p>
                   </div>
              
              	<div class="tabbertab" id="trend">
              	  <h2>Trends</h2>
              	  <p>User details.</p>
              
                   </div>
              
              
              </div>
              <script type="text/javascript">
              
              /* Since we specified manualStartup=true, tabber will not run after
                 the onload event. Instead let's run it now, to prevent any delay
                 while images load.
              */
              
              tabberAutomatic(tabberOptions);
              
              </script>
              <input type="submit" name="submit" value="submit"> <br>
              UserName<input type="text" name="UserName" onChange="callServer();" /><br>
              <span id="ReturnedData"></span>
              </body>
              </html>

              The XML file is
              Code:
              
              <?xml version = "1.0" encoding = "UTF-8"?>
              <user id ='ABC' name = 'BBBBB BBB'> 
              <groups>
              <group id = 'SL' name = 'SL group'>
              	<userlist>
              		<userid id ='RR' name = 'RR'/> 
              		<userid id ='SS' name = 'SS'/> 
              	</userlist>
              </group>
              <group id = 'NN' name = 'NN group'>
              	<userlist>
              	</userlist>
              </group>
              <group id = 'Web2.0' name = 'Web 2.0 Interest Group'>
              	<userlist>
              		<userid id ='SS' name = 'SSSSSS''/> 
              		<userid id ='NN' name = 'NNN'/> 
              	</userlist>
              </group>
              </groups>
              <channels>
              	<channel id ='CNN' name = 'CNN in June' url ='http://www.ibm.com/developerworks/news/dw_dwtp.rss' start='2007-06-01'/>
              </channels>
              <clouds>
              		<cloudid id ='cnn-in-oct' name = 'CNN in October'/>
              		<cloudid id ='Google-Jan' name = 'Google tags'/>
              		<cloudid id ='HBO-movies' name = 'HBO movie schedule'/>
              </clouds>
              </user>

              Comment

              • dorinbogdan
                Recognized Expert Contributor
                • Feb 2007
                • 839

                #22
                For Firefox, you must implement the selectNodes / selectSingleNod e methods manually, see this link.

                Comment

                • Scion
                  New Member
                  • Sep 2007
                  • 2

                  #23
                  Hi

                  I am trying to load contents (from XML file using Javascript ) which needs to be displayed on HTML page on button click & upon selecting one among many from a drop down box(both button & drop down on same page). So far I have been successfull in loading the content but the problem is that the content with respect to a particular button or selection should come but in my code the whole content is getting displayed...Can u help me..its urgent..Thanx

                  Comment

                  • Scion
                    New Member
                    • Sep 2007
                    • 2

                    #24
                    The xml is like
                    <ab>
                    <xyz>
                    <name>A</name>
                    <age>22</age>
                    </xyz>
                    <xyz>
                    <name>B</name>
                    <age>23</age>
                    </xyz>


                    What i want is when i click the button for A on HTML A's data should come as o/p but what i am getting is both A's & B's.
                    Help required immediately
                    Thnx in anticipation

                    Comment

                    • Hallow
                      New Member
                      • Dec 2009
                      • 2

                      #25
                      Issues with namespace

                      You will have issues with the namespaced XML, especially on Opera and Chrome browsers. There are also the differences from IE. In this case the you will have to make use of a library like sarisa for example. Or, could try to implement your own light xml library based on the existing implementation of the DOM on each browser. The last option it can perform faster since is based on the native browser support. I tried it my self and it behaves well until now. You can take a look here:
                      http://dotnetcaffe.blo gspot.com

                      Comment

                      Working...