按元素和总和值分组 XSLT

Group by element and sum values XSLT

我是 XSLT 的新手,遇到了一个问题。我做了一些搜索,我读到了有关 Muenchian 分组的内容,但我不知道如何在这个问题中使用它。

我正在尝试用 XSLT 编写代码来读取每个团队名称并对每个团队的目标求和。 我正在使用 XML v 1.0

下面是我在 XML 中的当前数据文件:

<footballLeague>
    <round num="1">
        <match>
            <local>
                <teamName>AA</teamName>
                <goals>0</goals>
            </local>
            <visitor>
                <teamName>BB</teamName>
                <goals>1</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>CC</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>DD</teamName>
                <goals>0</goals>
            </visitor>
        </match>
    </round>
    <round num="2">
        <match>
            <local>
                <teamName>DD</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>AA</teamName>
                <goals>1</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>BB</teamName>
                <goals>0</goals>
            </local>
            <visitor>
                <teamName>CC</teamName>
                <goals>1</goals>
            </visitor>
        </match>
    </round>
    <round num="3">
        <match>
            <local>
                <teamName>DD</teamName>
                <goals>0</goals>
            </local>
            <visitor>
                <teamName>BB</teamName>
                <goals>1</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>CC</teamName>
                <goals>0</goals>
            </local>
            <visitor>
                <teamName>AA</teamName>
                <goals>1</goals>
            </visitor>
        </match>
    </round>
    <round num="4">
        <match>
            <local>
                <teamName>BB</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>AA</teamName>
                <goals>0</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>DD</teamName>
                <goals>0</goals>
            </local>
            <visitor>
                <teamName>CC</teamName>
                <goals>1</goals>
            </visitor>
        </match>
    </round>
    <round num="5">
        <match>
            <local>
                <teamName>AA</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>DD</teamName>
                <goals>1</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>CC</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>BB</teamName>
                <goals>1</goals>
            </visitor>
        </match>
    </round>
    <round num="6">
        <match>
            <local>
                <teamName>BB</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>DD</teamName>
                <goals>0</goals>
            </visitor>
        </match>
        <match>
            <local>
                <teamName>AA</teamName>
                <goals>1</goals>
            </local>
            <visitor>
                <teamName>CC</teamName>
                <goals>0</goals>
            </visitor>
        </match>
    </round>
</footballLeague>

我希望在 运行 XSLT 之后实现的输出是:

Team Name | Goals For | Goals against | Games Won | Tied Matches
    AA    |      4    |        4      |      2    |      2
    BB    |      5    |        2      |      4    |      1
    CC    |      4    |        3      |      3    |      1
    DD    |      2    |        6      |      0    |      2

如果能帮助我入门,那就太棒了!

看看这是否可以帮助您入门:

XSLT 1.0

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

<xsl:key name="score" match="local|visitor" use="teamName" />

<xsl:template match="footballLeague">
    <stats> 
        <xsl:for-each select="round/match/*[count(. | key('score', teamName)[1]) = 1]">
            <team>
                <xsl:copy-of select="teamName"/>
                <goals-for>
                    <xsl:value-of select="sum(key('score', teamName)/goals)"/>
                </goals-for>
                <goals-against>
                    <xsl:value-of select="sum(key('score', teamName)[self::local]/../visitor/goals) + sum(key('score', teamName)[self::visitor]/../local/goals)"/>
                </goals-against>
            </team>
        </xsl:for-each>
    </stats>
</xsl:template>

</xsl:stylesheet>

应用于您的示例,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<stats>
  <team>
    <teamName>AA</teamName>
    <goals-for>4</goals-for>
    <goals-against>4</goals-against>
  </team>
  <team>
    <teamName>BB</teamName>
    <goals-for>5</goals-for>
    <goals-against>2</goals-against>
  </team>
  <team>
    <teamName>CC</teamName>
    <goals-for>4</goals-for>
    <goals-against>3</goals-against>
  </team>
  <team>
    <teamName>DD</teamName>
    <goals-for>2</goals-for>
    <goals-against>6</goals-against>
  </team>
</stats>