March 21, 2002 - Using "//" to Skip Intermediate Hierarchy Levels in DOMDocument | WebReference

March 21, 2002 - Using "//" to Skip Intermediate Hierarchy Levels in DOMDocument

Yehuda Shiran March 21, 2002
Using "//" to Skip Intermediate Hierarchy Levels in DOMDocument
Tips: March 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

The pattern "//" means any number of hierarchy levels. When used within a string, hierarchy matching starts at the first character and ends just before the "//". Hierarchy matching starts again just after the "//" pattern and ends up at the last character of the string. Any number of hierarchy levels can be skipped between the pre- and post-matching. For example, the pattern /sales//month/week matches all week records that are children of month records, anywhere under the sales node in mydvd7.xml (try it--Internet Explorer 5+):

  var matchedNodes = xmlDoc.selectNodes('/sales//month/week');
  alert("Number of matched nodes: " + matchedNodes.length);
Here is mydvd7.xml for your reference:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="mydvd7.xsl"?>
<!DOCTYPE sales SYSTEM "mydvd7.dtd">
  <sales>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, 
	    and &lt;&month;&gt; of 2001</description>
      <author>author: &preparedby;</author>
      <date>Jan 30, 2002</date>
    </summary>
    <data>
      <month>
        <name>January 2001</name>
        <week number="1" dvds_rented="12000" />
        <week number="2" dvds_rented="15000" />
        <week number="3" dvds_rented="18000" />
        <week number="4" dvds_rented="11800" />		  
      </month>
      <month>
        <name>February 2001</name>
        <week number="1" dvds_rented="11500" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="19050" />
        <week number="4" dvds_rented="11200" />		  
      </month>
      <month>
        <name>March 2001</name>
        <week number="1" dvds_rented="15300" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11230" />		  
      </month>
    </data>
  </sales>