-1

How XSL will work If we have more then one same record in XML, ignore them and extract only rest data. my xsl code handling only duplicate value, i need only that record which is not duplicate or more then one. Below is my XSL:

<xsl:template match="/*">
<xsl:for-each-group select="creation" group-by="id">
<xsl:sequence select="."/>
</xsl:for-each-group>
</xsl:template>

Input:

<?xml version="1.0" encoding="UTF-8"?>
<creations>
<creation>
<id>074</id>
</creation>
<creation>
<id>074</id>
</creation>
<creation>
<id>001</id>
</creation>
<creation>
<id>074</id>
</creation>
</creations>

Expected output:

  <creation>
  <id>001</id>
  </creation>
Rahul Singh
  • 11
  • 1
  • 7
  • 2
    You've been here a year, asked 9 questions, received downvotes on some and upvotes on 0, and accepted 0 answers. ***You really must read and embrace [ask] and other [help] topics in order to use this site effectively.*** Otherwise, you're just wasting everyone's time, including your own. – kjhughes Sep 28 '16 at 16:32
  • 1
    See [Remove duplicated elements via XSLT](http://stackoverflow.com/questions/10912544/removing-duplicate-elements-with-xslt) – uL1 Sep 28 '16 at 17:14
  • I have tried with this code, but that not working: – Rahul Singh Sep 28 '16 at 18:07
  • 1
    Please don't post code in comments - edit your question instead. – michael.hor257k Sep 28 '16 at 18:45

2 Answers2

0

Solution with Muenchian-Grouping:

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

    <xsl:key name="ccid" match="creation" use="id"/>

    <xsl:template match="creations">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="creation[count(key('ccid', id)) &gt; 1]"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XSLT 2.0 with grouping

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

    <xsl:template match="creations">
        <xsl:for-each-group select="creation" group-by="id" >
            <xsl:if test="count(current-group()) eq 1">
                <xsl:sequence select="."></xsl:sequence>
            </xsl:if>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Remove all duplicates of creation/id, so only unique creation/id remain.

uL1
  • 2,117
  • 2
  • 17
  • 28
0

All you have to do to get the expected result is:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="creation-by-id" match="creation" use="id"/> 

<xsl:template match="/creations">
    <xsl:copy-of select="creation[count(key('creation-by-id', id)) = 1]"/>
</xsl:template>

</xsl:stylesheet>

Note however that if there are two or more unique creation nodes, the result will not be well-formed XML (no single root element).

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51