xslt - XSL 在最后一个元素出现时将一个级别元素移动到另一个元素中
问题描述
给定输入 XML 数据:
<Report_Entry>
<Time_Off_Type_Group>
<Time_Off_Type Descriptor="Sickness Full" />
</Time_Off_Type_Group>
<Time_Off_Type_Group>
<Time_Off_Type Descriptor="Sickness Part" />
</Time_Off_Type_Group>
<Time_Off_Entry_ID>2d90199913fa9fae8</Time_Off_Entry_ID>
<Request_or_Correction>Time Off Request</Request_or_Correction>
</Report_Entry>
因此,我希望通过以下条件输出数据:“对于每个 Time_Off_Type_Group 将 Time_Off_Entry_ID 和 Request_or_Correction 移入 Time_Off_Type_Group”
输出示例:
<Report_Entry>
<Time_Off_Type_Group>
<Time_Off_Type Descriptor="Sickness Full" />
<Time_Off_Entry_ID>2d90199913fa9fae8</Time_Off_Entry_ID>
<Request_or_Correction>Time Off Request</Request_or_Correction>
</Time_Off_Type_Group>
<Time_Off_Type_Group>
<Time_Off_Type Descriptor="Sickness Part" />
<Time_Off_Entry_ID>2d90199913fa9fae8</Time_Off_Entry_ID>
<Request_or_Correction>Time Off Request</Request_or_Correction>
</Time_Off_Type_Group>
</Report_Entry>
解决方案
或者很快:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Report_Entry">
<xsl:variable name="common" select="Time_Off_Entry_ID | Request_or_Correction" />
<xsl:copy>
<xsl:for-each select="Time_Off_Type_Group">
<xsl:copy>
<xsl:copy-of select="* | $common"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- python - Python tkinter - 为什么当我更改选项卡时输入框会保留?
- c# - 使用 libgit2sharp 做“git log”
- java - 从java执行批处理文件时行为不一致
- testing - 使用 Testcafe 选择器:withText 然后兄弟
- jax-rs - 为什么这个 JAX-RS 404 响应得到一个空的正文?
- spotfire - 使用 Spotfire 中列中的数据在交叉表中创建列名?
- html - Office UI Fabric 的布局问题
- r - R编程:如何根据给定的纬度和经度将二维数组重塑为3维数组?
- linux - 为什么不同 Linux 操作系统但在相同硬件上的上下文切换时间不同?
- swift - 来自 notification.userInfo 的数据