spacer

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

home / experts / javascript / column104


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

Vice President of Risk Technology - READY TO HIRE! (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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