topical media & game development

talk show tell print

professional-javascript-15-MozillaXPathExample3.htm / htm

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <title>Mozilla XPath Example</title>   
          <script type="text/javascript" src="detect.js"></script>
          <script type="text/javascript" src="xmldom.js"></script>
          <script type="text/javascript">
              if (isMoz) {
                  Element.prototype.selectNodes = function (sXPath) {
                      var oEvaluator = new XPathEvaluator();
                      var oResult = oEvaluator.evaluate(sXPath, oXmlDom.documentElement, null, 
                                                        XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
                      var aNodes = new Array;
                      if (oResult != null) {
                          var oElement = oResult.iterateNext();
                          while(oElement) {
                              oElement = oResult.iterateNext();
                      return aNodes;
          <p>This example gets all nodes matching the expression <code>employee/name</code> using the <code>selectNodes()</code> method.</p>
          <script type="text/javascript">
              var oXmlDom = document.implementation.createDocument("","",null);
              oXmlDom.async = false;
              var aNodes = oXmlDom.documentElement.selectNodes("employee/name");
              var sMessage = "There are " + aNodes.length + " matching nodes.\n";
              var iCount = 0;
              for (var i=0; i < aNodes.length; i++) {                       
                  sMessage += aNodes[i].xml + "\n";

(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.