spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column104


Web Services, Part IX: Pattern-Based XML Node Selection

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Pattern Specification - Part A

We start off with the ./week pattern. It finds all week elements within the current context (try it). Same as week. We can test this pattern with January's context:


var matchedNodes = xmlDoc.childNodes[3].childNodes[1].
  childNodes[0].selectNodes('./week');
alert("Number of matched nodes: " + matchedNodes.length);

You can see that there are 4 nodes returned. These are January's four weeks of proceeds.

The /sales pattern matches the top level sales node (try it):

var matchedNodes = xmlDoc.selectNodes('/sales');
alert("Number of matched nodes: " + matchedNodes.length);

Notice that the forward slash denotes the root of the DOMDocument tree, as in Unix. Trying for the pattern /week won't match any node because there are no week nodes at the root of the tree (try it):

var matchedNodes = xmlDoc.selectNodes('/week');
alert("Number of matched nodes: " + matchedNodes.length);

The //week pattern uses two forward slashes and denotes searching the tree all the way to the bottom, from the current context. When the context is the root, we should find 12 week nodes down the tree (try it):

var matchedNodes = xmlDoc.selectNodes('//week');
alert("Number of matched nodes: " + matchedNodes.length);

One of the useful criteria to search nodes by is attribute values. Attributes are denoted by the @ character. To match all week nodes where the attribute dvds_rented is 12000, you will write this (try it):


var matchedNodes = xmlDoc.selectNodes
  ('//week[@dvds_rented = "12000"]');
alert("Number of matched nodes: " + matchedNodes.length);

Notice we found one week node that answers this query, as expected. Also notice the two forward slashes. They are needed because the week nodes are not directly beneath the context node (root), but rather deep in the tree. We can change the context to January and then can start the search at the week's father (try it):

var matchedNodes = xmlDoc.childNodes[3].childNodes[1].childNodes[0].
  selectNodes('week[@dvds_rented = "12000"]');
alert("Number of matched nodes: " + matchedNodes.length);

We have already shown the forward slash delimiter. The pattern month/week matches all these father/son combinations. When the context is the data node, you should get all 12 week nodes (try it):


var matchedNodes = xmlDoc.childNodes[3].childNodes[1].
  selectNodes('month/week');
alert("Number of matched nodes: " + matchedNodes.length);

You can use the double forward slashes anywhere in the pattern. They denote that the specified child can be a grandchild of the specified father. The pattern /sales//week should yield 12 week nodes (try it):

var matchedNodes = xmlDoc.selectNodes('/sales//week');
alert("Number of matched nodes: " + matchedNodes.length);


Next: How to specify patterns - Part B

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: February 25, 2002
Revised: February 25, 2002

URL: http://www.webreference.com/js/column104/5.html