Muenchian 将 XSLT 1.0 中的一系列元素组合在一起

Muenchian group a selection of elements in XSLT 1.0

考虑这个 XML 带有标题、类别和联赛的足球比赛文档。

<?xml version='1.0'?>
<games>
    <game>
        <title>Manchester City - Arsenal</title>
        <category>Live</category>
        <league>Premier League</league>
    </game>
    <game>
        <title>Barcelona - Real Madrid</title>
        <category>Live</category>
        <league>Primera Division</league>
    </game>
    <game>
        <title>Arsenal - Hull City</title>
        <category>Recap</category>
        <league>Premier League</league>
    </game>
    <game>
        <title>Everton - Liverpool</title>
        <category>Live</category>
        <league>Premier League</league>
    </game>
    <game>
        <title>Zaragoza - Deportivo</title>
        <category>Short Recap</category>
        <league>Primera Division</league>
    </game>
</games>

我试图按 类别 对这些游戏进行分组,但我只想保留 <league> 元素为 'Premier League' 的记录。该类别还应该有一个计数属性,其中列出了相应记录的数量。

关注 XSL-file 会起作用,但缺点是它还会列出未找到 'Premier League' 游戏的类别。理想情况下,根本不应该为这些分类标签。

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

 <xsl:key name="prod-cat" match="game" use="category"/>

    <xsl:template match="/">
        <root>
            <xsl:for-each select="games/game[count(. | key('prod-cat', category)[1]) = 1]">

                <category>

                <xsl:variable name="cat">
                    <xsl:value-of select="category"/>
                </xsl:variable>

                <xsl:variable name="count">
                    <xsl:value-of select="count(//game[category = $cat][league = 'Premier League'])"/>
                </xsl:variable>

                <xsl:attribute name="name">
                    <xsl:value-of select="category"/>
                </xsl:attribute>

                <xsl:attribute name="count">
                    <xsl:value-of select="$count"/>
                </xsl:attribute>

                    <xsl:for-each select="key('prod-cat', $cat)[league = 'Premier League']">
                        <game>
                            <title>
                                <xsl:value-of select="title"/>
                            </title>
                            <cat>
                                <xsl:value-of select="category"/>
                            </cat>
                            <series>
                                <xsl:value-of select="league"/>
                            </series>
                        </game>
                    </xsl:for-each>
                </category>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:transform>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <category name="Live" count="2">
      <game>
         <title>Manchester City - Arsenal</title>
         <cat>Live</cat>
         <series>Premier League</series>
      </game>
      <game>
         <title>Everton - Liverpool</title>
         <cat>Live</cat>
         <series>Premier League</series>
      </game>
   </category>
   <category name="Recap" count="1">
      <game>
         <title>Arsenal - Hull City</title>
         <cat>Recap</cat>
         <series>Premier League</series>
      </game>
   </category>
   <category name="Short Recap" count="0"/> <!--This one needs to go-->
</root>

这是一个优化和更正的版本。您可以仅对所需元素进行分组,过滤掉其余元素。
编辑: 感谢 Martin Honnen。我添加了一个 xsl:if 来去除输出中不相关的元素。

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:key name="prod-cat" match="game" use="category"/>
    <xsl:template match="/">
        <root>
            <xsl:for-each select="games/game[count(. | key('prod-cat', category)[1]) = 1]">
                <xsl:if test="key('prod-cat', category)[league = 'Premier League']">
                    <category name="{category}" count="{count(key('prod-cat', category)[league = 'Premier League'])}">
                        <xsl:for-each select="key('prod-cat', category)[league = 'Premier League']">
                            <game>
                                <title>
                                    <xsl:value-of select="title"/>
                                </title>
                                <cat>
                                    <xsl:value-of select="category"/>
                                </cat>
                                <series>
                                    <xsl:value-of select="league"/>
                                </series>
                            </game>
                        </xsl:for-each>
                    </category>
                </xsl:if>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:transform>

我会将条件放入键定义中:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:key name="prod-cat" match="game[league = 'Premier League']" use="category"/>
    <xsl:template match="/">
        <root>
            <xsl:for-each select="games/game[league = 'Premier League'][count(. | key('prod-cat', category)[1]) = 1]">
                <category name="{category}" count="{count(key('prod-cat', category))}">
                    <xsl:for-each select="key('prod-cat', category)">
                        <game>
                            <title>
                                <xsl:value-of select="title"/>
                            </title>
                            <cat>
                                <xsl:value-of select="category"/>
                            </cat>
                            <series>
                                <xsl:value-of select="league"/>
                            </series>
                        </game>
                    </xsl:for-each>
                </category>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:transform>

在线 http://xsltransform.net/ejivdHq/1