Reputation: 40
I have XML data like this:
<data ItemCount="5">
<zrow GroupName="Manager"User="User1" />
<zrow GroupName="Developer"User="User2" />
<zrow GroupName="Manager"User="User3" />
<zrow GroupName="CEO"User="User4" />
<zrow GroupName="CEO"User="User5" />
</data>
I want output like this:
Manager
User1
User3
Developer
User2
CEO
User4
User5
What should be my XSLT? I want to create an XSLT which should transform my data to the above format. Could you please help me create one?
Upvotes: 0
Views: 83
Reputation: 60414
An XSLT 2.0 solution:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="data">
<xsl:for-each-group select="zrow" group-by="@GroupName">
<xsl:value-of select="current-grouping-key()" />
<xsl:text>
</xsl:text>
<xsl:value-of select="current-group()/@User" separator="
"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
An XSLT 1.0 solution relies on the Muenchian Method:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:key name="byGroupName" match="zrow" use="@GroupName"/>
<xsl:template
match="zrow[generate-id()=
generate-id(key('byGroupName', @GroupName)[1])]">
<xsl:value-of select="@GroupName"/>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="key('byGroupName', @GroupName)" mode="out"/>
</xsl:template>
<xsl:template match="zrow" mode="out">
<xsl:value-of select="@User"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="zrow"/>
</xsl:stylesheet>
Upvotes: 1