Ray
Ray

Reputation: 43

XSL 2.0 output only first duplicate node

I tried searching the threads related to duplicate nodes and was almost able to achieve, it avoids duplicate nodes but it outputs the last duplicate node instead of the first node in the duplicate list (hope this is making sense). Please advise what I'm doing wrong/missing here ?

    =====XML =====
    <node id="j0dp1s8s">
        <name key="">ABC</name> 
        <link type="page" target="">
            <value>abc/index</value>
        </link>
    </node>
    <node id="j0dp1s8se">
        <name key="">DEF</name> 
        <link type="page" target="">
            <value>def/index</value>
        </link>
    </node>
    <node id="j0dp1s92">
        <name key="">XYZ</name> 
        <link type="page" target="">
            <value>abc/index</value>
        </link>
    </node>

    =======XSL=============
    <xsl:variable name="unique-list" select="link[not(value=following::link/value)]" />  
    <xsl:for-each select="$unique-list">
        <li><a href="#"><xsl:value-of select="../name" /></a></li>
    </xsl:for-each>

Output: 
DEF
XYZ

Desired Output:
ABC
DEF

Upvotes: 1

Views: 262

Answers (1)

uL1
uL1

Reputation: 2167

Prolog

  • Your source XML is not valid. Element name can't be closed by label [deprecated]
  • I do not show your mistakes, I just provide you a much simpler and working code
  • I added a root nodes to make your source XML valid

XSLT:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="html"/>

    <xsl:template match="nodes">
        <ul>
            <xsl:for-each-group select="node" group-by="link/value">
              <li><a href="#"><xsl:value-of select="name" /></a></li>
            </xsl:for-each-group>
        </ul>        
    </xsl:template>

</xsl:transform>

Therefore you can use XSLT 2, take the lovly functionality of it. https://www.w3.org/TR/xslt20/#element-for-each-group

Upvotes: 2

Related Questions