spacer
Yehuda Shiran February 27, 2002
Switching between XSLT and XPath
Tips: February 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

There are two standard formats to specify patterns for searching nodes in the DOMDocument tree: XSLT Patterns and XPath. The XPath format is richer than the XSLT Patterns, and supports many XSLT Patterns. XSLT Patterns are the default. To change to XPath, you need to change the selectionLanguage. Here is a demo:

function applyPatterns() {
  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
  xmlDoc.async = false;
  xmlDoc.load("mydvd7.xml");
  xmlDoc.setProperty("SelectionLanguage", "XPath");
  var matchedNodes = xmlDoc.selectNodes('/sales//month');
  alert("Number of matched nodes: " + matchedNodes.length);
}
Try it. You should get three nodes, for the three months reported in our mydvd7.xml file:

<?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>

People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark