Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XPath: Node value exists

 
author
Posts: 4342
40
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like
 
Scott Selikoff
author
Posts: 4342
40
jQuery Eclipse IDE Java
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or /data/[widget='123'] should work, IIRC.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if we want to say "widget=123 or 234" ?

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


 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic