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

使用XSLT将XML嵌套Property元素转换为逗号分隔串或独立属性节点

以下提供两种符合需求的XSLT实现方案,均基于XSLT 2.0标准,主流XML解析器均已兼容。

方案一:属性拼接为逗号分隔字符串

实现将所有属性拼接为单个逗号分隔字符串的扁平化输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 身份转换模板:原样复制所有未匹配的节点和属性 -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 重写Properties节点的处理逻辑 -->
    <xsl:template match="Properties">
        <xsl:copy>
            <Property>
                <xsl:value-of select="Property/concat(key, ':', value)" separator=","/>
            </Property>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

方案二:属性key作为节点名(更优)

实现将属性key直接转为节点名、value作为节点文本的结构化输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 身份转换模板:原样复制所有未匹配的节点和属性 -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 重写Property节点的处理逻辑,动态生成以key值为名的节点 -->
    <xsl:template match="Properties/Property">
        <xsl:element name="{key}">
            <xsl:value-of select="value"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

如果需要兼容XSLT 1.0,方案一的拼接逻辑需要替换为递归遍历Property节点拼接字符串的实现,方案二的动态节点生成逻辑无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 10:15:03