I use xsl version 2 to transform xml data This is the input xml file <?xml version="1.0"?> <csv xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <line> <field>210.84</field> <field>0</field> <field>1</field> <field>2</field> <field>3</field> </line> <line> <field>210.84</field> <field>0</field> <field>1</field> <field>2</field> <field>3</field> </line> <line> <field>210.84</field> <field>6</field> <field>5</field> <field>4</field> <field>3</field> </line> <line> <field>210.84</field> <field>6</field> <field>5</field> <field>4</field> <field>3</field> </line> <line> <field>210.84</field> <field>6</field> <field>5</field> <field>4</field> <field>3</field> </line> </csv> We need to get the DISTINCT values in the field[2] in each element <line> We have for this example two distinct value which are 0 and 6 in the elements <field[2]>. We need to have an output for these distinct values and the two elements following them. The output should be: <?xml version="1.0"?> <csv xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <line> <field>0</field> <field>1</field> <field>2</field> </line> <line> <field>6</field> <field>5</field> <field>4</field> </line> </csv>
heshamelesawy@gmail.com wrote: > We need to get the DISTINCT values in the field[2] in each element > <line> > We have for this example two distinct value which are 0 and 6 in the > elements <field[2]>. We need to have an output for these distinct > values and the two elements following them. You can use xsl:for-each-group in XSLT 2.0 as follows: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="csv"> <xsl:copy> <xsl:for-each-group select="line" group-by="field[2]"> <xsl:apply-templates select="current-group()[1]"/> </xsl:for-each-group> </xsl:copy> </xsl:template> <xsl:template match="line"> <xsl:copy> <xsl:apply-templates select="field[2] | field[3] | field[4]"/> </xsl:copy> </xsl:template> <xsl:template match="field"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/