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 URLhttp://www.authors.com/.[2.0] XPath 2.0 lets us use a wildcard as a namespace prefix. The XPath expression
*:authorreturns all element nodes in the current context that have a local name ofauthor, regardless of their namespace. In XPath 2.0, bothauth:*and*:authorare legal; in XPath 1.0, looking for*:authorcauses 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 prefixauthin the XML document and the prefixsomething_elsein our stylesheet. Looking forsomething_else:*in the stylesheet returns all of the elements with theauthprefix 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 URLhttp://www.authors.com. (There aren’t any attributes associated with the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access