Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop in XPath?

For the sample XML below, I need to check if all the /bookstore/book/price ends with "USD".

<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00USD</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99USD</price>
</book>
</bookstore>

The logic what I've used for this is

if(fn:substring(/bookstore/book//Salary/text(), fn:string-length(/bookstore/book//Salary/text())-2 ) = 'USD') then 'true' else 'false'

but it fails with "Caused by: net.sf.saxon.trans.XPathException: A sequence of more than one item is not allowed as the first argument of fn:substring()". Yes, the parser is right because fn:substring() accepts only a atomic value but I'm stuck at how to implement this scenario.

Please can anyone shed some light on the logic to implement this Or any function/Operator in XPath?

Just to let you know, My java code running on Linux supports XPath 2.0.

Thanks

like image 851
RamGopal Avatar asked Dec 09 '25 00:12

RamGopal


1 Answers

I've tested this with an XSL program, using a select in the template matching '/':

count(//price[not(ends-with(.,'USD'))])

Returns 0 on your XML.

like image 78
laune Avatar answered Dec 10 '25 15:12

laune



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!