简体   繁体   English

使用xslt删除选择性重复项

[英]Remove selective duplicates using xslt

I need to remove duplicates from an xml file, I want to retain the later record than the earlier record. 我需要从xml文件中删除重复项,我想保留比以前的记录晚的记录。 The xslt I have outputs the earlier record. 我拥有的xslt输出较早的记录。 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 我的XSLT是

<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>

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 ). 您的EmployeeInformation元素在XML( http://TargetNamespace.com/EmpDetails )和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] . 要获得最后一场比赛,只需使用[last()]而不是[1]

Incorporating both suggestions your XSLT will look like this: 结合这两个建议,您的XSLT将如下所示:

<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. 输出是所需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM