<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 ].
<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 ].
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(), ': ', ., '
')"/>
</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
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>