VilleLipponen
VilleLipponen

Reputation: 656

XSL - group and concat values

I have the following XML:

 <Records>
        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>2</ReportEntryId>
            <TaxAuthorityLabel>AA</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>2</ReportEntryId>
            <TaxAuthorityLabel>ZZ</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>3</ReportEntryId>
            <TaxAuthorityLabel>XX</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>3</ReportEntryId>
            <TaxAuthorityLabel>EE</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>
    </Records>

I need to group/concat all TaxAuthorityLabel values under FutureUse for every unique ReportEntryId value. What need to transform this to is:

<Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>2</ReportEntryId>
        <TaxAuthorityLabel>AA</TaxAuthorityLabel>
        <FutureUse>AA_ZZ</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>2</ReportEntryId>
        <TaxAuthorityLabel>ZZ</TaxAuthorityLabel>
        <FutureUse>AA_ZZ</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>3</ReportEntryId>
        <TaxAuthorityLabel>XX</TaxAuthorityLabel>
        <FutureUse>XX_EE</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>3</ReportEntryId>
        <TaxAuthorityLabel>EE</TaxAuthorityLabel>
        <FutureUse>XX_EE</FutureUse>
        <FutureUse32 />
    </Records>

The XSL that i have concats all TaxAuthorityLabel values under all nodes.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mapping="mapping:mapping" exclude-result-prefixes="mapping">
    <xsl:output method="xml"
                encoding="UTF-8"
                omit-xml-declaration="no"
                indent="yes" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="FutureUse"> <!-- we are setting every FutureUse -->
        <xsl:for-each select="../ReportEntryId" >
             <FutureUse>
                <xsl:if test=". = 2" >
                    <xsl:for-each select="../../Records/TaxAuthorityLabel" >
                        <xsl:value-of select="." />
                        <xsl:value-of select="'_'" />
                    </xsl:for-each>
                </xsl:if>
            </FutureUse>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I am finding it challenging obtaining the previous value of the ReportEntryId so I could compare them.

Any help with this is greatly appreciated.

Upvotes: 0

Views: 891

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117175

I'd suggest you try it this way:

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:strip-space elements="*"/>

<xsl:key name="record-by-entry" match="Record" use="ReportEntryId" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="FutureUse">
    <xsl:copy>
        <xsl:for-each select="key('record-by-entry', ../ReportEntryId)" >
            <xsl:value-of select="TaxAuthorityLabel" />
            <xsl:if test="position()!=last()">
                <xsl:text>_</xsl:text>
            </xsl:if>
        </xsl:for-each> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions