I need to solve a groping problem in my xsl; I have to group the input xml based on , and . If any of these data is not same then create new tag and copy the respective inside that. If you check my xslt i am able to do the grouping by but not sure how to incorporate the same logic for and . I am not sure if i explained the problem very well but if u see the "needed output" section it would be more easy to understand what I want. Thanks in advance for your help.
Input:
<?xml version="1.0" encoding="UTF-8"?>
<BC>
<SO>
<plantCode>xyz</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>1</package>
....
</SO>
<SO>
<plantCode>xyz</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>2</package>
...
</SO>
<SO>
<plantCode>abc</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>5</package>
....
</SO>
<SO>
<plantCode>abc</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>AB</airportofunloading>
<package>1</package>
....
</SO>
</BC>
Needed Output:
<?xml version="1.0" encoding="UTF-8"?>
<BC>
<plant name="xyz">
<SO>
<plantCode>xyz</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>1</package>
....
</SO>
<SO>
<plantCode>xyz</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>2</package>
....
</SO>
</plant>
<plant name="abc">
<SO>
<plantCode>abc</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>UK</airportofunloading>
<package>5</package>
....
</SO>
</plant>
<plant name="abc">
<SO>
<plantCode>abc</plantCode>
<airportofloading>US</airportofloading>
<airportofunloading>AB</airportofunloading>
<package>1</package>
....
</SO>
</plant>
</BC>
My XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml"/>
<xsl:key name="Code" match="SO" use="plantCode"/>
<xsl:template match="BC">
<xsl:element name="BC">
<xsl:apply-templates select="SO[generate-id(.) = generate-id(key ('Code', plantCode)[1])]"/>
</xsl:element>
</xsl:template>
<xsl:template match="SO">
<xsl:element name="plant">
<xsl:attribute name="name"><xsl:value-of select="plantCode"/></xsl:attribute>
<xsl:for-each select="key('Code', plantCode)">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>