如何获取布尔 XPath 表达式的结果(例如“false”)?
我想让 Java 生成一个“0”,以防 XPath 表达式计算结果为“假”。
我有这个Java代码:
//Read the input XML document
private SAXBuilder parser = new SAXBuilder();
private Document characters;
private XPath pProbs;
private List<Attribute> probs;
private Double[] dprobs;
private String pathToSourceXml;
private String content1, content2, content3, content4, content5;
characters = parser.build(pathToSourceXml);
pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
probs = (List<Attribute>) pProbs.selectNodes(characters);
...
//Return all the values of the @probability attibrutes
public Double[] getProbs(String pathToSourceXml) {
this.pathToSourceXml = pathToSourceXml;
List<Double> theDoubles = new ArrayList();
dprobs = new Double[5];
for (int i=0; i<probs.size(); i++) {
theDoubles.add(Double.parseDouble(probs.get(i).getValue()));
dprobs[i] = theDoubles.get(i);
}
return dprobs;
}
这里的问题是这个代码:
pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
只返回4个元素,因为'content1'返回'false'。不存在内容包含字符串“$ $ $”的 n 元语法节点。但是,其余内容节点的计算结果为“true”。
如果这些 contains() 表达式之一的计算结果为“false”,我必须在 Xaml 代码中绘制“0”。屏幕上必须出现“0”,例如:
'# # #' = 0.0015
'? ? ?' = 0.0047
'k i d' = 0.0012
'$ $ $' = 0
I can't fetch this '0'。我不知道怎么说:“如果没有包含 '$ $ $' 作为内容的节点,则返回零。
关于如何执行此操作的任何想法?
谢谢。
I would like to have Java generate a '0' in case an XPath expression evaluates to 'false'.
I have this Java code:
//Read the input XML document
private SAXBuilder parser = new SAXBuilder();
private Document characters;
private XPath pProbs;
private List<Attribute> probs;
private Double[] dprobs;
private String pathToSourceXml;
private String content1, content2, content3, content4, content5;
characters = parser.build(pathToSourceXml);
pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
probs = (List<Attribute>) pProbs.selectNodes(characters);
...
//Return all the values of the @probability attibrutes
public Double[] getProbs(String pathToSourceXml) {
this.pathToSourceXml = pathToSourceXml;
List<Double> theDoubles = new ArrayList();
dprobs = new Double[5];
for (int i=0; i<probs.size(); i++) {
theDoubles.add(Double.parseDouble(probs.get(i).getValue()));
dprobs[i] = theDoubles.get(i);
}
return dprobs;
}
The problem here is that this code:
pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
only returns 4 elements because 'content1' returns 'false'. There are no n-gram nodes whose content contains the string '$ $ $'. However, the rest of the content nodes evaluates to 'true'.
If one of these contains() expressions evaluates to 'false', I must draw a '0' in my Xaml code. A '0' must appear on the screen, such as:
'# # #' = 0.0015
'? ? ?' = 0.0047
'k i d' = 0.0012
'$ $
I can't fetch this '0'. I don't know how to say: "return a zero in case that there are no nodes that contain '$ $ $' as content.
Any ideas on how to do this?
Thank you.
= 0
I can't fetch this '0'. I don't know how to say: "return a zero in case that there are no nodes that contain '$ $ $' as content.
Any ideas on how to do this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是答案:(
此代码不适用于我想要在这个问题之外完成的任务,但它适用于这个问题,因为当 XPath 表达式产生零长度时,我有一种在 java 中返回“0”的方法)。
解决方案实际上是这部分:
Here is the answer:
(This code is not working for what I want to accomplish outside of this question, but it works for this question because I have a way of returning a '0' in java, when the XPath expression yields zero-length).
The solution is this part really:
此 XPath 表达式:
当
boolean(yourExpression)
为false()
时,计算结果恰好为0
。您只需将上面的
yourExpression
替换为您的表达式(问题中根本不清楚),并使用您可用的 XPath 相关类/方法来评估此 XPath 表达式。This XPath expression:
evaluates to
0
exactly whenboolean(yourExpression)
isfalse()
.You need simply to substitute
yourExpression
above with your expression (which isn't at all clear from the question) and to evaluate this XPath expression with the XPath related classes/methods available to you.