1
  1. I want to keep the first occurance of the duplicate topics. The duplicate topic may appear at any level in the hierarchy.
  2. In my original XSLT the input XML will be a variable.

Input.xml as Variable ($Ixml)

      <map href="A.xml">
          <topic href="1.xml"/>
          <topic href="2.xml"/>
          <map href="C.xml">
              <topic href="3.xml">
                  <topic href="4.xml"/>
              </topic>
          </map>
          <map href="B.xml">
              <topic href="5.xml">
                  <topic href="6.xml"/>
                  <topic href="7.xml"/>
                  <topic href="8.xml"/>
                  <topic href="4.xml"/>
                  <topic href="9.xml"/>
                  <topic href="10.xml"/>
                  <topic href="11.xml"/>
                  <topic href="12.xml"/>
              </topic>
          </map>
      </map>

Expected - Output.xml ($Oxml)

      <map href="A.xml">
          <topic href="1.xml"/>
          <topic href="2.xml"/>
          <map href="C.xml">
              <topic href="3.xml">
                  <topic href="4.xml"/>
              </topic>
          </map>
          <map href="B.xml">
              <topic href="5.xml">
                  <topic href="6.xml"/>
                  <topic href="7.xml"/>
                  <topic href="8.xml"/>

                  <topic href="9.xml"/>
                  <topic href="10.xml"/>
                  <topic href="11.xml"/>
                  <topic href="12.xml"/>
              </topic>
          </map>
      </map>

Based on the suggestion i have updated my XSLT as below:---------------------------------------------------------

    <xsl:key name="removeDuplicate" match="topic" use="@href"/>
        <xsl:template match="/">
            <xsl:variable name="Ixml">
                <map href="A.xml">
                    <topic href="1.xml"/>
                    <topic href="2.xml"/>
                    <map href="C.xml">
                        <topic href="3.xml">
                            <topic href="4.xml"/>
                        </topic>
                    </map>
                    <map href="B.xml">
                        <topic href="5.xml">
                            <topic href="6.xml"/>
                            <topic href="7.xml"/>
                            <topic href="8.xml"/>
                            <topic href="4.xml"/>
                            <topic href="9.xml"/>
                            <topic href="10.xml"/>
                            <topic href="11.xml">
                                <topic href="4.xml"/>
                            </topic>
                            <topic href="12.xml"/>
                        </topic>
                    </map>
                </map>
            </xsl:variable>
            <xsl:variable name="Oxml">
                <xsl:apply-templates select="$Ixml" mode="removeDuplicates"/>
            </xsl:variable>
            <xsl:copy-of select="$Oxml"/>        
        </xsl:template>
        <xsl:template match="node() | @*" mode="removeDuplicates">
            <xsl:copy>
                <xsl:apply-templates select="node() | @*" mode="removeDuplicates"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="topic[@href][not(. is key('removeDuplicate', @href)[1])]" mode="removeDuplicates"/>      
     </xsl:stylesheet>
  • Possible duplicate of [How to use XSLT to create distinct values](https://stackoverflow.com/questions/2291567/how-to-use-xslt-to-create-distinct-values) – stuartd Nov 06 '17 at 11:47
  • 1
    I am not sure why you need to use variables here, but you should just be able to do `` within variable declaraton for `Oxml`. – Tim C Nov 06 '17 at 13:02
  • Note also that the XSLT 1 based `` can in XSLT 2 be expressed easier as ``. – Martin Honnen Nov 06 '17 at 14:05
  • @MartinHonnen Thanks! This helps. based on your suggestion I have updated the question with updated xslt. Can you please take a look and let me know if the updated xslt can be fine-tuned? – DitaNewbie La Nov 06 '17 at 17:44
  • If you have other modes which also need the identity transformation you can write that template with `mode="#all"` on the `xsl:template` and `mode="#current"` on the inner `apply-templates`. – Martin Honnen Nov 06 '17 at 18:19

0 Answers0