0

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?

0

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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

Comments

0

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>

4 Comments

group by is an invalid attribute for XSL for each.thats the error i am getting now
@AKS: This is XSL version 2.0. Please check your version and apply it for <xsl:for-each-group>
can please you show me how i can apply this xsl version to for each loop only
@AKS: Do you need for XSL version 1.0 only. If possible change your xsl version to 2.0 and use the above coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.