• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

XPath: Node value exists

 
author
Posts: 4356
45
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: 4356
45
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'] ?


 
Sheriff
Posts: 28394
100
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
Sheriff
Posts: 28394
100
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic