Muenchian组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>元素为"英超联赛"的记录。该类别还应具有计数属性,该属性列出了相应记录的数量。

XSL-File之后将有效,但缺点,它还将列出没有找到"英超"游戏的类别。理想情况下,根本不应该有类别标签。

<?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>

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

<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。

这是一个优化和更正的版本。您只能将所需元素分组,然后过滤剩余的元素。
编辑:感谢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>

相关内容

  • 没有找到相关文章

最新更新