将一个节点的子节点复制到另一个节点,但基于匹配在另一个节点中?

时间:2019-05-06 19:56:48

标签: xml xslt xslt-2.0

我需要根据员工匹配将子节点从一个父节点复制到另一个父节点,但需要放置在单独的节点中。我几乎已经有了使用密钥的解决方案,但是我无法将子节点粘贴到新的单独节点中。

我使用键功能开发了以下代码:

<?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:output indent="yes"/>
 <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="@*"/>
           <wd:Event>
               <xsl:apply-templates 
select="$vLeaveStatus/wd:LeaveStatus/wd:LeaveDetail/."/>
           </wd:Event>
    </xsl:copy>

</xsl:template>
</xsl:stylesheet>

以下是实际输入:

<?xml version='1.0' encoding='utf-8'?>
<wd:Census_Report xmlns:wd="urn:com.workday/bsvc">
<wd:Workers>
    <wd:Worker_Data>
        <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:Worker_Data>
  </wd:Workers>
  <wd:ChangeEventSummary>       
    <wd:ChangeEvent>
        <wd:EmpID>50211</wd:EmpID>
        <wd:TermDate>2018-04-27</wd:TermDate>
        <wd:EventDetails/>
    </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>
        <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:Worker_Data>
  </wd:Workers>
  <wd:ChangeEventSummary>       
    <wd:ChangeEvent>
        <wd:EmpID>50211</wd:EmpID>
        <wd:TermDate>2018-04-27</wd:TermDate>
        <wd:EventDetails>
        <wd:Event>
           <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:Event>
        <wd:Event>
           <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:Event>
     </wd:EventDetails>
    </wd:ChangeEvent>
</wd:ChangeEventSummary>
</wd:Census_Report>

我能够将LeaveStatus节点放入Event节点,但是无法获得如何将每个节点保持在LeaveStatus下的方法。

2 个答案:

答案 0 :(得分:0)

更改您的以下代码(从第二个模板开始)

<xsl:copy>
  <xsl:apply-templates select="@*"/>
  <wd:Event>
    <xsl:apply-templates select="$vLeaveStatus/wd:LeaveStatus/wd:LeaveDetail/."/>
  </wd:Event>
</xsl:copy>

<xsl:copy>
  <xsl:apply-templates select="@*"/>
  <xsl:for-each select="$vLeaveStatus/wd:LeaveStatus/wd:LeaveDetail">
    <wd:Event>
      <wd:LeaveDetail>
        <xsl:apply-templates select="*"/>
      </wd:LeaveDetail>
    </wd:Event>
  </xsl:for-each>
</xsl:copy>

然后您将获得所需的输出。

答案 1 :(得分:0)

为什么不能简单地做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday/bsvc">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="worker" match="wd:Worker_Data" use="wd:EmpID" />

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

<xsl:template match="wd:EventDetails">
    <xsl:copy>
        <xsl:for-each select="key('worker', ../wd:EmpID)/wd:LeaveStatus/wd:LeaveDetail">
            <wd:Event>
                <xsl:copy-of select="."/>
            </wd:Event>
         </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>