March 2, 2002 - Context-Dependent Pattern Matching | WebReference

March 2, 2002 - Context-Dependent Pattern Matching

Yehuda Shiran March 2, 2002
Context-Dependent Pattern Matching
Tips: March 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

When using patterns to find nodes on the DOMDocument tree, the concept of context plays a very important role. In fact, most of the search patterns are based on the search context. The context is the Node object whose selectNodes() method or selectSingleNode() method is called. Since methods are members of their object instances, the Node object instance that you call these methods with determines the context. Each node determines a different context, and applying identical patterns on different nodes yields different results. The context is like a file system directory. Results of system commands depend on the directory you are currently at.

Let's go over all the node contexts that mydvd7.xml's DOMDocument. The root of the DOMDocument tree is simply xmlDoc, the object created when we read the file in:

  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
  xmlDoc.async = false;
  xmlDoc.load("mydvd7.xml");
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>