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

XSLT 2.0 count()函数返回结果不符合预期问题排查

问题:统计符合条件的XML节点数量始终返回1

原始XML结构

<wd:All_Eligible_Time_Off_Plans_for_Worker_group>
    <wd:Time_Off_Plan wd:Descriptor="Personal Floating Holiday">
        <wd:ID wd:type="WID">9cae06e1e9c601054c26dc48e9790000</wd:ID>
        <wd:ID wd:type="Absence_Plan_ID">HCM_ABSENCE_PTO</wd:ID>
    </wd:Time_Off_Plan>
    <wd:Time_Off_Balance>66</wd:Time_Off_Balance>
</wd:All_Eligible_Time_Off_Plans_for_Worker_group>

<wd:All_Eligible_Time_Off_Plans_for_Worker_group>
    <wd:Time_Off_Plan wd:Descriptor="Personal Floating Holiday">
        <wd:ID wd:type="WID">9cae06e1e9c601054c26dc48e9790000</wd:ID>
        <wd:ID wd:type="Absence_Plan_ID">HCM_ABSENCE_PTM</wd:ID>
    </wd:Time_Off_Plan>
    <wd:Time_Off_Balance>99</wd:Time_Off_Balance>
</wd:All_Eligible_Time_Off_Plans_for_Worker_group>

错误的XSLT代码

<xsl:variable name="PTOTimeoffCount">
    <xsl:value-of select="count(wd:All_Eligible_Time_Off_Plans_for_Worker_group/wd:Time_Off_Plan/wd:ID[@wd:type ='Absence_Plan_ID'] = 'HCM_ABSENCE_PTO')"/>
</xsl:variable>

问题原因

你当前的表达式中,wd:ID[@wd:type ='Absence_Plan_ID'] = 'HCM_ABSENCE_PTO'是一个布尔比较,返回单个布尔值(true或false)。count()函数会将这个单个布尔值视为一个独立节点,因此计数结果始终为1,和实际匹配的节点数量无关。

修正后的XSLT代码

将条件整合到节点选择的谓词(方括号)中,让count()直接统计符合条件的节点集合:

<xsl:variable name="PTOTimeoffCount">
    <xsl:value-of select="count(wd:All_Eligible_Time_Off_Plans_for_Worker_group[wd:Time_Off_Plan/wd:ID[@wd:type='Absence_Plan_ID' and .='HCM_ABSENCE_PTO']])"/>
</xsl:variable>

说明

  • 谓词[wd:Time_Off_Plan/wd:ID[@wd:type='Absence_Plan_ID' and .='HCM_ABSENCE_PTO']]用于筛选包含指定ID的All_Eligible_Time_Off_Plans_for_Worker_group节点
  • count()会准确统计满足条件的节点数量:无匹配时返回0,有1个匹配返回1,多个匹配返回对应数量

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:53:27