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

XSLT添加条件后编译错误:需实现基于G_HL节点数量的执行逻辑

XSLT编译错误修复与需求实现

问题背景

原本可正常运行的XSLT代码,添加<xsl:if>条件判断后出现编译错误。需求为:仅当源XML中G_HL[S_HL/D_735='T']节点的数量大于1时,才执行对应节点复制逻辑;若该节点仅出现一次,则跳过该逻辑。

源XML

<?xml version="1.0" encoding="UTF-8"?>
<M_856>
    <S_ST>
        <D_143>856</D_143>
        <D_329>0001</D_329>
    </S_ST>
    <S_BSN>
        <D_353>00</D_353>
        <D_396>0080000055</D_396>
        <D_373>20240123</D_373>
        <D_337>040849</D_337>
        <D_1005>0001</D_1005>
    </S_BSN>
    <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>S</D_735>
        </S_HL>
        <S_TD5>
            <D_66>2</D_66>
            <D_67>0BP070A</D_67>
            <D_91>M</D_91>
        </S_TD5>
    </G_HL>
    <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>O</D_735>
        </S_HL>
    </G_HL>
        <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>I</D_735>
        </S_HL>
        <S_LIN>
            <D_350>000010</D_350>
            <D_235>BP</D_235>
        </S_LIN>
    </G_HL>
    <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>I</D_735>
        </S_HL>
        <S_LIN>
            <D_350>000020</D_350>
            <D_235>BP</D_235>
        </S_LIN>
    </G_HL>
    <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>T</D_735>
        </S_HL>
        <S_PO4>
            <D_82>0.000</D_82>
            <D_350>000010</D_350>
        </S_PO4>
    </G_HL>
     <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>I</D_735>
        </S_HL>
        <S_LIN>
            <D_350>000030</D_350>
            <D_235>BP</D_235>
        </S_LIN>
    </G_HL>
    <G_HL>
        <S_HL>
            <D_628/>
            <D_734/>
            <D_735>I</D_735>
        </S_HL>
        <S_LIN>
            <D_350>000040</D_350>
            <D_235>BP</D_235>
        </S_LIN>
    </G_HL>
    <S_CTT>
        <D_354>B2B_LIN_COUNTER</D_354>
    </S_CTT>
</M_856>

报错的XSLT代码

添加<xsl:if>条件后出现编译错误:

<?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"/>
    <!-- Identity template to copy nodes as-is -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="G_HL[S_HL/D_735='T' and S_PO4/D_350 = following::G_HL[S_HL/D_735='I']/S_LIN/D_350]">
        <xsl:copy-of select="."/>
        <xsl:if test="count(//G_HL/S_HL/[D_735='T']) ge 2)">
        <xsl:copy-of select="following::G_HL[S_HL/D_735='I' and S_LIN/D_350 = current()/S_PO4/D_350]"/>
    </xsl:if>
</template>
</xsl:stylesheet>

错误分析

  1. 版本声明不匹配:代码声明为XSLT 1.0,但需求明确使用XSLT 2.0,版本声明需与环境一致。
  2. XPath语法错误:count(//G_HL/S_HL/[D_735='T'])中多了一个冗余斜杠,正确路径应为count(//G_HL[S_HL/D_735='T']);同时条件末尾多了一个右括号),导致语法不合法。
  3. 逻辑表述冗余:原匹配模板已筛选出G_HL[S_HL/D_735='T']节点,条件判断应直接统计这类节点的总数。

修正后的XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <!-- Identity template to copy nodes as-is -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="G_HL[S_HL/D_735='T' and S_PO4/D_350 = following::G_HL[S_HL/D_735='I']/S_LIN/D_350]">
        <xsl:copy-of select="."/>
        <!-- 判断符合条件的T节点数量是否大于1 -->
        <xsl:if test="count(//G_HL[S_HL/D_735='T']) gt 1">
            <xsl:copy-of select="following::G_HL[S_HL/D_735='I' and S_LIN/D_350 = current()/S_PO4/D_350]"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

关键修正点

  • 将XSLT版本声明改为2.0,匹配需求环境。
  • 修正XPath表达式:count(//G_HL[S_HL/D_735='T'])准确统计所有S_HL/D_735='T'的G_HL节点数量。
  • 使用gt 1替代ge 2,语义更贴合“数量大于1”的需求(两者效果一致,但表述更直观)。
  • 移除条件末尾多余的右括号,修复语法错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 11:59:49