xslt 2.0删除重复项

时间:2017-05-01 16:46:59

标签: xml xslt-2.0

我有一个XML文件,其中包含重复的employeeNumbers节点。我只需要第一张唱片。如何删除具有相同employeeNumber的第二条记录?非常感谢任何帮助。

示例XML文件

<?xml version='1.0' encoding='UTF-8'?>
<cd:Report_Data xmlns:cd="urn:com.comday.report/INT007BOutbound">
   <cd:Report_Entry>
      <cd:displayName>Sarah Smith</cd:displayName>
      <cd:employeeNumber>97061</cd:employeeNumber>
      <cd:employeeType>CL</cd:employeeType>
      <cd:CF_-_Current_employeeType>CL</cd:CF_-_Current_employeeType>
      <cd:ont-startdate>20131018</cd:ont-startdate>
      <cd:ont-enddate>20160301</cd:ont-enddate>
   </cd:Report_Entry>
   <cd:Report_Entry>
      <cd:displayName>Danny Jones</cd:displayName>
      <cd:employeeNumber>97062</cd:employeeNumber>
      <cd:employeeType>EMP</cd:employeeType>
      <cd:CF_-_Current_employeeType>EMP</cd:CF_-_Current_employeeType>
      <cd:ont-startdate>20170501</cd:ont-startdate>
      <cd:ont-enddate>20670501</cd:ont-enddate>
   </cd:Report_Entry>
   <cd:Report_Entry>
      <cd:displayName>Danny Jones</cd:displayName>
      <cd:employeeNumber>97062</cd:employeeNumber>
      <cd:employeeType>CL</cd:employeeType>
      <cd:CF_-_Current_employeeType>CL</cd:CF_-_Current_employeeType>
      <cd:ont-startdate>20131112</cd:ont-startdate>
      <cd:ont-enddate>20140620</cd:ont-enddate>
   </cd:Report_Entry>
   <cd:Report_Entry>
      <cd:displayName>David Jenkins</cd:displayName>
      <cd:employeeNumber>97063</cd:employeeNumber>
      <cd:employeeType>EMP</cd:employeeType>
      <cd:CF_-_Current_employeeType>EMP</cd:CF_-_Current_employeeType>
      <cd:ont-startdate>20131118</cd:ont-startdate>
      <cd:ont-enddate>20631118</cd:ont-enddate>
   </cd:Report_Entry>
</cd:Report_Data>

示例XSL文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="no" method="text"/>
    <xsl:template match="cd:Report_Data" xmlns:cd="urn:com.comday.report/INT007BOutbound">
        <!--These are just text column headers for output-->
        <xsl:text>displayName,employeeNumber,employeeType</xsl:text>
<xsl:call-template name="insertNewLine"/>
        <xsl:for-each select="cd:Report_Entry">
            <xsl:variable name="date">
                <xsl:value-of select="format-date(current-date(),'[Y0001][M01][D01]')"/>    
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="$date &lt; cd:ont-startdate or cd:ont-enddate &lt; $date">
                    <xsl:value-of select="replace(cd:displayName,',',' ')"/>
                    <xsl:call-template name="insertDelimiter"/>
                    <xsl:value-of select="cd:employeeNumber"/>
                    <xsl:call-template name="insertDelimiter"/>
                    <xsl:value-of select="cd:employeeType"/>
                </xsl:when>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="cd:ont-startdate &lt;= $date and $date &lt;= cd:ont-enddate">
                    <xsl:value-of select="replace(cd:displayName,',',' ')"/>
                    <xsl:call-template name="insertDelimiter"/>
                    <xsl:value-of select="cd:employeeNumber"/>
                    <xsl:call-template name="insertDelimiter"/>
                    <xsl:value-of select="cd:CF_-_Current_employeeType"/>
                </xsl:when>
            </xsl:choose>
            <xsl:call-template name="insertNewLine"/>
        </xsl:for-each>
    </xsl:template>
<!--****************************************-->
<!-- Starting function library section here -->
    <xsl:template name="insertDelimiter">
        <xsl:text>,</xsl:text>
    </xsl:template>
<!--****************************************-->
    <xsl:template name="insertNewLine">
<!-- Used to insert a new line into the XML output -->
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

public static final Object DAY_OF_MONTH=org.joda.time....更改为<xsl:for-each select="cd:Report_Entry">(当然还有结束标记<xsl:for-each-group select="cd:Report_Entry" group-by="cd:employeeNumber">,然后更改为</xsl:for-each>)。