Aravind
Aravind

Reputation: 845

How to Iterate through nodes and skip duplicate nodes with same value- XSLT 1,0

i have a xml like,

        <DESIGN-FUNCTION-PROTOTYPE>
          <SHORT-NAME>xxx</SHORT-NAME>
          <TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">ABC/DEF/123</TYPE-TREF>
        </DESIGN-FUNCTION-PROTOTYPE>
        <DESIGN-FUNCTION-PROTOTYPE>
          <SHORT-NAME>yyy</SHORT-NAME>
          <TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">LMN/OPQ/123</TYPE-TREF>
        </DESIGN-FUNCTION-PROTOTYPE>
        <DESIGN-FUNCTION-PROTOTYPE>
          <SHORT-NAME>mmm</SHORT-NAME>
          <TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">XYZ/GHY/456</TYPE-TREF>
        </DESIGN-FUNCTION-PROTOTYPE>
        <DESIGN-FUNCTION-PROTOTYPE>
          <SHORT-NAME>nnn</SHORT-NAME>
          <TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">AJK/UTL/456</TYPE-TREF>
        </DESIGN-FUNCTION-PROTOTYPE>

My xslt,

    <xsl:template name="substring-after-last">
    <xsl:param name="string" />
    <xsl:param name="delimiter" />
    <xsl:choose>
      <xsl:when test="contains($string, $delimiter)">
        <xsl:call-template name="substring-after-last">
          <xsl:with-param name="string"
            select="substring-after($string, $delimiter)" />
          <xsl:with-param name="delimiter" select="$delimiter" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
          <xsl:value-of select="$string" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

<xsl:for-each select="select="//DESIGN-FUNCTION-PROTOTYPE/ea:TYPE-TREF[@TYPE='DESIGN-FUNCTION-TYPE']">
      <xsl:variable name="myVar" select="current()"/>
      <xsl:variable name="taskName" select="../ea:SHORT-NAME"/>

         <xsl:variable name="Var7">    
         <xsl:call-template name="substring-after-last">
         <xsl:with-param name="string" select="$myVar" />
         <xsl:with-param name="delimiter" select="'/'" />
         </xsl:call-template>
         </xsl:variable>

         <varoutput> 
         <xsl:value-of select="$Var7"/>
         </varoutput>

</xsl:for-each>

My intention here is to iterate all the 'DESIGN-FUNCTION-PROTOTYPE' elements and display the sub-string of 'TYPE-TREF' value, but if a sub-string of 'TYPE-TREF' value has already been read..i must skip that element.

Expected output,

123
456

And Not,

123
123
456
456

In general I should consider only the first occurrence and skip the rest.

Upvotes: 0

Views: 438

Answers (1)

zx485
zx485

Reputation: 29042

You can achieve this using the technique of Muenchian Grouping, the grouping method for XSLT-1.0.
The following code sample implements it to sort-out doublettes:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" />
    <xsl:key name="proto" match="DESIGN-FUNCTION-PROTOTYPE" use="TYPE-TREF" />

    <xsl:template match="/">
        <xsl:for-each select="//DESIGN-FUNCTION-PROTOTYPE[generate-id() = generate-id(key('proto',TYPE-TREF)[1])]">
            <varoutput> 
                <xsl:value-of select="TYPE-TREF"/>
            </varoutput>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Its output is:

<varoutput>ABC</varoutput>
<varoutput>XYZ</varoutput>

An answer for your now different question, implementing the same technique, can be

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" />
    <xsl:key name="proto" match="DESIGN-FUNCTION-PROTOTYPE" use="substring-after(substring-after(TYPE-TREF,'/'),'/')" />

    <xsl:template match="/">
        <xsl:for-each select="//DESIGN-FUNCTION-PROTOTYPE[generate-id() = generate-id(key('proto',substring-after(substring-after(TYPE-TREF,'/'),'/'))[1])]">
            <xsl:value-of select="substring-after(substring-after(TYPE-TREF,'/'),'/')"/><xsl:text>&#xA;</xsl:text>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Its output is:

123
456

Please note that substantially changing the question after a fitting answer is frowned upon by SO rules. Do not do that again.

Upvotes: 2

Related Questions