2017-09-01 92 views
3

現實的問題:我使用XPath 1.0與2.0語法,所以下面不工作:/root/row/checks[not(taxAmount = sum(selections/tax))]/sum(selections/tax/text())XPATH:在子節點一筆不整XPATH



我有以下XPATH:

/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 =總和(選擇/稅))] /總和(選擇/稅/文本())

2.50

2.75

+1

你能告訴我們你的XML文檔的樣本? – skovorodkin

回答

2

從本質上講,我有檢查未匹配 詳細信息的報頭信息,想總結的細節與 頭比較。

如果你只是試圖找出checksselections/tax之和不等於taxAmount,你可以使用:

/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.5

selections/tax輸出兩個taxAmount和金額,你可以使用下面(的XPath 2.0)。

+0

根據您的回答更新我的帖子。 –

+1

我在XPath 1.0上,這就是爲什麼最後一筆總額沒有按預期工作。 DERP ...謝謝。 –

0

我使用Python與lxml,我要檢查,如果增量比小量更大,以獲得相應的元素:

/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