Using Wildcards

XPath features three wildcards:

The asterisk (*)

Selects all element nodes in the current context. Be aware that the asterisk wildcard selects element nodes only; attributes, text nodes, comments, or processing instructions aren’t included. You can also use a namespace prefix with an asterisk. In our sample sonnet, the XPath expression auth:* returns all element nodes in the current context that are associated with the namespace URL http://www.authors.com/.

[2.0] XPath 2.0 lets us use a wildcard as a namespace prefix. The XPath expression *:author returns all element nodes in the current context that have a local name of author, regardless of their namespace. In XPath 2.0, both auth:* and *:author are legal; in XPath 1.0, looking for *:author causes a fatal error.

Note

As always, searching for a matching namespace is based on the namespace URL, not the prefix. For example, assume the namespace URL http://www.authors.com/ is associated with the prefix auth in the XML document and the prefix something_else in our stylesheet. Looking for something_else:* in the stylesheet returns all of the elements with the auth prefix in the XML document.

The at-sign and asterisk (@*)

Selects all attribute nodes in the current context. You can use a namespace prefix with the attribute wildcard. In our sample sonnet, @auth:* returns all attribute nodes in the current context that are associated with the namespace URL http://www.authors.com. (There aren’t any attributes associated with the ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.