java 如何使用 XPath 计数表达式来评估 if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11603345/
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 can I use the XPath count expression to evaluate a if statement?
提问by djangofan
I'm trying to figure out how to use a Xpath expression to verify that there is only one matching node in an XPath expression. Here is some sample XML:
我试图弄清楚如何使用 Xpath 表达式来验证 XPath 表达式中只有一个匹配节点。这是一些示例 XML:
<a>
<b>
<c>X:1 Y:0</c>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
<b>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
</a>
So, I tried code similar to this, but it doesn't work:
所以,我尝试了类似的代码,但它不起作用:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
try {
XPathExpression expr = xpath.compile( "count(//a/b/c)" ) );
} catch ( XPathExpressionException e ) {
printErr( e );
}
if ( expr == true ) {
// edit XML node
}
There is supposedly a way for the count expression to return a Boolean = true . How do I get this?
据说有一种方法可以让 count 表达式返回 Boolean = true 。我怎么得到这个?
回答by Kennet
Maybe something like this:
也许是这样的:
XPathExpression xpr = xpath.compile("count(//a/b/c)");
System.out.println(xpr.evaluate(n, XPathConstants.NUMBER));
xpr = xpath.compile("count(//a/b/c) = 5");
System.out.println(xpr.evaluate(n, XPathConstants.BOOLEAN));
where n is a Node/Element from your document
其中 n 是您文档中的节点/元素
First prints 5.0, second prints true
第一次打印 5.0,第二次打印 true
回答by pb2q
You've only compiled an XPath expression, you haven't actually evaluated it.
您只编译了一个 XPath 表达式,实际上还没有对其进行评估。
Note that you can evaluate the expression directly using XPath.evaluate()
:
请注意,您可以使用XPath.evaluate()
以下命令直接评估表达式:
result = xpath.evaluate("count(//a/b/c)", input);
Or using your pre-compiled expr
:
或者使用您的预编译expr
:
result = expr.evaluate(input);
And that these evaluate
methods can also take as an additional argument a type, such as XPathConstants.BOOLEAN
.
并且这些evaluate
方法还可以将类型作为附加参数,例如XPathConstants.BOOLEAN
.
So this will work, with input
as an InputSource
for your XML:
所以这将起作用,input
作为InputSource
您的 XML:
XPathExpression expr = xpath.compile("count(//a/b/c) = 5");
Object retval = expr.evaluate(input, XPathConstants.BOOLEAN);
if ((Boolean) retval)
// edit XML node
回答by Dimitre Novatchev
You may use:
您可以使用:
count(//a/b/c) = 1
This evaluates to true()
if and only if count(//a/b/c)
evaluates to 1
-- false()
otherwise.
这评估为true()
当且仅当count(//a/b/c)
评估为1
--false()
否则。
however this may not be efficient (causes the complete evaluation of the XPath expression and counting all the nodes).
但是这可能效率不高(导致 XPath 表达式的完整评估和所有节点的计数)。
A more efficient XPath expression:
更高效的 XPath 表达式:
(//a/b/c)[1] and not( (//a/b/c)[2] )
回答by i.terrible
how about //a/b/c[count(*) != 0]
?
also if i remember XPathExpression returns an Object, which you'll have to convert to a primitive type i.e xpath Number to Double etc.
怎么样//a/b/c[count(*) != 0]
?此外,如果我记得 XPathExpression 返回一个对象,您必须将其转换为原始类型,即 xpath Number to Double 等。