代码之家  ›  专栏  ›  技术社区  ›  user1619873

使用xslt删除选择性重复项

  •  0
  • user1619873  · 技术社区  · 8 年前

    我需要从xml文件中删除重复项,我想保留比先前记录更晚的记录。xslt I已输出早期记录。我想要晚一点的。你能帮帮我吗。

    <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>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   zx485 potemkin    8 年前

    您有两个错误:

    1. 你的 EmployeeInformation 元素在XML中具有不同的命名空间( http://TargetNamespace.com/EmpDetails )和XSLT( http://TargetNamespace.com/GetFileDetails ). 两者使用相同的一个
    2. 要获取最后一个匹配项,只需使用 [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>
    

    输出符合要求。