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

XSLT执行报错:无法在包含元素子节点后创建属性节点 求解决方案

错误原因
  1. XSLT强制要求:属性节点必须在所属元素的子节点、文本内容之前创建,你原有代码先输出了PaymentMethod子元素,再尝试创建属性,直接触发语法报错。
  2. 路径取值错误:匹配CollectionDetail节点时,当前上下文就是CollectionDetail本身,取@AmountCollected不需要再加CollectionDetail/前缀,原路径无法取到对应值。
  3. 逻辑偏差:你的需求是将AmountCollected属性挂载到PaymentMethod节点上,而非保留CollectionDetail节点,原有代码给CollectionDetail加属性的逻辑不符合输出要求。
修正后可用的XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Pid" match="PaymentMethod" use="@PaymentType" />
<!-- 通用身份转换模板 -->
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
</xsl:template>
<!-- 处理CollectionDetail节点,仅输出去重后的PaymentMethod,不保留自身节点 -->
<xsl:template match="CollectionDetail">
    <xsl:apply-templates select="PaymentMethod[count(key('Pid',@PaymentType)[1]|.)=1]" />
</xsl:template>
<!-- 处理PaymentMethod节点,追加父节点的AmountCollected属性 -->
<xsl:template match="PaymentMethod">
    <xsl:copy>
        <!-- 先创建属性,再处理原有属性和子节点,符合语法规则 -->
        <xsl:attribute name="AmountCollected">
            <xsl:value-of select="number(../@AmountCollected)" />
        </xsl:attribute>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>
执行效果说明

上述代码完全兼容XSLT1.0,保留了你原有按PaymentType去重的逻辑,输出结果和你给出的期望格式完全一致:AmountCollected自动转为整数格式去掉.00后缀,PaymentMethod原有属性、子节点完全保留,CollectionDetail节点被移除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 12:27:10