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

如何通过XSL实现XML行列标签的多操作生成指定结构?

XSLT实现:按行生成指定标签并补充缺失列

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<importoTotaleCommissioni>
    <row>
        <column>5</column>
        <column>6</column>
    </row>
    <row>
        <column>5</column>
        <column>6</column>
        <column>7</column>
        <column>8</column>
    </row>
</importoTotaleCommissioni>

期望输出XML

<importoTotaleCommissioni>
    <row>
        <fascia>custom text for row 1</fascia>
        <column_1>5</column_1>
        <column_2>6</column_2>
        <column_3>undefined</column_3>
        <column_4>undefined</column_4>
        <column_5>undefined</column_5>
        <column_6>undefined</column_6>
    </row>
    <row>
        <fascia>another custom text for row 2</fascia>
        <column_1>5</column_1>
        <column_2>6</column_2>
        <column_3>7</column_3>
        <column_4>8</column_4>
        <column_5>undefined</column_5>
        <column_6>undefined</column_6>
    </row>
</importoTotaleCommissioni>

需求说明

  • 行数不固定,需兼容任意数量的<row>节点
  • 每行最多生成6个带编号的列标签(column_1至column_6)
  • 若原行中<column>数量不足6个,补充对应标签并填充值为undefined
  • 每行开头需插入自定义的<fascia>标签,内容按行位置指定

解决方案(XSLT 1.0/2.0兼容)

<?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" encoding="UTF-8"/>
    
    <!-- 自定义fascia文本映射,可按需添加更多行的文本 -->
    <xsl:variable name="fascia-texts">
        <text row="1">custom text for row 1</text>
        <text row="2">another custom text for row 2</text>
    </xsl:variable>
    <xsl:key name="fascia-key" match="text" use="@row"/>

    <xsl:template match="/importoTotaleCommissioni">
        <xsl:copy>
            <xsl:apply-templates select="row"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="row">
        <xsl:copy>
            <!-- 插入当前行对应的fascia标签 -->
            <fascia>
                <xsl:value-of select="key('fascia-key', position(), $fascia-texts)"/>
            </fascia>
            
            <!-- 循环生成6个带编号的列 -->
            <xsl:for-each select="1 to 6">
                <xsl:element name="column_{.}">
                    <!-- 匹配对应位置的column值,无则填充undefined -->
                    <xsl:choose>
                        <xsl:when test="../column[position()=current()]">
                            <xsl:value-of select="../column[position()=current()]"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>undefined</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:element>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

关键逻辑说明

  1. 自定义文本管理:通过变量fascia-texts存储每行的自定义文本,结合key实现按行位置快速匹配,后续新增行只需添加对应<text>节点即可。
  2. 固定列数生成:通过1 to 6循环强制生成6个带编号的列标签,不受原行<column>数量影响。
  3. 缺失值填充:使用choose判断当前循环位置是否存在对应<column>节点,不存在则输出undefined。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 05:03:36