Muenchian group a selection of elements in XSLT 1.0

78 views Asked by At

Consider this XML document of football games with a title, category and league.

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

I'm trying to group these games by category, but I only want to retain the records for which the <league> element is 'Premier League'. The category should also have a count attribute which lists the number of corresponding records.

Following XSL-file will work, but has the disadvantage that it will also list categories for which no 'Premier League' games are found. Ideally there shouldn't be a category tag for those at all.

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

Result:

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

There are 2 answers

0
Martin Honnen On BEST ANSWER

I would put the condition into the key definition:

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

Online at http://xsltransform.net/ejivdHq/1.

1
Lingamurthy CS On

Here is an optimized and corrected version. You can group only the required elements, filtering out the rest.
EDIT: Thanks to Martin Honnen. I've added an xsl:if instead to get rid of the irrelevant elements in output.

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