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

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


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

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

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