topical media & game development

talk show tell print

professional-javascript-15-MozillaXPathExample.htm / htm



  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  
  <html>
      <head>
          <title>Mozilla XPath Example</title>   
          <script type="text/javascript" src="detect.js"></script>
          <script type="text/javascript" src="xmldom.js"></script>           
      </head>
      <body>
          <p>This example gets all nodes matching the expression <code>employee/name</code>.</p>
          <script type="text/javascript">
              var oXmlDom = document.implementation.createDocument("","",null);
              
              oXmlDom.async = false;
              
              oXmlDom.load("employees.xml");
              
              var oEvaluator = new XPathEvaluator();
              var oResult = oEvaluator.evaluate("employee/name", oXmlDom.documentElement, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  
                         
              var sMessage = "";
              var iCount = 0;
              
              var oElement = oResult.iterateNext();
              while (oElement) {
                  sMessage += oElement.xml + "\n";
                  iCount++;
                  oElement = oResult.iterateNext()
              }
              
              sMessage = "There are " + iCount + " matching nodes.\n" + sMessage;
              
              alert(sMessage);
              
          </script>
   
   
      </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.