How do I combine or merge grouped nodes?

Posted by LOlliffe on Stack Overflow See other posts from Stack Overflow or by LOlliffe
Published on 2010-03-15T20:14:06Z Indexed on 2010/03/15 23:39 UTC
Read the original article Hit count: 381

Filed under:
|
|
|
|

Using the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml"/>
<xsl:template match="/">
    <records>
        <record>
            <!-- Group record by bigID, for further processing -->
            <xsl:for-each-group select="records/record" group-by="bigID">
                <xsl:sort select="bigID"/>
                <xsl:for-each select="current-group()">
                    <!-- Create new combined record -->
                    <bigID>
                        <!-- <xsl:value-of select="."/> -->
                        <xsl:for-each select=".">
                            <xsl:value-of select="bigID"/>
                        </xsl:for-each>
                    </bigID>
                    <text>
                        <xsl:value-of select="text"/>
                    </text>
                </xsl:for-each>
            </xsl:for-each-group>
        </record>
    </records>
</xsl:template>

I'm trying to change:

<?xml version="1.0" encoding="UTF-8"?>
<records>
  <record>
    <bigID>123</bigID>
    <text>Contains text for 123</text>
    <bigID>456</bigID>
    <text>Some 456 text</text>
    <bigID>123</bigID>
    <text>More 123 text</text>
    <bigID>123</bigID>
    <text>Yet more 123 text</text>
</record>

into:

<?xml version="1.0" encoding="UTF-8"?>
<records>
  <record>
    <bigID>123</bigID>
       <text>Contains text for 123</text>
       <text>More 123 text</text>
       <text>Yet more 123 text</text>
    </bigID>
    <bigID>456
       <text>Some 456 text</text>
    </bigID>
</record>

Right now, I'm just listing the grouped <bigID>s, individually. I'm missing the step after grouping, where I combine the grouped <bigID> nodes. My suspicion is that I need to use the "key" function somehow, but I'm not sure.

Thanks for any help.

© Stack Overflow or respective owner

Related posts about grouping

Related posts about combining