Web Services, Part IX: Pattern-Based XML Node Selection: Pattern Specification (A) - Doc JavaScript | WebReference

Web Services, Part IX: Pattern-Based XML Node Selection: Pattern Specification (A) - Doc JavaScript


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

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

https://www.internet.com


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

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