xml - XPATH:子节点上的总和不是整个 XPATH

标签 xml xpath

真正的问题:我使用的是带有 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 元素。

要同时输出 taxAmountselections/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/

相关文章:

java - 在java中进行xml转换和过滤

sql - 具有多个 COUNT(DISTINCT xxx) 的 PL/SQL SELECT - 意外结果

javascript - 打印 JSON 对象数据时控制台上出现未定义消息

XML 设计和名称重用 - 不同类型的元素具有相同的名称是好是坏?

python - 如何在随机 lxml.html.Element (与根相对)的上下文中使用 xpath() ?

关于可以添加到 XML 文档的元素的 XML namespace 信息?

javascript - XMLHttpRequest 从本地服务器而不是外部服务器加载 XML?

sql - 改进 xpath 评估的 SQL 查询

python - 如何在 AngularJS 网站上的 Python 中使用 Selenium 迭代和保存动态表中的信息

python - 在 python 中使用 xpath 在所有 css 选择器中搜索整个页面中的特定单词