Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get wildcards to work in XPath attribute value matching?

Tags:

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?

like image 567
James A. Rosen Avatar asked Jun 17 '09 23:06

James A. Rosen


People also ask

Can we 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.

How do I create a wildcard in XPath?

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.

What does the wildcard operator * Do in XPath?

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.

Can you use wildcards in XML?

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.


1 Answers

I just checked with this Online XPATH Evaluators and it is working fine:

http://www.mizar.dk/XPath/Default.aspx

like image 195
Baget Avatar answered Oct 11 '22 23:10

Baget