Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath wildcards on node name

Tags:

xml

xslt

xpath

People also ask

Can you use wildcard in XPath?

XPATH allows the use of wildcards to write more robust path expressions where the use of specific path expressions is either impossible or undesirable. matches any element node. matches any attribute node. matches any type of node.

What does the wildcard operator do in XPath?

XPath wildcard replaces the literal name of a given node making the Xpath more robust. And these wildcard steps are preferable when the XML element names are unknown and promoted as a shorthand notation to consider a set of elements.

Can you use wildcards in XML?

There are three wildcards: * , node( ) , and @* . The * does not match attributes, text nodes, comments, or processing-instruction nodes. Thus, in the previous example, output will only come from child elements that have their own template rules that override this one.

What is Asterisk XPath?

We generally use an asterisk (*) while writing XPaths. This is called a Wildcard character. An asterisk in XPath may represent any node or attribute name of XML or DOM. A node is a tag in XML document. When we do not bother about node or attribute name we can use an asterisk.


This is the correct XPath 1.0 expression which selects an element with the last 5 character of name equal to "_cost" in any namespace.

/data/stats/*[substring(name(), string-length(name()) - 4) = '_cost']

You could also use contains

e.g

/data/stats[contains(.,'_cost')] 

With XPath 1.0 you can use /data/stats/*[substring-after(name(), '_cost') = ''] pattern. That checks if the element's name ends with the _cost suffix.

In XPath 2.0 there is fn:ends-with(str, str) and your corresponding expression will be *[ends-with(name(), '_cost')].


The above did not work for me. I had to "slightly" modify that as follows:

/data/stats/*[contains(name(),'_cost')]