2011-08-17 67 views
0

我希望Java在XPath表達式計算爲'false'時生成'0'。如何獲得布爾XPath表達式的結果(例如'false')?

我有這樣的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個元素,因爲 '內容1' 回報 '假'。沒有n-gram節點,其內容包含字符串'$ $ $'。但是,其餘內容節點的評估結果爲「true」。

如果其中一個包含()表達式的計算結果爲'false',那麼我必須在Xaml代碼中繪製'0'。 '0'必須出現在屏幕上,例如:

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

我無法讀取此'0'。我不知道怎麼說:「萬一回零,有包含‘$ $ $’爲內容沒有節點

如何做到這一點的任何想法

謝謝

+0

爲什麼當你可以用Java創建一個時,XPath應該返回一個'0'? XPath只能選擇表單*實際存在的*。 – Tomalak

+0

無論哪種方式,我試圖得到一個0,如果其中一個元素不包含特定的字符串'x y z'。 – MrStack

+0

你不會。 XPath是一種選擇語言,不能保證您可以選擇您要求的內容。讓5個表達式在一個循環中運行並分別將結果(或0)分配給數組。 – Tomalak

回答

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; 
    } 
} 

(此代碼是不工作我要完成這個問題之外,但它適用於這個問題,因爲我已經在返回「0」的一種方式java,當XPath表達式產生零長度時)。

的解決方案是這部分真的:

 else { 
      theDoubles.add(0.0); 
     } 
0

我想有Java的產生「0」的情況下,XPath表達式 計算結果爲「假」。

這個XPath表達式

number(boolean(yourExpression)) 

評估爲0什麼時候boolean(yourExpression)false()

您只需將上面的yourExpression替換爲您的表達式(問題根本不清楚),然後使用可用的XPath相關類/方法評估此XPath表達式。