Search code examples
xsltxslt-1.0xslt-grouping

How to concatenate XML field in different node, if a key field is common between them?


My source xml file has elements in this form:

<Employees>
<Employee>
<Name>ABC</Name>
<City>Delhi</Delhi>
</Employee>
<Employee>
<Name>ABC</Name>
<City>Mumbai</Delhi>
</Employee>
<Employee>
<Name>KBC</Name>
<City>Kolkata</Delhi>
</Employee>
</Employees>

I've used the following XSLT code

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <Employees> 
        <xsl:for-each-group select="row" group-starting-with="row[Name/text()]">
            <Employee>  
                <FIELD7>
                    <xsl:value-of select="City" separator="|"/>
                </FIELD7> 
            </Employee>
        </xsl:for-each-group>
    </Employees> 
</xsl:template>

</xsl:stylesheet>

Here, if the employee's name is same then the XSLT will concatenate the city with pipe. My expected output.

<Employees>
<Employee>
<Name>ABC</Name>
<City>Delhi|Mumbai</Delhi>
</Employee>
<Employee>
<Name>KBC</Name>
<City>Kolkata</Delhi>
</Employee>
</Employees>

Solution

  • You have tagged the question as XSLT 1.0 but in your attempt you use XSLT 2/3's for-each-group so assuming you are using an XSLT 3 processor like a current release of Saxon Java, Saxon JS or SaxonC or SaxonCS or .NET you would need code like

    <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:mode on-no-match="shallow-copy"/>
      
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/Employees">
        <xsl:copy> 
            <xsl:for-each-group select="Employee" group-by="Name">
                <xsl:copy>  
                    <xsl:apply-templates/> 
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy> 
    </xsl:template>
    
    <xsl:template match="Employee/City">
      <xsl:copy>
        <xsl:value-of select="current-group()/City" separator="|"/>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Online fiddle using Saxon 12 HE.