Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XPath: Node value exists

 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given an XML such as the following

<data>
<widget>123</widget>
</data>

How would I write an XPath query to determine whether there was a widget with value 123? I know how to write it to find instances of "<widget>" and parse the results in Java, but I was hoping to do it in a single XPath line.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I was trying "/data/widget[123]", thanks!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or /data/[widget='123'] should work, IIRC.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if we want to say "widget=123 or 234" ?

should it be /data/[widget='123' || widget='234'] ?


 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The way to say "or" in XPath is "or".
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No. The way to say "or" in XPath is "or".


But I tested



it gave me exception as

javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.

where "doc" is a Document. There is no problem with this because I tried other "eval" strings and they worked fine.



 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. But that's nothing to do with "or", which doesn't even feature in the problem. It's all to do with the fact that all of the XPath expressions posted so far have had the same problem, namely a predicate which doesn't modify anything. Try this instead:
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Ah, I see. But that's nothing to do with "or", which doesn't even feature in the problem. It's all to do with the fact that all of the XPath expressions posted so far have had the same problem, namely a predicate which doesn't modify anything. Try this instead:


it gives very strange results.. no exception though.

Basically I want to get some boolean type result showing "is it true that there is a data element whose child element widget (assuming only one child element called widget) has value of "123" ? I don't like the "eval" method which always returns an object. How do you come up with a Xpath and returns a true/false kind result to me ?

 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do it is to make good use of the javax.xml.xpath.XPathConstants to specify the desired return type. Like this (with missing parts and variables following the documentation)

However, there is too boolean() function, a supported xpath function. Hence, this expression is still a good xpath and it makes the desired result to capture clearer (rather than hidden in the option XPathConstants.BOOLEAN argument).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic