xsl-list
[Top] [All Lists]

[xsl] Group elements by two keys

2009-09-21 03:02:38
Hallo all,

I have a want to group some elements by two keys. My problem is that I don't 
know how to create the second key which is a combined key. This key should be a 
string that contains the values of all itemA elements that are children of an 
info element.

Here is the xml:
<?xml version="1.0" encoding="UTF-8"?>
<test>
        <fir>
                <id>FIR1</id>
                <name>name1</name>
                <info>
                        <id>Info1</id>
                        <num>1</num>
                        <serie>A</serie>
                        <typ>COM</typ>
                        <itemA>
                                <id>N1</id>
                                <name>N_One</name>
                        </itemA>
                        <itemA>
                                <id>N2</id>
                                <name>N_Two</name>
                        </itemA>
                        <itemA>
                                <id>N3</id>
                                <name>N_Three</name>
                        </itemA>
                </info>
                <info>
                        <id>Info2</id>
                        <num>2</num>
                        <serie>A</serie>
                        <typ>RAF</typ>
                        <itemA>
                                <id>N1</id>
                                <name>N_One</name>
                        </itemA>
                        <itemA>
                                <id>N4</id>
                                <name>N_Four</name>
                        </itemA>
                </info>
        </fir>
        <fir>
                <id>FIR2</id>
                <name>name2</name>
                <info>
                        <id>Info3</id>
                        <num>3</num>
                        <serie>B</serie>
                        <typ>COM</typ>
                        <itemA>
                                <id>N1</id>
                                <name>N_One</name>
                        </itemA>
                        <itemA>
                                <id>N2</id>
                                <name>N_Two</name>
                        </itemA>
                </info>
                <info>
                        <id>Info5</id>
                        <num>2</num>
                        <serie>A</serie>
                        <typ>RAF</typ>
                        <itemA>
                                <id>N2</id>
                                <name>N_Two</name>
                        </itemA>
                        <itemA>
                                <id>N4</id>
                                <name>N_Four</name>
                        </itemA>
                </info>
                <info>
                        <id>Info4</id>
                        <num>4</num>
                        <serie>B</serie>
                        <typ>RAF</typ>
                        <itemA>
                                <id>N2</id>
                                <name>N_Two</name>
                        </itemA>
                        <itemA>
                                <id>N4</id>
                                <name>N_Four</name>
                        </itemA>
                </info>
        </fir>
</test>

This is my style sheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:fo="http://www.w3.org/1999/XSL/Format";>
        <xsl:key name="supra" match="fir/info" use="typ"/>

        <xsl:template match="/">
                <html>
                        <head>
                                <title>TEST</title>
                        </head>
                        <body>
                                <h1>TEST</h1>
                                <xsl:apply-templates/>
                        </body>
                </html>
        </xsl:template>

        <xsl:template match="test">
                <xsl:variable name="varKey" select="key('supra','COM')"/>
        COM<br/>
                <xsl:for-each select="$varKey">
                        <xsl:call-template name="info"/>
                </xsl:for-each>
                <xsl:variable name="varKey2" select="key('supra','RAF')"/>
        <br/>RAF<br/>
                <xsl:for-each select="$varKey2">
                        <xsl:call-template name="info"/>
                </xsl:for-each>
        </xsl:template>

        <xsl:template name="info">
                <xsl:for-each select="itemA">
                        <xsl:value-of select="id"/>-<xsl:value-of 
select="name"/>
                        <xsl:if test="position() != last()">
                                <xsl:text>, </xsl:text>
                        </xsl:if>
                </xsl:for-each>
                <br/>
                <xsl:value-of select="id"/>-<xsl:value-of select="num"/>
                <xsl:value-of select="serie"/>
                <br/>
        </xsl:template>
</xsl:stylesheet>

The desired output is:
TEST
COM
N1-N_One, N2-N_Two, N3-N_Three
Info1-1A
N1-N_One, N2-N_Two
Info3-3B

RAF
N1-N_One, N4-N_Four
Info2-2A
N2-N_Two, N4-N_Four
Info5-2A Info4-4B

As you can see the first group is the value of the fir/info/type element, the 
second group title should be a combined string created out of the data of itemA 
elements of a fir element.

I'd like to know if this is possible and if yes how.

Thanks and Kind Regards

Jens Reese

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>