0
votes

I know that there are a bunch of examples here on how to do Muenchian grouping, but I have been trying for quite some time and havent been able to get it.

I am trying to group <spec> based on their <title> value using Muenchian grouping.

I am using XSLT 1.0.

XML:

<product>
    <specifications>
        <spec>
            <title>A</title>
            <value>val1</value>
        </spec>
        <spec>
            <title>A</title>
            <value>val2</value>
        </spec>
        <spec>
            <title>B</title>
            <value>val3</value>
        </spec>
        <spec>
            <title>C</title>
            <value>val4</value>
        </spec>
        <spec>
            <title>C</title>
            <value>val5</value>
        </spec>
        <spec>
            <title>D</title>
            <value>val6</value>
        </spec>
    <specifications>
</product>

Wanted Result:

<group>
    <title>A</title>
    <values>
        <value>val1</value>
        <value>val2</value>
    </values>
</group>
<group>
    <title>B</title>
    <values>
        <value>val3</value>
    </values>
</group>
<group>
    <title>C</title>
    <values>
        <value>val4</value>
        <value>val5</value>
    </values>
</group>
<group>
    <title>D</title>
    <values>
        <value>val6</value>
    </values>
</group>
1
Read jenitennison.com/xslt/grouping/muenchian.html and adjust it to your needs.Martin Honnen
What have you tried? It's pretty much a given that your XSLT processor is not going to perform Muenchian grouping correctly if you don't give it an XSLT stylesheet. It's also pretty much a given that if you don't show what you've tried, many question answerers on Stack Overflow will assume you're trying to get other people to do your work for you. Unfair, I know, but compatible with all the evidence you've provided so far.C. M. Sperberg-McQueen

1 Answers

0
votes

Define a grouping key for the <title>.

<xsl:key name="kTitle" match="spec" use="title" />

XSL to achieve the desired output

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:key name="kTitle" match="spec" use="title" />

    <xsl:template match="spec[generate-id() = generate-id(key('kTitle', title)[1])]">
        <group>
            <xsl:copy-of select="title" />
            <values>
                <xsl:copy-of select="key('kTitle', title)/value" />
            </values>
        </group>
    </xsl:template>

    <xsl:template match="spec" />
</xsl:stylesheet>

Output

<group>
    <title>A</title>
    <values>
        <value>val1</value>
        <value>val2</value>
    </values>
</group>
<group>
    <title>B</title>
    <values>
        <value>val3</value>
    </values>
</group>
<group>
    <title>C</title>
    <values>
        <value>val4</value>
        <value>val5</value>
    </values>
</group>
<group>
    <title>D</title>
    <values>
        <value>val6</value>
    </values>
</group>