-1

Can someone help me plz to get a distinct values of attribute ID in node INDIC ?

Explication:

Im using xslt version 1.

  • This is how my xml looks like:

    <Results><Result>
        <INDICS>
            <INDIC ID="I000207" LIB="A"/>
            <INDIC ID="I000208" LIB="B"/>
            <INDIC ID="I999999" LIB="C"/>
        </INDICS>
    </Result>
    <Result>
        <INDICS>
            <INDIC ID="I000207" LIB="A"/>
            <INDIC ID="I000208" LIB="B"/>
            <INDIC ID="I999999" LIB="C"/>
        </INDICS>
    </Result>
    

this is how my template looks like, my template display all IDs

     <xsl:template match="/">
<xsl:variable name="INDICS" select="//INDIC"/>
     <PRODUITS>
     <INDICS>
     <xsl:for-each select="//Results/Result/INDICS/INDIC/@ID">
     <INDIC ID= "{.}" LIB="{$INDICS[@ID=current()]/@LIB}" />
     </xsl:for-each>
     </INDICS>
     </PRODUITS>
     </xsl:template>

the output that I get is:

<PRODUITS>
<INDICS>
<INDIC ID="I000207" LIB="A"/>
<INDIC ID="I000208" LIB="B"/>
<INDIC ID="I999999" LIB="C"/>
<INDIC ID="I000207" LIB="A"/>
<INDIC ID="I000208" LIB="B"/>
<INDIC ID="I999999" LIB="C"/>
 <INDICS/>
</PRODUITS>

the output that Im trying to get is:

<PRODUITS>
<INDICS>
<INDIC ID="I000207" LIB="A"/>
<INDIC ID="I000208" LIB="B"/>
<INDIC ID="I999999" LIB="C"/>
 <INDICS/>
</PRODUITS>

thanks alot.

Sacamoto
  • 107
  • 1
  • 8
  • 1
    There are already many answer like this. Next time please use a [search like this](https://stackoverflow.com/search?q=%5Bxslt%5D+unique) before asking. – zx485 Oct 26 '18 at 14:35
  • is like that you help people ? :3 – Sacamoto Oct 26 '18 at 14:45
  • 1
    It's from the usage guidelines for this site. See the second paragraph of [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – zx485 Oct 26 '18 at 14:51
  • I would be grateful if someone can help plz. – Sacamoto Oct 26 '18 at 15:08

2 Answers2

2

Another possibility used in the linked questions is using a xsl:key and the generate-id() function. It is a variation of the so called Muenchian Grouping.

<xsl:key name="values" match="INDIC" use="@ID"/>

<xsl:template match="/">
    <PRODUITS>
        <INDICS>
            <xsl:for-each select="//Results/Result/INDICS/INDIC[generate-id() = generate-id(key('values',@ID)[1])]">
                <INDIC ID= "{@ID}" LIB="{@LIB}" />
            </xsl:for-each>
        </INDICS>
    </PRODUITS>
</xsl:template>
Tim C
  • 70,053
  • 14
  • 74
  • 93
zx485
  • 28,498
  • 28
  • 50
  • 59
  • Sacamoto - It is well worth learning about this techique, as is it is much more efficient with larger data sources. See http://www.jenitennison.com/xslt/grouping/muenchian.html – Tim C Oct 26 '18 at 15:29
0

You can do like this, hope this code help you to solve your problem:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <Results>
            <Result>
                <INDICS>
                    <xsl:for-each select="//INDIC">
                        <xsl:variable name="c-id" select="@ID"/>
                        <xsl:choose>
                            <xsl:when test="$c-id = ancestor::Result/following-sibling::Result/INDICS/INDIC/@ID"/>
                            <xsl:otherwise>
                                <INDIC ID= "{@ID}" LIB="{@LIB}" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </INDICS>
            </Result>
        </Results>
    </xsl:template>

</xsl:stylesheet>
Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17