如何使用 XSLT 在特定标签后插入 XML 标签? [英] How to Insert an XML tag after a particular tag using XSLT?

查看:36
本文介绍了如何使用 XSLT 在特定标签后插入 XML 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 - 我会帮助您使用 XSLT 转换以下输入 XML

Hi – I would your help to transform following input XML using XSLT

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
    <Summary>
        <Employee_ID>12345</Employee_ID>
        <Name>Mel Gibson</Name>
    </Summary>
    <Personal>
        <First_Name>Mel</First_Name>
        <Middle_Name>Samuel</Middle_Name>
        <Gender>Male</Gender>
    </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 的要求是:

Requirements for transforming above XML is:

如果输入的 XML 中不存在 Last_Name,则应在 First_Name 节点之后创建一个 Last_Name 节点,并保存 Last_Formatted_Name 的值

If Last_Name doesn’t exist in input XML, a node Last_Name should be created right after First_Name node and should hold the value of Last_Formatted_Name

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

Output should look like below. Please note Last_Name node is created right after First_Name and Last_Formatted_Name node is removed.

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
    <Summary>
        <Employee_ID>12345</Employee_ID>
        <Name>Mel Gibson</Name>
    </Summary>
    <Personal>
        <First_Name>Mel</First_Name>
        <Last_Name>Samuel Gibson</Last_Name>
        <Middle_Name>Samuel</Middle_Name>
        <Gender>Male</Gender>
    </Personal>
    <Status>
        <Active>Yes</Active>
        <Base_Location>Boston</Base_Location>
    </Status>
    <Summary_Information>
        <Location>California</Location>
    </Summary_Information>
</Employee>

我写了以下 XSLT(在另一篇 stackoverflow 帖子的帮助下).但是,Last_Name 被插入为Personal"下的最后一个标签,而Last_Name"应插入在First_Name"标签之后.

I have written following XSLT (with the help of another stackoverflow post). However, Last_Name is being inserted as last tag under ‘Personal’ where as ‘Last_Name’ should be inserted right after ‘First_Name’ tag.

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

使用以上 XSLT,我得到以下结果.我应该如何更改我的 XSLT 以在 First_Name 标记之后插入 Last_Name.

With above XSLT, I’m getting following result. How should I change my XSLT to insert Last_Name just after First_Name tag.

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
    <Summary>
        <Employee_ID>12345</Employee_ID>
        <Name>Mel Gibson</Name>
    </Summary>
    <Personal>
        <First_Name>Mel</First_Name>
        <Middle_Name>Samuel</Middle_Name>
        <Gender>Male</Gender>
        <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>

推荐答案

您可以将模板应用到 First_Name,创建新的 Last_Name,然后应用模板到其他一切.

You can apply-templates to First_Name, create the new Last_Name, and then apply-templates to everything else.

示例...

<xsl:template match="Personal[not(Last_Name)]">
  <xsl:copy>
    <xsl:apply-templates select="First_Name"/>
    <Last_Name>
      <xsl:value-of select="../Summary_Information/Last_Formatted_Name" />
    </Last_Name>
    <xsl:apply-templates select="*[not(self::First_Name)]"/>
  </xsl:copy>
</xsl:template>

这篇关于如何使用 XSLT 在特定标签后插入 XML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆