根据日期生成单独的元素

时间:2017-07-19 13:47:47

标签: xml xslt

有人可以帮助我如何制作此代码

<pi:Employee>
<pi:Summary>
    <pi:Employee_ID>100000</pi:Employee_ID>
    <pi:Name>Test Name</pi:Name>
    <pi:Payroll_Company_ID>101</pi:Payroll_Company_ID>
    <pi:Payroll_Company_Name>Test Payroll Company</pi:Payroll_Company_Name>
    <pi:Pay_Group_ID>01</pi:Pay_Group_ID>
    <pi:Pay_Group_Name>Regular</pi:Pay_Group_Name>
</pi:Summary>
<pi:Personal />
<pi:Leave_of_Absence>
    <pi:Operation>ADD</pi:Operation>
    <pi:On_Leave>1</pi:On_Leave>
    <pi:Leave_Start_Date>2017-07-19</pi:Leave_Start_Date>
    <pi:Estimated_Leave_End_Date>2017-07-21</pi:Estimated_Leave_End_Date>
    <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
    <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
</pi:Leave_of_Absence>

变得像这样

<pi:Employee>
<pi:Summary>
    <pi:Employee_ID>100000</pi:Employee_ID>
    <pi:Name>Test Name</pi:Name>
    <pi:Payroll_Company_ID>101</pi:Payroll_Company_ID>
    <pi:Payroll_Company_Name>Test Payroll Company</pi:Payroll_Company_Name>
    <pi:Pay_Group_ID>01</pi:Pay_Group_ID>
    <pi:Pay_Group_Name>Regular</pi:Pay_Group_Name>
</pi:Summary>
<pi:Personal />
<pi:Leave_of_Absence>
    <pi:Operation>ADD</pi:Operation>
    <pi:On_Leave>1</pi:On_Leave>
    <pi:Leave_Start_Date>2017-07-19</pi:Leave_Start_Date>
    <pi:Estimated_Leave_End_Date>2017-07-19</pi:Estimated_Leave_End_Date>
    <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
    <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
</pi:Leave_of_Absence>
<pi:Leave_of_Absence>
    <pi:Operation>ADD</pi:Operation>
    <pi:On_Leave>1</pi:On_Leave>
    <pi:Leave_Start_Date>2017-07-20</pi:Leave_Start_Date>
    <pi:Estimated_Leave_End_Date>2017-07-20</pi:Estimated_Leave_End_Date>
    <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
    <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
</pi:Leave_of_Absence>
<pi:Leave_of_Absence>
    <pi:Operation>ADD</pi:Operation>
    <pi:On_Leave>1</pi:On_Leave>
    <pi:Leave_Start_Date>2017-07-21</pi:Leave_Start_Date>
    <pi:Estimated_Leave_End_Date>2017-07-21</pi:Estimated_Leave_End_Date>
    <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
    <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
</pi:Leave_of_Absence>

正如您所注意到的,从Leave_of_Absence的1个根开始,它变为3.这必须取决于员工休假的天数。例如,Leave_Start_Date是从2017-07-19到2017-07-21,这使得3天所以必须还有3个Leave_of_Absence根。但Leave_Start_Date和Estimated_Leave_End_Date的元素必须反映包含天数。 有没有办法做到这一点?有人能帮我吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

在XSLT 2.0中,您可以使用xs:date数据类型和日期算法以及持续时间:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    xpath-default-namespace="http://example.com/pi"
    version="2.0">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()" mode="#all">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" mode="#current"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Leave_of_Absence">
        <xsl:variable name="this" select="."/>
        <xsl:for-each select="0 to days-from-duration(xs:date(Estimated_Leave_End_Date) - xs:date(Leave_Start_Date))">
            <xsl:apply-templates select="$this" mode="day">
                <xsl:with-param name="date" tunnel="yes" select="xs:date($this/Leave_Start_Date) + xs:dayTimeDuration('P1D') * ."/>
            </xsl:apply-templates>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="Leave_Start_Date | Estimated_Leave_End_Date" mode="day">
        <xsl:param name="date" tunnel="yes"/>
        <xsl:copy>
            <xsl:value-of select="$date"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

变换

<?xml version="1.0" encoding="UTF-8"?>
<pi:Root xmlns:pi="http://example.com/pi">
    <pi:Employee>
        <pi:Summary>
            <pi:Employee_ID>100000</pi:Employee_ID>
            <pi:Name>Test Name</pi:Name>
            <pi:Payroll_Company_ID>101</pi:Payroll_Company_ID>
            <pi:Payroll_Company_Name>Test Payroll Company</pi:Payroll_Company_Name>
            <pi:Pay_Group_ID>01</pi:Pay_Group_ID>
            <pi:Pay_Group_Name>Regular</pi:Pay_Group_Name>
        </pi:Summary>
        <pi:Personal />
        <pi:Leave_of_Absence>
            <pi:Operation>ADD</pi:Operation>
            <pi:On_Leave>1</pi:On_Leave>
            <pi:Leave_Start_Date>2017-07-19</pi:Leave_Start_Date>
            <pi:Estimated_Leave_End_Date>2017-07-21</pi:Estimated_Leave_End_Date>
            <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
            <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
        </pi:Leave_of_Absence>
    </pi:Employee>
</pi:Root>

进入

<?xml version="1.0" encoding="UTF-8"?>
<pi:Root xmlns:pi="http://example.com/pi">
   <pi:Employee>
      <pi:Summary>
         <pi:Employee_ID>100000</pi:Employee_ID>
         <pi:Name>Test Name</pi:Name>
         <pi:Payroll_Company_ID>101</pi:Payroll_Company_ID>
         <pi:Payroll_Company_Name>Test Payroll Company</pi:Payroll_Company_Name>
         <pi:Pay_Group_ID>01</pi:Pay_Group_ID>
         <pi:Pay_Group_Name>Regular</pi:Pay_Group_Name>
      </pi:Summary>
      <pi:Personal/>
      <pi:Leave_of_Absence>
         <pi:Operation>ADD</pi:Operation>
         <pi:On_Leave>1</pi:On_Leave>
         <pi:Leave_Start_Date>2017-07-19</pi:Leave_Start_Date>
         <pi:Estimated_Leave_End_Date>2017-07-19</pi:Estimated_Leave_End_Date>
         <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
         <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
      </pi:Leave_of_Absence>
      <pi:Leave_of_Absence>
         <pi:Operation>ADD</pi:Operation>
         <pi:On_Leave>1</pi:On_Leave>
         <pi:Leave_Start_Date>2017-07-20</pi:Leave_Start_Date>
         <pi:Estimated_Leave_End_Date>2017-07-20</pi:Estimated_Leave_End_Date>
         <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
         <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
      </pi:Leave_of_Absence>
      <pi:Leave_of_Absence>
         <pi:Operation>ADD</pi:Operation>
         <pi:On_Leave>1</pi:On_Leave>
         <pi:Leave_Start_Date>2017-07-21</pi:Leave_Start_Date>
         <pi:Estimated_Leave_End_Date>2017-07-21</pi:Estimated_Leave_End_Date>
         <pi:Leave_Last_Day_of_Work>2017-07-18</pi:Leave_Last_Day_of_Work>
         <pi:Leave_Reason>LEAVE_TYPE_REASON-6-19</pi:Leave_Reason>
      </pi:Leave_of_Absence>
   </pi:Employee>
</pi:Root>