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

如何使用XSLT将XML根节点下所有子节点包裹到新插入的items节点中

XSLT XML结构转换实现

你需要的XSLT转换代码如下,可直接实现将原<root>下的所有直接子节点包裹到<items>标签内的需求:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 配置输出格式,自动缩进适配目标结构 -->
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

    <!-- 身份模板:默认规则,原样复制所有节点、属性、文本内容 -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 重写root节点匹配规则,新增items包裹层 -->
    <xsl:template match="root">
        <xsl:copy>
            <items>
                <xsl:apply-templates select="*"/>
            </items>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

以上代码兼容XSLT 1.0及以上所有版本,不需要逐个指定要保留的子节点,只要是原<root>下的直接子元素都会自动归入<items>层内,后续如果原<root>下新增其他节点也不需要修改转换规则。

内容的提问来源于stack exchange,提问作者Shawn de Wet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 15:57:03