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 Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

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 B

The XPath patterns support the wild card (*), much the same way as it is supported by the Unix operating system. The pattern /sales/*/week does not match any nodes, because the week nodes are not exactly grandchildren of sales, as this pattern suggests. The week nodes are great-grandchildren of sales. The pattern /sales/*/*/week reflects the actual tree structure. It matches all 12 week nodes that are great-grandchildren of sales (try it):

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

The pattern /sales//month/week matches all week records that are children of month records, anywhere under the sales node (try it):

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

The pattern .//week matches all week nodes that are any number of levels below the current node (try it):

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

The pattern */* matches all grandchildren of the current node. If the current context is the data node, its grandchildren will be all 12 week nodes and 3 name nodes (try it):


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

The pattern *[@dvds_rented] matches all nodes having the attribute dvds_rented. The context may change. The pattern /*[@dvds_rented] will match nodes only at the root. The pattern //*[@dvds_rented] will match nodes any level below the root (try it):

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

The pattern @dvds_rented will match nodes at the current level. If the context is the first week of January, we should get a single week node (try it):


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

Similar patterns are also possible. The pattern week/@dvds_rented will match 4 nodes when the context is the month of January (try it)


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

The pattern week[2] matches the third week node of the current context. If the context is January we will match one node (try it):


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

The pattern month[week][2] matches the third month node that has a week node (try it):


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


Next: A Final Word

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/6.html