1
<xsl:variable name="Rows" select=" .. some stmt .." />
<xsl:for-each select="$Rows">
    <xsl:value-of select="@ATTRNAME"/>
</xsl:for-each>

Would like to know how to find 'Rows' with unique/distinct attribute 'ATTRNAME' [ in XSLT 1.0 ].

Oved
  • 21
  • 2
  • 6
  • possible duplicate question - http://stackoverflow.com/questions/1012321/xslt-distinct-select-group-by – Vitaliy Dec 16 '11 at 19:32

2 Answers2

1

Grouping in XSLT 1.0 is done using xsl:key. The following prints only the unique elements of the root element:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes"/>
    <xsl:key name="attrByVal" match="/*/@*" use="."/>
    <xsl:template match="/">
        <xsl:apply-templates select="/*/@*"/>
    </xsl:template>
    <xsl:template match="@*[generate-id()=generate-id(key('attrByVal', .)[1])]">
        <xsl:value-of select="concat(name(), ': ', ., '&#xA;')"/>
    </xsl:template>
    <xsl:template match="@*"/>
</xsl:stylesheet>

Explanation: First, we group all attributes of the root element by value:

<xsl:key name="attrByVal" match="/*/@*" use="."/>

Then, create a template that matches only the first element for each key in the group:

<xsl:template match="@*[generate-id()=generate-id(key('attrByVal', .)[1])]">

And ignore all the others:

<xsl:template match="@*"/>

Example input:

<root one="apple" two="apple" three="orange" four="apple"/>

Output:

one: apple
three: orange
Wayne
  • 59,728
  • 15
  • 131
  • 126
0

XSLT 2.0 solution :

<xsl:for-each-group select="$Rows" group-by="@ATTRNAME">
 <!-- Do something with the rows in the group. These rows
 can be accessed by the current-group() function. -->
    </xsl:for-each-group>
Vincent Biragnet
  • 2,950
  • 15
  • 22