如何根据员工匹配从父节点复制子节点并将其保留在另一个父节点中?

时间:2019-05-03 20:39:24

标签: xml xslt

我是XSLT的新手,并且需要根据员工ID匹配复制子节点并将其放置在另一个节点中吗?

两个节点都具有EmpID,需要复制。

Workers_Data-> LeaveStatus节点(仅当EmpID匹配时)wd:ChangeEventSummary-> wd:ChangeEvent-> wd:EventDetails并保持整个报告不变。

<?xml version='1.0' encoding='utf-8'?>
<wd:Census_Report xmlns:wd="urn:com.workday/bsvc">
<wd:Workers>
    <wd:Worker_Data xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <wd:EmpID>50211</wd:EmpID>
        <wd:LeaveStatus> <!--This entire node need to copy to 
EventDetails-->
            <wd:LeaveDetail>
                <wd:LOA_Start_Date>2017-12-22</wd:LOA_Start_Date>
                <wd:LOA_End_Date>2018-01-22</wd:LOA_End_Date>
            </wd:LeaveDetail>
            <wd:LeaveDetail>
                <wd:LOA_Start_Date>2018-02-20</wd:LOA_Start_Date>
                <wd:LOA_End_Date>2018-03-02</wd:LOA_End_Date>
            </wd:LeaveDetail>
        </wd:LeaveStatus>
        <wd:Allocation_Details>
            <wd:AllocationInstance>
                <wd:Costing_ID/>
                <wd:Start_Date/>
                <wd:End_Date/>
                <wd:Costing_Allocation_Data>
                    <wd:Allocation_Order/>
                    <wd:Cost_Center_Allocation/>
                    <wd:Region_Allocation/>
                    <wd:Location_Allocation/>
                    <wd:Distribution_Percentage/>
                    <wd:Default_from_Organization_Assignment/>
                </wd:Costing_Allocation_Data>
            </wd:AllocationInstance>
        </wd:Allocation_Details>
    </wd:Worker_Data>
  </wd:Workers>
 <wd:ChangeEventSummary>       
    <wd:ChangeEvent xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <wd:EmpID>50211</wd:EmpID>
        <wd:TermDate>2018-04-27</wd:TermDate>
        <wd:EventDetails/><!--Paste the Leave Status inside this node -->
    </wd:ChangeEvent>
</wd:ChangeEventSummary>
</wd:Census_Report>

预期输出如下:

 <?xml version='1.0' encoding='utf-8'?>
 <wd:Census_Report xmlns:wd="urn:com.workday/bsvc">
 <wd:Workers>
    <wd:Worker_Data xmlns:xsd="http://www.w3.org/2001/XMLSchema">

        <wd:EmpID>50211</wd:EmpID>
        <wd:LeaveStatus>
            <wd:LeaveDetail>
                <wd:LOA_Start_Date>2017-12-22</wd:LOA_Start_Date>
                <wd:LOA_End_Date>2018-01-22</wd:LOA_End_Date>
            </wd:LeaveDetail>
            <wd:LeaveDetail>
                <wd:LOA_Start_Date>2018-02-20</wd:LOA_Start_Date>
                <wd:LOA_End_Date>2018-03-02</wd:LOA_End_Date>
            </wd:LeaveDetail>
        </wd:LeaveStatus>
        <wd:Allocation_Details 
 xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
            <wd:AllocationInstance>
                <wd:Costing_ID/>
                <wd:Start_Date/>
                <wd:End_Date/>
                <wd:Costing_Allocation_Data>
                    <wd:Allocation_Order/>
                    <wd:Cost_Center_Allocation/>
                    <wd:Region_Allocation/>
                    <wd:Location_Allocation/>
                    <wd:Distribution_Percentage/>
                    <wd:Default_from_Organization_Assignment/>
                </wd:Costing_Allocation_Data>
            </wd:AllocationInstance>
        </wd:Allocation_Details>
    </wd:Worker_Data>
</wd:Workers>
<wd:ChangeEventSummary>       
    <wd:ChangeEvent xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <wd:EmpID>50211</wd:EmpID>
        <wd:TermDate>2018-04-27</wd:TermDate>
        <wd:EventDetails> <!--Copied inside this node based on EmpID match-->
            <wd:LeaveStatus>
                <wd:LeaveDetail>
                    <wd:LOA_Start_Date>2017-12-22</wd:LOA_Start_Date>
                    <wd:LOA_End_Date>2018-01-22</wd:LOA_End_Date>
                </wd:LeaveDetail>
                <wd:LeaveDetail>
                    <wd:LOA_Start_Date>2018-02-20</wd:LOA_Start_Date>
                    <wd:LOA_End_Date>2018-03-02</wd:LOA_End_Date>
                </wd:LeaveDetail>
            </wd:LeaveStatus>
        </wd:EventDetails>
    </wd:ChangeEvent>
 </wd:ChangeEventSummary>
 </wd:Census_Report>

1 个答案:

答案 0 :(得分:0)

Here is the code I used to get the values:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wd="urn:com.workday/bsvc"
    exclude-result-prefixes="xs xsd"
    version="2.0">
    <xsl:key name="kEmpID" match="wd:Worker_Data" 
    use="concat(ancestor::wd:LeaveStatus/wd:LeaveDetail,wd:EmpID)"/>

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

    <xsl:template match="wd:ChangeEvent/wd:EventDetails">
    <xsl:variable name="vLeaveStatus" select="key('kEmpID',../wd:EmpID)"/>
    <xsl:copy>

        <xsl:apply-templates select="@*"/>
            <xsl:for-each select="key('kEmpID',../wd:EmpID)">
                <wd:Event>
                    <xsl:apply-templates 
    select="$vLeaveStatus/wd:LeaveStatus/*"/>
                </wd:Event>

        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>