2017-09-01 9 views
3

本当の問題:/root/row/checks[not(taxAmount = sum(selections/tax))]/sum(selections/tax/text())XPATH:子ノードの合計全体ではなく、XPATH



私は、次のXPATHがあります:

を以下が働いていなかったので、私は2.0の構文でXPATH 1.0を使用していました
/root/row/checks[./taxAmount/text() != sum(./selections/tax/text())]/selections/tax/text() 

私は、次のような何かをしたい:

/root/row/checks[./taxAmount/text() != sum(./selections/tax/text())]/sum(selections/tax/text()) 

文全体、つまり/選択肢の下にある子ノードだけを合計したくないのですが、これまでに試した結果、構文エラーが発生します

本質的に、ヘッダーと比較するために詳細を合計したいと考えました。

例XML:ダニエルの答えに基づいて

<root> 
    <row> 
    <checks> 
     <selections>  
     <tax>1.00</tax> 
     </selections> 
     <selections>  
     <tax>2.00</tax> 
     </selections> 
     <selections>  
     <tax>1.14</tax> 
     </selections> 
     <taxAmount>4.14</taxAmount> 
    </checks> 
    <checks> 
     <selections>  
     <tax>1.00</tax> 
     </selections> 
     <selections>  
     <tax>0.50</tax> 
     </selections> 
     <selections>  
     <tax>1.00</tax> 
     </selections> 
     <taxAmount>3.00</taxAmount> 
    </checks> 
    <checks> 
     <selections>  
     <tax>1.00</tax> 
     </selections> 
     <selections>  
     <tax>0.50</tax> 
     </selections> 
     <selections>  
     <tax>1.25</tax> 
     </selections> 
     <taxAmount>5.00</taxAmount> 
    </checks> 
    </row> 
</root> 

編集:

私が探していたものはかなりありません。それは問題にチェックを選択しますが、私はこのような何か欲しかっ:

/ルート/行/チェック[ない(taxAmount = SUM(選択/税込))]/taxAmount /テキスト()

3.00

5.00

/ルート/行/チェック[しない(taxAmount = SUM(選択/税))]/SUM(選択/税/テキスト())

2.50

2.75

+1

あなたは私達にあなたのXMLドキュメントのサンプルを示してもらえますか? – skovorodkin

答えて

2

は、本質的に、私は 詳細情報と一致しないヘッダ情報をチェックしており、 ヘッダと比較する詳細を合計したかったです。

あなただけselections/taxの合計がtaxAmount等しくないところchecksを特定しようとしている場合は、使用することができます:あなたのサンプル入力に基づいて

/root/row/checks[not(taxAmount = sum(selections/tax))] 

を、この第二checks要素を選択することになります。あなたのサンプル入力を使用して

/root/row/checks[not(taxAmount = sum(selections/tax))]/concat(taxAmount,' ',sum(selections/tax)) 

この希望出力3.00 2.5selections/taxの出力の両方taxAmountと合計に

は、次の(のXPath 2.0)を使用することができます。

+0

あなたの答えに基づいて私の投稿を更新しました。 –

+1

私はXPath 1.0を使用していました。そのため、最後の合計は期待どおりに機能しませんでした。 Derp ...ありがとう。 –

0

私はlxmlでのPythonを使用して、私はデルタが適切な要素を取得することがイプシロンよりも大きいかどうかを確認する必要があります。悲しいこと

/root/row/checks[sum(./selections/tax/text()) - ./taxAmount/text() > 1e-6 or sum(./selections/tax/text()) - ./taxAmount/text() < -1e-6] 

を、XPath 1.0のは(libxml2のは、新しいバージョンをサポートしていません)abs()を持っていません関数なので、orを使う必要があります。

平等のチェックが私のために動作しません:

>>> tree.xpath('sum((//checks)[1]/selections/tax/text())') 
4.140000000000001 

>>> tree.xpath('sum((//checks)[1]/selections/tax/text()) - (//checks)[1]/taxAmount/text()') 
8.881784197001252e-16