0

I need to remove duplicates from an xml file, I want to retain the later record than the earlier record. The xslt I have outputs the earlier record. I wanted the later one. Can you please help me.

<FileRead xmlns="http://TargetNamespace.com/EmpDetails">
   <EmployeeInformation>
      <Empl_ID>63496</Empl_ID>
      <First_Name>ALEXIS</First_Name>
      <Last_Name>TORRES</Last_Name>
      <Record_Updated_Date>7/19/2017</Record_Updated_Date>
   </EmployeeInformation>
   <EmployeeInformation>
      <Empl_ID>63497</Empl_ID>
      <First_Name>JOHN</First_Name>
      <Last_Name>DOE</Last_Name>
      <Record_Updated_Date>8/19/2017</Record_Updated_Date>
   </EmployeeInformation>
   <EmployeeInformation>
      <Empl_ID>63496</Empl_ID>
      <First_Name>ALEXIS</First_Name>
      <Last_Name>TORRES</Last_Name>
      <Record_Updated_Date>8/19/2017</Record_Updated_Date>
   </EmployeeInformation>
   <EmployeeInformation>
      <Empl_ID>63498</Empl_ID>
      <First_Name>BILL</First_Name>
      <Last_Name>SMITH</Last_Name>
      <Record_Updated_Date>7/19/2017</Record_Updated_Date>
   </EmployeeInformation>   
</FileRead>

My XSLT is

<xsl:stylesheet version="1.0" xmlns:ns0="http://TargetNamespace.com/EmpDetails" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="employees" match="ns0:EmployeeInformation" use="ns0:Empl_ID"/>
  <xsl:template match="/*">
    <ns0:FileRead>
      <xsl:copy-of select="*[generate-id() = generate-id(key('employees', ns0:Empl_ID)[1])]"/>
    </ns0:FileRead>
  </xsl:template>
</xsl:stylesheet>

Expected Output is

<FileRead xmlns="http://TargetNamespace.com/EmpDetails">
   <EmployeeInformation>
      <Empl_ID>63497</Empl_ID>
      <First_Name>JOHN</First_Name>
      <Last_Name>DOE</Last_Name>
      <Record_Updated_Date>8/19/2017</Record_Updated_Date>
   </EmployeeInformation>
   <EmployeeInformation>
      <Empl_ID>63496</Empl_ID>
      <First_Name>ALEXIS</First_Name>
      <Last_Name>TORRES</Last_Name>
      <Record_Updated_Date>8/19/2017</Record_Updated_Date>
   </EmployeeInformation>
   <EmployeeInformation>
      <Empl_ID>63498</Empl_ID>
      <First_Name>BILL</First_Name>
      <Last_Name>SMITH</Last_Name>
      <Record_Updated_Date>7/19/2017</Record_Updated_Date>
   </EmployeeInformation>   
</FileRead>

1 Answers1

1

You have two errors:

  1. Your EmployeeInformation element has a different namespace in the XML(http://TargetNamespace.com/EmpDetails) and XSLT(http://TargetNamespace.com/GetFileDetails). Use the same one for both
  2. To get the last of the matches, simply use [last()] instead of [1].

Incorporating both suggestions your XSLT will look like this:

<xsl:stylesheet version="1.0" 
  xmlns:ns0="http://TargetNamespace.com/EmpDetails" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="employees" match="ns0:EmployeeInformation" use="ns0:Empl_ID"/>

  <xsl:template match="/*">
    <ns0:FileRead>
      <xsl:copy-of select="*[generate-id() = generate-id(key('employees', ns0:Empl_ID)[last()])]"/>
    </ns0:FileRead>
  </xsl:template>

</xsl:stylesheet>

The output is as desired.

zx485
  • 28,498
  • 28
  • 50
  • 59