1

请注意,我是 XSLT 的新手,可能永远不必再次使用它。

我有一个简化的 XML 文档,如下所示:

<row>
    <tax_allocation_details>
        <tax_allocation_detail>
            <allocation_percent>0.0269</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.0583</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.1704</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.7444</allocation_percent>
        </tax_allocation_detail>
    </tax_allocation_details>
    <ParticipantID>364787525</ParticipantID>
    <TransactionAmount>407252.34</TransactionAmount>
</row>

我想要做的是输出所有分配百分比 * TransactionAmount 产品的总和,并且对如何做到这一点一无所知。我正在使用 XSLT 2.0 并进行了一些研究并发现

sum(for $x in node-set return f($x))

可以使用,但我完全不知道如何实现它,也没有时间去理解它。

4

2 回答 2

0

使用这个 XPath 2.0 表达式

sum(/*/*/tax_allocation_detail/allocation_percent/(../../../TransactionAmount * .))

基于 XSLT 2.0 的验证

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence select=
   "sum(/*/*/tax_allocation_detail/allocation_percent/(../../../TransactionAmount * .))"/>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<row>
    <tax_allocation_details>
        <tax_allocation_detail>
            <allocation_percent>0.0269</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.0583</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.1704</allocation_percent>
        </tax_allocation_detail>
        <tax_allocation_detail>
            <allocation_percent>0.7444</allocation_percent>
        </tax_allocation_detail>
    </tax_allocation_details>
    <ParticipantID>364787525</ParticipantID>
    <TransactionAmount>407252.34</TransactionAmount>
</row>

对上面的 XPath 表达式求值并输出该求值的结果:

407252.34
于 2020-07-16T01:43:51.483 回答
-1

您设置了一个匹配 tax_allocation_details 的模板,然后在节点之间求和:

<xsl:template match="tax_allocation_details">
    <p><xsl:value-of select="sum($tranAmount * tax_allocation_detail/allocation_percent)" /></p>
</xsl:template>
于 2020-07-15T07:17:36.607 回答