需要帮助 - 使用XSLT根据条件添加/删除XML中的节点

时间:2017-10-26 04:40:35

标签: xml xslt

我必须使用XSLT转换以下两个XML(输入XML#1和输入XML#2)。我需要你的帮助才能使用XSLT对它们进行转换,因为我正在寻找解决方案。

提前感谢您的帮助。

输入XML#1

<Employee>
<Summary>
    <Employee_ID>12345</Employee_ID>
    <Name>Mel Gibson</Name>
</Summary>
<Personal>
    <First_Name>Mel</First_Name>
    <Last_Name>Gibson</Last_Name>
</Personal>
<Status>
    <Active>Yes</Active>
    <Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
    <Location>California</Location>
    <Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>

转换以上XML的条件是

如果XML#1中存在Last_Name节点,则该节点应在转换后保留Last_Formatted_Name的值并删除 XML#1中的Last_Formatted_Name节点。

输出应如下所示。请注意,Last_Name保持Last_Formatted_Name的值,并删除Last_Formatted_Name。

<Employee>
<Summary>
    <Employee_ID>12345</Employee_ID>
    <Name>Mel Gibson</Name>
</Summary>
<Personal>
    <First_Name>Mel</First_Name>
    <Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
    <Active>Yes</Active>
    <Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
    <Location>California</Location>
</Summary_Information>
</Employee>

输入XML#2

<Employee>
<Summary>
    <Employee_ID>12345</Employee_ID>
    <Name>Mel Gibson</Name>
</Summary>
<Personal>
    <First_Name>Mel</First_Name>
</Personal>
<Status>
    <Active>Yes</Active>
    <Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
    <Location>California</Location>
    <Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
</Summary_Information>
</Employee>

转换XML#2的条件是:如果输入XML中不存在Last_Name,则应在First_Name节点之后创建一个Last_Name节点,并且应该保留Last_Formatted_Name的值

输出应如下所示。请注意,在删除First_Name和Last_Formatted_Name节点后立即创建Last_Name节点。

<Employee>
<Summary>
    <Employee_ID>12345</Employee_ID>
    <Name>Mel Gibson</Name>
</Summary>
<Personal>
    <First_Name>Mel</First_Name>
    <Last_Name>Samuel Gibson</Last_Name>
</Personal>
<Status>
    <Active>Yes</Active>
    <Base_Location>Boston</Base_Location>
</Status>
<Summary_Information>
    <Location>California</Location>
</Summary_Information>
</Employee>

遵循XSLT是我为实现这一点而编写的,但不会产生我想要的输出。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()" mode="#default">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Personal">

    <xsl:if test="exists(Last_Name)">
    <xsl:copy>
            <xsl:apply-templates/>
        <Last_Name>
            <xsl:value-of select="//Last_Formatted_Name"/>
        </Last_Name>
        </xsl:copy>
    </xsl:if>
</xsl:template>

<xsl:template match="Personal">
<xsl:if test="not(Last_Name)">
    <xsl:copy>
            <xsl:apply-templates/>
        <Last_Name>
            <xsl:value-of select="//Last_Formatted_Name"/>
        </Last_Name>
        </xsl:copy>
    </xsl:if>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

请尝试以下XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

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

    <!-- modifies the value of <Last_Name> if it exists -->
    <xsl:template match="Last_Name">
        <xsl:copy>
            <xsl:value-of select="../../Summary_Information/Last_Formatted_Name" />
        </xsl:copy>
    </xsl:template>

    <!-- if <Last_Name> does not exist, creates a last name with the appropriate value -->
    <xsl:template match="Personal[not(Last_Name)]">
        <xsl:copy>
            <xsl:apply-templates />
            <Last_Name>
                <xsl:value-of select="../Summary_Information/Last_Formatted_Name" />
            </Last_Name>
        </xsl:copy>
    </xsl:template>

    <!-- removes the node -->
    <xsl:template match="Last_Formatted_Name" />
</xsl:stylesheet>
相关问题