Useful Patterns for XPath Wildcards and Axes

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Useful Patterns for XPath Wildcards and Axes

Try to select an efficient approach. The use of axes and wildcard characters can radically simplify your code, but that come at the expense of decreased efficiency. For example, the pattern "//*" will match every node in the tree, which is useful if you're trying to search for a given element or attribute but which can be disastrous if your XML structure has 100,000 nodes.

//* Linearizes Tree

The //* pattern will put all of the nodes in a tree into a single node-set in regular traversal order.

//* Is Least Efficient

Using universal match for searches is the least efficient way of finding nodes, as it traverses every point in the tree.

.//*[@attrName] Gets Elements With a Specific Attribute

The id() function has been removed from the XSLT specification, but you can search for all elements with given attributes with the XPath expression //*[@id='myID'].

ancestor-or-self::* Gets Ancestor List

This pattern can be used to retrieve a list of all direct ancestors of a given node, useful for identifying paths.

Use Axes and Wildcards for Recursion

Both Axes and Wildcards are perfect for making recursive calls.

following-sibling::* is a node set

This pattern returns ALL sibling nodes after the current node. To get the next node, use:

following::sibling::*[1]