首页 > 解决方案 > 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>

标签: xsltxslt-2.0

解决方案


或者很快:

<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>

推荐阅读