Reputation: 5670
My xml is like this
<syddata lastUpdated="19.04.2013 12:40 ">
<data itemnumber="ln1044-wh-36" variant1="White" variant2="Size 36" variant1group="Farver" variant2group="Størrelser" var1code="wh" var2code="36" estocklevel="0,000000000000" sortering="0"></data>
<data itemnumber="ln1044-wh-38" variant1="White" variant2="Size 38" variant1group="Farver" variant2group="Størrelser" var1code="wh" var2code="38" estocklevel="0,000000000000" sortering="0"></data>
<data itemnumber="ln1044-wh-40" variant1="White" variant2="Size 40" variant1group="Farver" variant2group="Størrelser" var1code="wh" var2code="40" estocklevel="0,000000000000" sortering="0"></data>
<data itemnumber="ln1044-wh-42" variant1="White" variant2="Size 42" variant1group="Farver" variant2group="Størrelser" var1code="wh" var2code="42" estocklevel="0,000000000000" sortering="0"></data>
<data itemnumber="ln1044-wh-44" variant1="White" variant2="Size 44" variant1group="Farver" variant2group="Størrelser" var1code="wh" var2code="44" estocklevel="0,000000000000" sortering="0"></data>
</syddata>
and I have a for each loop
<xsl:for-each select="$variants/syddata/data">
<xsl:value-of select="@variant1"/>
</xsl:for-each>
which will output white as 5 times. But I just want to out put it one time. Means I want to get distinct values. Is there any way to achieve this?
Upvotes: 0
Views: 3512
Reputation: 52848
If you need to get distinct values in XSLT 1.0, one of the most efficient ways is by using an xsl:key
. (Muenchian grouping.)
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="variant1" match="data" use="@variant1"/>
<xsl:template match="/syddata">
<distinct-values>
<xsl:for-each select="data[count(.|key('variant1',@variant1)[1])=1]">
<value><xsl:value-of select="@variant1"/></value>
</xsl:for-each>
</distinct-values>
</xsl:template>
</xsl:stylesheet>
Working example: http://xsltransform.net/bdxtqU
Upvotes: 1
Reputation: 9627
Try something like this:
<xsl:for-each select="data">
<xsl:variable name="variant1" select="@variant1"/>
<xsl:if test="not(following-sibling::data[@variant1= $variant1])">
<xsl:value-of select="@variant1"/>
</xsl:if>
</xsl:for-each>
Upvotes: 1
Reputation: 126722
Use just the first data
element with each @variant1
attribute value by making sure sure there are no preceding sibling elements with a matching attribute:
<xsl:for-each select="$variants/syddata/data">
<xsl:if test="not(preceding-sibling::data[@variant1 = current()/@variant1])">
<xsl:value-of select="@variant1"/>
</xsl:if>
</xsl:for-each>
Upvotes: 2
Reputation: 459
When the below transformation runs on your Input XML will get the required output
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each-group select="syddata/data" group-by="@variant1">
<xsl:value-of select="distinct-values(@variant1)"/>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0