Search code examples
xsltsoa

Eliminating duplicates from xslt 1.0


I'm New to xslt can any one help me on this

I tried several ways but no luck can you please help.

I can only use XSLT 1.0

    <?xml version="1.0" encoding="UTF-8"?><OrderNumberVar>
       <VariableCollection xmlns="http://www.mcp.com/xsd" 
       xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" 
       xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" 
   xmlns:tns="http://www.mcb.com/xsd">
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>263932</tns:OrderNumber>
      </tns:Variable>
   </VariableCollection>

Need to eliminate the duplicates from the above xml

</VariableCollection>
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>263932</tns:OrderNumber>
      </tns:Variable>
</VariableCollection>

Solution

  • With XSLT 1.0 you can do like this, checking with preceding-sibling values:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
        xmlns="http://www.mcp.com/xsd"
        xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE"
        xmlns:tns="http://www.mcb.com/xsd"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        exclude-result-prefixes="xs plnk wsdl client tns xsl" version="1.0">
    
        <xsl:strip-space elements="*"/>
    
        <xsl:output indent="yes"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="tns:Variable">
            <xsl:variable name="current">
                <xsl:value-of select="tns:OrderNumber"/>
            </xsl:variable>
            <xsl:if test="not(preceding-sibling::tns:Variable[tns:OrderNumber=$current])">
                <tns:Variable>
                    <tns:OrderNumber>
                        <xsl:value-of select="$current"/>
                    </tns:OrderNumber>
                </tns:Variable>
            </xsl:if>
        </xsl:template>
    
    </xsl:stylesheet>