如何获取布尔XPath表达式的结果(例如'false')?

时间:2011-08-17 19:02:19

标签: java xpath boolean-expression

如果XPath表达式的计算结果为'false',我想让Java生成'0'。

我有这个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-gram节点,其内容包含字符串'$ $ $'。但是,其余内容节点的计算结果为“true”。

如果其中一个contains()表达式的计算结果为'false',我必须在我的Xaml代码中绘制一个'0'。屏幕上必须出现“0”,例如:

'# # #' = 0.0015
'? ? ?' = 0.0047
'k i d' = 0.0012
'$ $ $' = 0

我无法获取此'0'。我不知道如何说:“如果没有包含'$ $''的节点作为内容,则返回零。

关于如何做到这一点的任何想法?

谢谢。

2 个答案:

答案 0 :(得分:0)

以下是答案:

public class XPathCharacters {
    private SAXBuilder parser = new SAXBuilder();
    private Document characters;
    private XPath pProbs, pContent1, pContent2, pContent3, pContent4, pContent5;
    private List<Attribute> probs1, probs2, probs3, probs4, probs5;
    private List<List<Attribute>> probs;
    private List<String> noNodes;
    private Double[] dprobs;
    private String pathToSourceXml;
    private String content1, content2, content3, content4, content5;
    private int noNode;

    public XPathCharacters(){

    }

    public XPathCharacters(String path, String content1,String content2,String content3,String content4,String content5){
        setPathToSourceXml(path);
        this.content1 = content1;
        this.content2 = content2;
        this.content3 = content3;
        this.content4 = content4;
        this.content5 = content5;
        noNode = 0;
        initiate();
    }

    public void initiate() {
        try {
        //Evaluate each XPath expression seperately 
            characters = parser.build(pathToSourceXml);
            pContent1 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+")]/@probability");
            pContent2 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content2+")]/@probability");
            pContent3 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content3+")]/@probability");
            pContent4 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content4+")]/@probability");
            pContent5 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content5+")]/@probability");

            //Convert the result of the above XPath expressions to nodes
            probs1 = (List<Attribute>) pContent1.selectNodes(characters);
            probs2 = (List<Attribute>) pContent2.selectNodes(characters);
            probs3 = (List<Attribute>) pContent3.selectNodes(characters);
            probs4 = (List<Attribute>) pContent4.selectNodes(characters);
            probs5 = (List<Attribute>) pContent5.selectNodes(characters);

        } catch (JDOMException jdome) {
            System.out.println("Error at XPathInformationgain.initiate(): JDOMException: " + jdome.getMessage());
        } catch (IOException ioe) {
            System.out.println("Error at XPathInformationgain.initiate(): IOException: " + ioe.getMessage());
        }
    }

    private void setPathToSourceXml(String path) {
        this.pathToSourceXml = path;
    }

    public Double[] getProbs(String pathToSourceXml) {
        this.pathToSourceXml = pathToSourceXml;

        probs = new ArrayList();
        probs.add(probs1);
        probs.add(probs2);
        probs.add(probs3);
        probs.add(probs4);
        probs.add(probs5);

        List<Double> theDoubles = new ArrayList();
        dprobs = new Double[5];
        noNodes = new ArrayList();
        int j=0;
        for (int i=0; i<5; i++) {
            if (probs.get(i).size() > 0) {
                System.out.println("i: "+i);
                System.out.println("j: "+j);
               //Add the value of all these nodes to an Array so you can work with them
               theDoubles.add(Double.parseDouble(probs.get(i).get(0).getValue()));
                dprobs[j] = theDoubles.get(j);
            }
            else {
                //If one of the values happens to be a zero-length array, add 0.0 to the array of values
                theDoubles.add(0.0);
            }
        }
        return dprobs;
    }

    public List<String> getNoNodes() {
        return noNodes;
    }
}

(这个代码不适用于我想在这个问题之外完成的任务,但它适用于这个问题,因为我有一种方法可以在java中返回'0',当XPath表达式产生零长度时)。

解决方案真的就是这个部分:

        else {
            theDoubles.add(0.0);
        }

答案 1 :(得分:0)

  

我希望Java在XPath表达式的情况下生成'0'   评估为'假'。

此XPath表达式

number(boolean(yourExpression))

0boolean(yourExpression)时确切评估为false()

您只需要将上面的yourExpression替换为您的表达式(在问题中一点都不清楚),并使用可用的XPath相关类/方法来评估此XPath表达式。