***This is my input xml coming from source.***
<?xml version="1.0" encoding="UTF-8" ?>
<exampleElement xmlns="http://www.example.org">
<element1>
<Id>123</Id>
<Name>Imag</Name>
<Status>active</Status>
<EndDate/>
</element1>
<element1>
<Id>888</Id>
<Name>Preci</Name>
<Status>active</Status>
<EndDate/>
</element1>
<element1>
<Id>123</Id>
<Name>Imag</Name>
<Status>terminated</Status>
<EndDate>2016-12-10</EndDate>
</element1>
<element1>
<Id>143</Id>
<Name>kilok</Name>
<Status>terminated</Status>
<EndDate>2016-11-10</EndDate>
</element1>
</exampleElement>
In above input data two sets duplicated with different value.Where ID=123 element node has two records with EndDate value and with out EndDate.
**I want out put in this below format where one record set
from duplicates with EndDate tag empty.**
<?xml version = '1.0' encoding = 'UTF-8'?>
<ns1:exampleElement xmlns:ns1="http://www.example.org">
<ns1:element1>
<ns1:Id>888</ns1:Id>
<ns1:Name>Preci</ns1:Name>
<ns1:Status>active</ns1:Status>
<ns1:EndDate/>
</ns1:element1>
<ns1:element1>
<ns1:Id>123</ns1:Id>
<ns1:Name>Imag</ns1:Name>
<ns1:Status>active</ns1:Status>
<ns1:EndDate/>
</ns1:element1>
<ns1:element1>
<ns1:Id>143</ns1:Id>
<ns1:Name>Kilok</ns1:Name>
<ns1:Status>terminated</ns1:Status>
<ns1:EndDate>2016-11-10</ns1:EndDate>
</ns1:element1>
</ns1:exampleElement>
Please find the below my xslt where this will give unique records,but I want to get the unique record based on value.
<xsl:template match="/">
<ns1:exampleElement>
<xsl:for-each select="/ns1:exampleElement/ns1:element1[not(ns1:Id=following::ns1:Id)]">
<ns1:element1>
<ns1:Id>
<xsl:value-of select="./ns1:Id"/>
</ns1:Id>
<ns1:Name>
<xsl:value-of select="./ns1:Name"/>
</ns1:Name>
<ns1:Status>
<xsl:value-of select="./ns1:Status"/>
</ns1:Status>
<ns1:EndDate>
<xsl:value-of select="./ns1:EndDate"/>
</ns1:EndDate>
</ns1:element1>
</xsl:for-each>
</ns1:exampleElement>
because from the source system will receive the user Active record in first set and terminated record in second set but we need to forward only active record to target system.Please help on this.
You were given a link to an article to Muenchian Grouping in comments, which explains the approach to take. It involves first define a key to match the elements you want to group (where
ns1
is a namespace prefix bound to the namespace URI)Then, to get the distinct ones (or rather, the first occurrence of elements for each possible value of
Id
), you would do this...And to access all the elements in the group, do this..
Try this XSLT
If you really wanted to output the XML with
ns1:
prefix (although it really shouldn't make a difference), try this instead