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

XSLT 1.0如何将Header的Misc属性值仅添加到首个Package的LineCharge中

问题原因

你当前的模板匹配规则match="LineCharge[1]"作用是匹配每个父节点下的第一个LineCharge元素,因此两个Package下的首个LineCharge都会命中该模板,导致每个Package都被插入了新的LineCharge元素。

解决方法

仅需要调整匹配规则,让模板只命中第一个Package下的第一个LineCharge即可,修改后的完整XSLT代码如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"  xmlns:xe="urn:XSLTExtender"
                exclude-result-prefixes="msxsl">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />

<xsl:template match="@*|node()" >
    <xsl:copy>
      <xsl:apply-templates select="@*[normalize-space()]|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@Misc" />
  <!-- 仅修改这一行的匹配规则 -->
  <xsl:template match="Package[1]//LineCharge[1]" >
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
    <xsl:if test="normalize-space(//Invoice/Header/@Misc) != '' and number(//Invoice/Header/@Misc) = normalize-space(//Invoice/Header/@Misc)">
      <xsl:element name ="LineCharge">        
        <xsl:attribute name="Chargecd" >
          <xsl:value-of select="'CD01'"/>
        </xsl:attribute>
        <xsl:attribute name="Amount" >
          <xsl:value-of select="//Invoice/Header/@Misc"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

效果说明

修改后只有第一个Package下会插入带Chargecd="CD01"的LineCharge元素,其余Package保持原有结构,总金额计算结果为28.00+5.00=33.00,符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 15:36:03