按元素和值分组 XSLT

我是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

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

解决方案:

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

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>

给TA打赏
共{{data.count}}人
人已打赏
未分类

导入错误。没有名为selenium的模块,windows 10

2022-9-7 23:28:17

未分类

有没有一种方法可以从循环的文件目录中动态地生成一个字典。

2022-9-7 23:28:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索