You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT复制子节点并添加额外节点?附XML示例

Solution: Complete XSLT to Copy Subnodes and Add Extra Nodes

Here's how to finish your XSLT to achieve the desired result. The key is using the identity transform to preserve all existing content, then adding a targeted template for <employee> elements where we insert the extra node.

Complete XSLT Code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- Identity transform: copies all nodes and attributes exactly as they are -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Custom template for <employee> elements: copy existing content + add extra node -->
    <xsl:template match="employee">
        <xsl:copy>
            <!-- Copy all original child nodes (Name, Dept, Designation) -->
            <xsl:apply-templates select="@* | node()"/>
            <!-- Add your custom extra node here; adjust name and value as needed -->
            <Status>Active</Status>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

How It Works

  • Identity Transform: This template acts as a default rule, copying every node and attribute in your input XML. It ensures all existing structure is preserved unless we explicitly override it with another template.
  • Employee Template: This template targets only <employee> elements. We first copy the <employee> wrapper, then apply the identity transform to its existing children (so Name, Dept, Designation stay intact). Finally, we insert the new <Status> node—you can rename this or add multiple nodes by including more elements here.

Resulting XML

When applied to your input XML, the output will look like this:

<root>
    <employees>
        <employee>
            <Name>ABC</Name>
            <Dept>CS</Dept>
            <Designation>sse</Designation>
            <Status>Active</Status>
        </employee>
    </employees>
</root>

内容的提问来源于stack exchange,提问作者pshemek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:14:17