Skip to main content

XPath Wildcard

XPath allows the use of wildcards to write more robust path expressions where the use of specific path expressions is either impossible or undesirable.

XPath wildcards can be used to select unknown XML nodes.

XPath Wildcard List

XPath defines the following wildcards on nodes to be used with the XPath expressions.

WildcardExpression
*It is used to match any node.
.It is used to match the current node in context.
@*It is used to match any attribute.
node()It is used to match node of any type.

Example

Consider the following XML document:

<?xml version="1.0"  encoding="UTF-8"?>

<bookstore>
<book>
<title lang="en">A Great Book</title>
<author>Tom Nolan</author>
<price>19.99</price>
</book>

<book>
<title lang="en">Learning XML</title>
<author>Tutorial Reference</author>
<price>29.99</price>
</book>
</bookstore>

In the following table there are some examples of selection:

ExampleResult
//*Matches ALL nodes in the input XML document
/bookstore/*Matches all child nodes of bookstore. In this example the only child nodes are book.
/*/categoryMatches all book nodes as children of the root node (bookstore). If there are nodes other than book under the bookstore node then this expression will only return the required book nodes.
attribute::*Selects all attributes of the current node
//*[@*]Matches ANY element node which has an attribute defined. This example will only match title nodes.
//title[@*]Matches only title element nodes with an attribute defined.

Table of Contents