java 如何在 xpath 表达式中使用逻辑运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31473666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use logical operator in xpath expression
提问by user2121
i wrote the beloww code to retrive the value of the attribuet v
in the element tag
by specifyin the lat
and lon
values as shown below.
i think my mistake is related to the how i use the && operator in the xpath expression, because when i used
我编写了下面的代码,通过在和 中指定值来检索v
元素中属性的值,如下所示。我认为我的错误与我在 xpath 表达式中使用 && 运算符的方式有关,因为当我使用tag
lat
lon
String expr0 = "//node[@lat='53.334062']/following-sibling::tag[1]/@v";
i receive the expected value, but when i uesd the below posted expression, the system crashs
我收到了预期值,但是当我使用下面发布的表达式时,系统崩溃了
but at run time, the program crashs and shows the below error message.
但在运行时,程序崩溃并显示以下错误消息。
code:
代码:
String expr0 = "//node[@lat='53.334062' && @lon='8.841545']/following-sibling::tag[1]/@v";
xPath.compile(expr0);
String s = (String) xPath.evaluate(expr0, document, XPathConstants.STRING);
System.out.println(s);
xml:
xml:
<?xml version='1.0' encoding='utf-8' ?>
<osm>
<node id="25779111" lat="53.0334062" lon="8.8461545"/>
<node id="25779112" lat="53.0338904" lon="8.846314"/>
<node id="25779119" lat="53.0337395" lon="8.8489255"/>
<tag k="maxspeed" v="30"/>
<tag k="maxspeed:zone" v="yes"/>
<node id="25779114" lat="53.334062" lon="8.841545"/>
<node id="25779117" lat="53.038904" lon="8.84614"/>
<node id="25779110" lat="53.033795" lon="8.489255"/>
<tag k="maxspeed" v="32"/>
<tag k="maxspeed:zone" v="yes"/>
</osm>
error:
错误:
Exception in thread "main" javax.xml.transform.TransformerException: Zus?tzliche ungültige Tokens: '&&', '[', '@', 'lon', '=', ''8.841545'', ']', '/', 'following-sibling', '::', 'tag', '[', '1', ']', '/', '@', 'v'
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source)
at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source)
回答by Ian Roberts
The Boolean "and" operator in XPath is the word and
, not &&
(specification)
XPath 中的布尔“与”运算符是单词and
, not &&
(规范)
String expr0 = "//node[@lat='53.334062' and @lon='8.841545']/following-sibling::tag[1]/@v";
Alternatively you can use two predicates:
或者,您可以使用两个谓词:
String expr0 = "//node[@lat='53.334062'][@lon='8.841545']/following-sibling::tag[1]/@v";
This has the same effect - predicates apply from left to right so the first predicate filters the set of all node
elements to just those that have @lat='53.334062'
, the second filters thatset to just those that have @lon='8.841545'
.
这有相同的效果-谓词从左至右适用,所以第一个谓词筛选集合所有的node
元素,只是那些有@lat='53.334062'
,第二过滤器是集只是那些有@lon='8.841545'
。