XPath 中的循环?
For loop in XPath?
对于下面的示例 XML,我需要检查是否所有 /bookstore/book/price 都以 "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>
我为此使用的逻辑是
if(fn:substring(/bookstore/book//Salary/text(), fn:string-length(/bookstore/book//Salary/text())-2 ) = 'USD') then 'true' else 'false'
但它因 "Caused by: net.sf.saxon.trans.XPathException: A sequence of more than one item is not allowed as the first argument of fn:substring()" 而失败。
是的,解析器是正确的,因为 fn:substring() 只接受一个原子值,但我对如何实现这种情况感到困惑。
任何人都可以阐明在 XPath 中实现这个或任何 function/Operator 的逻辑吗?
特此通知您,我在 Linux 上的 java 代码 运行 支持 XPath 2.0。
谢谢
我已经用 XSL 程序测试了这个,在模板中使用 select 匹配 '/':
count(//price[not(ends-with(.,'USD'))])
Returns 0 在你的 XML.
有两种方法:两者都可能比使用count()
更有效(如果条件在中途被违反) ] 必须遍历整个序列:
not(/bookstore/book/price[not(ends-with(., 'USD'))])
和:
every $p in /bookstore/book/price
satisfies ends-with($p, 'USD')
后者可以更简洁地写成:
every $b in bookstore/book/price/ends-with(., 'USD')
satisfies $b"/>
对于下面的示例 XML,我需要检查是否所有 /bookstore/book/price 都以 "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>
我为此使用的逻辑是
if(fn:substring(/bookstore/book//Salary/text(), fn:string-length(/bookstore/book//Salary/text())-2 ) = 'USD') then 'true' else 'false'
但它因 "Caused by: net.sf.saxon.trans.XPathException: A sequence of more than one item is not allowed as the first argument of fn:substring()" 而失败。 是的,解析器是正确的,因为 fn:substring() 只接受一个原子值,但我对如何实现这种情况感到困惑。
任何人都可以阐明在 XPath 中实现这个或任何 function/Operator 的逻辑吗?
特此通知您,我在 Linux 上的 java 代码 运行 支持 XPath 2.0。
谢谢
我已经用 XSL 程序测试了这个,在模板中使用 select 匹配 '/':
count(//price[not(ends-with(.,'USD'))])
Returns 0 在你的 XML.
有两种方法:两者都可能比使用count()
更有效(如果条件在中途被违反) ] 必须遍历整个序列:
not(/bookstore/book/price[not(ends-with(., 'USD'))])
和:
every $p in /bookstore/book/price
satisfies ends-with($p, 'USD')
后者可以更简洁地写成:
every $b in bookstore/book/price/ends-with(., 'USD')
satisfies $b"/>