![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Getting distinct values [Thread Next] Re: Getting distinct valuesTo: NULL Date: 3/29/2008 2:22:00 PM chris fellows wrote: > Output XML required: > <Transformed> > <Record Id="1"> > <ColValuesA> > <ColA>A1</ColA> > <ColA>A2</ColA> > </ColValuesA> > <ColValuesB> > <ColB>B1</ColB> > <ColB>B2</ColB> > <ColB>B3</ColB> > </ColValuesB> > </Record> > <Record Id="2"> > ...more... > </Record> > </Transformed> Here is an XSLT 2.0 stylesheet that uses xsl:for-each-group and the distinct-values function: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="Root"> <Tranformed> <xsl:for-each-group select="Record" group-by="@Id"> <Record Id="{current-grouping-key()}"> <ColValuesA> <xsl:for-each select="distinct-values(current-group()/ColA)"> <ColA> <xsl:value-of select="."/> </ColA> </xsl:for-each> </ColValuesA> <ColValuesB> <xsl:for-each select="distinct-values(current-group()/ColB)"> <ColB> <xsl:value-of select="."/> </ColB> </xsl:for-each> </ColValuesB> </Record> </xsl:for-each-group> </Tranformed> </xsl:template> </xsl:stylesheet> You can use XSLT 2.0 with Saxon 9 <URL:http://saxon.sourceforge.net/>, with Gestalt <URL:http://gestalt.sourceforge.net/> and with AltovaXML tools <URL:http://www.altova.com/altovaxml.html> If you can't use XSLT 2.0 nevertheless then here is an XSLT 1.0 solution: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:key name="by-id" match="Record" use="@Id"/> <xsl:key name="by-a" match="ColA" use="concat(../@Id, '_', .)"/> <xsl:key name="by-b" match="ColB" use="concat(../@Id, '_', .)"/> <xsl:template match="Root"> <Transformed> <xsl:for-each select="Record[generate-id() = generate-id(key('by-id', @Id)[1])]"> <Record Id="{@Id}"> <ColValuesA> <xsl:for-each select="key('by-id', @Id)/ColA[generate-id() = generate-id(key('by-a', concat(../@Id, '_', .))[1])]"> <ColA> <xsl:value-of select="."/> </ColA> </xsl:for-each> </ColValuesA> <ColValuesB> <xsl:for-each select="key('by-id', @Id)/ColB[generate-id() = generate-id(key('by-b', concat(../@Id, '_', .))[1])]"> <ColB> <xsl:value-of select="."/> </ColB> </xsl:for-each> </ColValuesB> </Record> </xsl:for-each> </Transformed> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
