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

使用XSL代码修改XML现有标签并新增标签的实现需求

XML Transformation with XSLT

Got it, let's work through this XML transformation request. The goal is to update the <1> element's content and add a new <5> element with two children, while keeping all other existing XML structure intact. Here's a clean XSLT solution that achieves exactly that:

XSLT Code

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

    <!-- Override template to update <1> element's content -->
    <xsl:template match="1">
        <1>kkkkkkkk</1>
    </xsl:template>

    <!-- Override template to add new <5> element inside <EEE> -->
    <xsl:template match="EEE">
        <xsl:copy>
            <!-- Copy all existing children of <EEE> first -->
            <xsl:apply-templates select="@*|node()"/>
            <!-- Add the new <5> element with two child tags -->
            <5>
                <5a>new-child-content-1</5a>
                <5b>new-child-content-2</5b>
            </5>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

How It Works

  • Identity Template: This is the foundation of most XSLT transformations—it copies every node (elements, attributes, text nodes) from the input XML to the output, so we don't have to redefine rules for all the unchanged parts (like <AAA>, <BBB>, <CCC>, <DDD>, <2>, <3>, <4>).
  • <1> Element Override: The template matching <1> replaces its original content with kkkkkkkk while keeping the element itself.
  • <EEE> Element Override: We first copy all existing children of <EEE> (the updated <1>, plus <2>, <3>, <4>), then append the new <5> element with your specified child tags. You can adjust the names and content of the <5> child elements (<5a>, <5b>) to match your exact needs.

Sample Input & Output

Input XML

<AAA>
    <BBB>
        <CCC>
            <DDD>
                <EEE>
                    <1>abc</1>
                    <2>abc</2>
                    <3>abc</3>
                    <4>abc</4>
                </EEE>
            </DDD>
        </CCC>
    </BBB>
</AAA>

Output XML

<AAA>
    <BBB>
        <CCC>
            <DDD>
                <EEE>
                    <1>kkkkkkkk</1>
                    <2>abc</2>
                    <3>abc</3>
                    <4>abc</4>
                    <5>
                        <5a>new-child-content-1</5a>
                        <5b>new-child-content-2</5b>
                    </5>
                </EEE>
            </DDD>
        </CCC>
    </BBB>
</AAA>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:03:26