【问题标题】:How to calculate the sum of nodes grouped per units in XSLT 1.0?如何计算 XSLT 1.0 中每个单元分组的节点总和?
【发布时间】:2020-10-12 16:36:27
【问题描述】:

我想用 XSLT1.0 根据 XML 中的一个节点计算 XML 值的总和。 我需要 Routes 表中可用的所有 @Qty 值的总和。 Routes 中的每个 OrderTrans 都有一个 itedID,它将与 Products 中的 itemID 匹配。 Products 表也有ItemDetails 节点,每个产品都有一个unitID。我的最终结果应该显示按@unitID 分组的所有@Qty 的总和。

以下是 XML 源代码:

<root>
    <Routes>
        <OrderTrans itemID="1">
            <View Qty="5.00"/>
        </OrderTrans>
        <OrderTrans itemID="2">
            <View Qty="200.00"/>
        </OrderTrans>
        <OrderTrans itemID="2">
            <View Qty="10.00"/>
        </OrderTrans>
        <OrderTrans itemID="1">
            <View Qty="20.00"/>
        </OrderTrans>
    </Routes>
    <Products>
        <Product itemID="2">
            <ItemDetails unitID="KG"/>
        </Product>
        <Product itemID="1">
            <ItemDetails unitID="Pcs"/>
        </Product>
    </Products>
</root>

这是我尝试过的一个 sn-p:

<xsl:key name="groupkey" match="/root/Products/Product/ItemDetails" use="@unitID"/>
<xsl:template>
    <xsl:for-each select="/root/Products/Product/ItemDetails[generate-id(.) = generate-id(key('groupkey',@unitID)[1])]">
        <fo:block>Sum here:
            <xsl:value select="sum(../../../Routes/OrderTrans/View/@Qty[@unitID = current()/@unitID])">
        </fo:block>
    </xsl:for-each>
</xslt:template>

结果应该是:

Sum here: 25.00 Pcs. 210.00 KG

【问题讨论】:

  • 您的代码充满了错误,这使得我们这些想要使用它来解决您的问题的人无法使用它。请更多地考虑那些试图帮助你的人的时间。

标签: xml xslt xslt-1.0


【解决方案1】:

当您已经为每个产品拥有一个唯一节点时,我不明白为什么需要对任何内容进行分组。你可以这样做:

<xsl:key name="order" match="OrderTrans" use="@itemID"/>

<xsl:template match="/root">
    <your-root-here>
        <xsl:for-each select="Products/Product">
            <fo:block>
                <xsl:text>Sum here: </xsl:text>
                <xsl:value-of select="sum(key('order', @itemID)/View/@Qty)"/>
                <xsl:text> </xsl:text>
               <xsl:value-of select="ItemDetails/@unitID"/>
            </fo:block>
        </xsl:for-each>
    </your-root-here>
</xsl:template>

【讨论】:

    猜你喜欢
    • 2014-06-10
    • 2011-10-27
    • 2012-09-03
    • 2021-12-05
    • 2012-12-01
    • 1970-01-01
    • 2013-11-18
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多