2

我是 XSLT 的新手,在完成结果时遇到了麻烦,我来到这里以防有人可以帮助我。

我有以下 XML:

<funds>
    <bags>
        <bag name="BAG_USD_MAIN" value="10.0" type="USD">
            <pockets>
                <pocket name="bank" value="7.5">
                <pocket name="award" value="2.5">
            </pockets>
         </bag>
        <bag name="BAG_USD_SAVINGS" value="290.75" type="USD">
            <pockets>
                <pocket name="bank" value="290.75">
            </pockets>
         </bag>
        <bag name="BAG_EUR_EXTRA" value="890.0" type="EUR">
            <pockets>
                <pocket name="bank" value="753.0">
                <pocket name="bank_eng" value="137.0">
            </pockets>
         </bag>
    </bags>
 </funds>

我希望能够以这种方式对其进行改造:

<result>
    <total type="USD">375.0</total>
    <total type="EUR">890.0</total>
</result>

XSLT 可以吗?

谢谢, 问候 TS

4

2 回答 2

3

Because you are using XSLT 2.0 you can use the <xsl:for-each-group> element to group elements by @type and then use sum() to sum the elements in the group.

The following stylesheet solves what you are trying to do:

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


    <!-- Match the bags element  -->
    <xsl:template match="bags">
        <result>
            <!-- Group each bag element by its type -->
            <xsl:for-each-group select="bag" group-by="@type">
                <!-- Use the current key to display the type attribute -->
                <total type="{current-grouping-key()}">
                    <!-- Sum all the elements from the current group -->
                    <xsl:value-of select="sum(current-group()/@value)" />
                </total>
            </xsl:for-each-group>
        </result>
    </xsl:template>

</xsl:stylesheet>

Just for completeness an XSLT 1.0 solution would be based on Muenchian Grouping.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <!-- Index all bag elements withing bags element using their
         type attribute by using a key -->
    <xsl:key name="currency-key"
             match="/funds/bags/bag"
             use="@type" />

    <!-- Match bags element -->
    <xsl:template match="bags">
        <result>
            <!-- Match the first bag element for a specific group -->
            <xsl:apply-templates select="bag[generate-id() = generate-id(key('currency-key', @type)[1])]" />
        </result>
    </xsl:template>

    <xsl:template match="bag">
        <total type="{@type}">
            <!-- Sum all the elements from the @type group -->
            <xsl:value-of select="sum(key('currency-key', @type)/@value)" />
        </total>
    </xsl:template>

</xsl:stylesheet>
于 2013-02-27T13:08:08.877 回答
0

是的。我手边没有 XSLT 处理器,但可以尝试以下方法:

<result>
  <xsl:for-each select="distinct-values(//bag/@type)">
    <total type="."><xsl:value-of select="sum(//bag[@type = .])"/></total>
  </xsl:for-each>
</result>

对于大量数据,您应该考虑<xsl:for-each-group>改用。

于 2013-02-27T13:05:04.797 回答