sqltobi
sqltobi

Reputation: 137

xslt 1.0 conditional grouping

in my source xml, I have a lines-element, containing N line-elements. Each line-element has a groupid and a lineNo-element. lineNo is a unique integer value.

I also have a groups-element, containing M group-elements. For each distinct groupid from the line-elements I have exactly one group-element, so groupid is unique within the group-elements.

Now, I want to transform the xml in a way, that I add data from the group-element to the corresponding line-element, but only to the line element with the min. lineNo for that groupid.

Can that be done with XSLT 1.0? See sample xmls below.

thank you very much & best regards

Tobias

Here's a simplified input:

<test>
    <lines>
        <line>
            <groupid>1</groupid>
            <lineNo>1</lineNo>
        </line>
        <line>
            <groupid>1</groupid>
            <lineNo>2</lineNo>
        </line>
        <line>
            <groupid>1</groupid>
            <lineNo>3</lineNo>
        </line>        
        <line>
            <groupid>2</groupid>
            <lineNo>4</lineNo>
        </line>
        <line>
            <groupid>2</groupid>
            <lineNo>5</lineNo>
        </line>
    </lines>
    <groups>
        <group>
            <groupid>1</groupid>
            <groupTotal>100</groupTotal>
        </group>
        <group>
            <groupid>2</groupid>
            <groupTotal>200</groupTotal>
        </group>        
    </groups>
</test>

And here the expected result.

<test2>
    <lines>
        <line>
            <groupid>1</groupid>
            <lineNo>1</lineNo>
            <groupTotal>100</groupTotal>
        </line>
        <line>
            <groupid>1</groupid>
            <lineNo>2</lineNo>
            <groupTotal/>
        </line>
        <line>
            <groupid>1</groupid>
            <lineNo>3</lineNo>
            <groupTotal/>
        </line>        
        <line>
            <groupid>2</groupid>
            <lineNo>4</lineNo>
            <groupTotal>200</groupTotal>
        </line>
        <line>
            <groupid>2</groupid>
            <lineNo>5</lineNo>
            <groupTotal/>
        </line>
    </lines>
</test2>

Upvotes: 1

Views: 264

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

Here's one way to look at it:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="line-by-group" match="line" use="groupid" />

<xsl:template match="/">
    <test2>
        <lines>
            <xsl:for-each select="test/groups/group">
                <xsl:apply-templates select="key('line-by-group', groupid)">
                    <xsl:with-param name="total" select="groupTotal"/>
                </xsl:apply-templates>
            </xsl:for-each>
        </lines>    
    </test2>
</xsl:template>

<xsl:template match="line">
    <xsl:param name="total"/>
    <xsl:copy>
        <xsl:copy-of select="groupid | lineNo"/>
        <xsl:if test="position()=1">
            <groupTotal><xsl:value-of select="$total"/></groupTotal>
        </xsl:if>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Here's another (assuming lines are sorted by group):

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="group" match="group" use="groupid" />

<xsl:template match="/">
    <test2>
        <lines>
            <xsl:for-each select="test/lines/line">
                <xsl:copy>
                    <xsl:copy-of select="groupid | lineNo"/>
                    <xsl:if test="not(groupid=preceding-sibling::line[1]/groupid)">
                        <groupTotal>
                            <xsl:value-of select="key('group', groupid)/groupTotal"/>
                        </groupTotal>
                    </xsl:if>
                </xsl:copy>
            </xsl:for-each>
        </lines>    
    </test2>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions