aspose file tools*
The moose likes XML and Related Technologies and the fly likes XPath: Node value exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath: Node value exists" Watch "XPath: Node value exists" New topic
Author

XPath: Node value exists

Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Something like
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Ah, I was trying "/data/widget[123]", thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or /data/[widget='123'] should work, IIRC.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
what if we want to say "widget=123 or 234" ?

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


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

No. The way to say "or" in XPath is "or".
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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

Joined: Mar 28, 2006
Posts: 375
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

Joined: Jan 18, 2011
Posts: 544
    
    3
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath: Node value exists