I have the following XML:
<root>
<foo>
<bar type="a whole bunch of stuff, then a magic string: MUPPET" />
<value>my Muppet value</value>
</foo>
<foo>
<bar type="some other stuff, then a different magic string: GREMLIN" />
<value>my Gremlin value</value>
</foo>
</root>
I'd like to build an XPath query that returns "my Muppet value" (the string) given the magic string "MUPPET". My guess was:
/root/foo[contains(bar/@type,'MUPPET')]/value/text()
but that doesn't seem to work. I'm really not sure whether that contains(x,y)
operator allows a query as the first parameter. As a side issue, I'm not sure whether I need the text()
on the end.
Any help?
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.
We generally use an asterisk (*) while writing XPaths. This is called a Wildcard character. //input[@*='demo'] -> Any “input” node who contains an attribute value as “demo” for any attribute.
XPath wildcard is defined as a special character used in XML language to do access in the selection process of Xpath Expressions to save time. So far, we specified elements by their names but with this wildcard, we can do the selection process for more than one element at a time.
Use wildcards in queries when you need to replace certain characters or strings in a search value. For example, if you do not know the exact spelling of a word that you want to search for, you can replace the characters that you are unsure of by a wildcard.
I just checked with this Online XPATH Evaluators and it is working fine:
http://www.mizar.dk/XPath/Default.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With