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

XSLT 2.0 按条件筛选XML中employment_information节点的咨询

解决方案

问题原因

你现有XSLT的匹配规则是无条件删除所有assignment_class不等于'GA'的employment_information节点,没有前置判断Person节点下该类节点的总数量,因此无法适配0或1个节点时原样输出的要求。

修正后的完整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"/>
    <!-- 身份模板:默认所有节点/属性原样复制 -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- 仅当Person下employment_information总数大于1时,才删除非GA类型的节点 -->
    <xsl:template match="Person/employment_information[assignment_class != 'GA'][count(../employment_information) > 1]"/>
</xsl:stylesheet>

逻辑说明

  • 保留了默认的身份复制模板,保证除需要过滤的employment_information节点外,其余所有节点、属性完全原样输出
  • 新增的过滤规则增加了双重判断:
    1. 当前employment_information的assignment_class值不等于'GA'
    2. 当前节点的父节点Person下,employment_information的总数量大于1
  • 只有同时满足以上两个条件的节点才会被删除,其他场景下所有employment_information都会走默认复制逻辑原样保留

适配场景验证

  • 当Person下employment_information节点数≥2:仅保留assignment_class为'GA'的节点,其余同类型节点被删除
  • 当Person下employment_information节点数=1:无论assignment_class取值如何,节点完整保留
  • 当Person下employment_information节点数=0:输入XML整体原样输出

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 15:15:00