真正的问题:我使用的是带有 2.0 语法的 XPATH 1.0,所以下面的代码不起作用:/root/row/checks[not(taxAmount = sum(selections/tax))]/sum(selections/tax/文本())
我有以下 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())
我不想对整个语句求和,只是对/selections 下的子节点求和,但到目前为止我尝试的结果导致语法错误
本质上,我检查了与详细信息不匹配的 header 信息,并希望对详细信息求和以与 header 进行比较。
示例 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>
根据丹尼尔的回答编辑:
这不是我想要的。这将选择有问题的支票,但我想要这样的东西:
/root/row/checks[not(taxAmount = sum(selections/tax))]/taxAmount/text()
3.00
5.00
/root/row/checks[not(taxAmount = sum(selections/tax))]/sum(selections/tax/text())
2.50
2.75
最佳答案
Essentially, I have check header information which isn't matching detail information and wanted to sum the detail to compare with the header.
如果您只是想识别 checks
,其中 selections/tax
的总和不等于 taxAmount
,您可以使用:
/root/row/checks[not(taxAmount = sum(selections/tax))]
根据您的示例输入,这将选择第二个 checks
元素。
要同时输出 taxAmount
和 selections/tax
的总和,您可以使用以下 (XPath 2.0):
/root/row/checks[not(taxAmount = sum(selections/tax))]/concat(taxAmount,' ',sum(selections/tax))
这将使用您的示例输入输出 3.00 2.5
。
关于xml - XPATH:子节点上的总和不是整个 XPATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004410/