You need to enable JavaScript to run this app. XSLT 2.0实现:为orderItem节点从10开始逐次累加10生成productCode
优惠活动
大模型
产品
解决方案
定价
更多

XSLT 2.0实现:为orderItem节点从10开始逐次累加10生成productCode

XSLT 2.0 XML转换实现

核心匹配规则

  • 基于恒等转换机制,100%保留输入XML的原有节点结构、属性、文本内容,不做无意义修改
  • 按文档顺序识别所有orderItem节点,从初始值10开始逐次步增10,为每个orderItem追加productCode子节点
  • 赋值对应关系:第一个orderItem生成productCode=10,第二个生成20,按文档顺序依次递增至第九个orderItem生成90

完整可运行代码

<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>

  <!-- orderItem节点专属处理逻辑 -->
  <xsl:template match="orderItem">
    <xsl:copy>
      <!-- 保留orderItem原有的全部内容与属性 -->
      <xsl:apply-templates select="node() | @*"/>
      <!-- 按文档顺序计算并写入productCode值 -->
      <xsl:element name="productCode">
        <xsl:value-of select="(count(preceding-sibling::orderItem) + 1) * 10"/>
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

输出效果校验

  • 输入中orderHeader根节点下取值为ABC的distributionChannelCode节点会被完整保留,无内容改动
  • 9个无嵌套内容的orderItem节点会按文档顺序,依次生成值为10、20、30……90的productCode子节点
  • 代码严格遵循XSLT 2.0语法规范,可直接在Saxon等支持XSLT 2.0的处理引擎上运行,无语法兼容问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.27 20:45:03