本文介绍了将一个节点的子节点复制到另一个节点,但在基于匹配的单独节点中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据员工匹配将子节点从一个父节点复制到另一个父节点,但需要放置在单独的节点中.我几乎已经使用密钥获得了解决方案,但我无法将子节点粘贴到新的单独节点中.
I have a requirement to copy the child node from one parent to another based on a employee match but needs to place in a separate node. I have almost got the solution using key, but I am not able to paste the child node in a new separate node.
我使用关键函数开发了以下代码:
I developed the below code using key function:
<?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>
下面的输出是预期的输出:
The below output is the expected output:
<?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
下.
I am able to get the LeaveStatus
node into Event
node, but not getting how I can keep each node under LeaveStatus
.
推荐答案
为什么你不能简单地做:
Why can't you do simply:
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>
这篇关于将一个节点的子节点复制到另一个节点,但在基于匹配的单独节点中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!