我需要从xml文件中删除重复项,我想保留比早期记录更新的记录。我拥有的 xslt 输出较早的记录。我想要后者。你能帮帮我吗?
<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>
我的 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>
预期输出为
<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>
您有两个错误:
- 您的
EmployeeInformation
元素在 XML(http://TargetNamespace.com/EmpDetails
( 和 XSLT(http://TargetNamespace.com/GetFileDetails
中具有不同的命名空间。对两者使用相同的 - 要获得最后一场比赛,只需使用
[last()]
而不是[1]
.
合并这两个建议后,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>
输出是需要的。