按元素和值分组 XSLT

问题描述 投票:0回答:1

我是XSLT的新手,我被一个问题卡住了。我已经做了一些搜索,我读到了Muenchian分组,但我不知道如何在这个问题上使用它。

我试图用XSLT写代码来读取每个TeamName和每个球队的目标总和,我使用的是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

任何帮助让我开始将是梦幻般的!

sum grouping xslt-1.0 xslt-grouping
1个回答
0
投票

看看这个能不能让你入门。

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>
© www.soinside.com 2019 - 2024. All rights reserved.