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

如何使用XSLT 2跳过含有效SurveyDate的记录,仅处理无内容的记录?

解决XSLT 2中仅处理为空记录的问题

你之前用not(SurveyDate/text())的判断逻辑失效,核心原因是这个条件无法处理**包含空白字符(空格、换行、制表符)**的情况——只要元素内存在任何文本节点(哪怕是空白),text()就会返回该节点,not(text())就会判定为false,导致本该处理的空白记录被错误跳过。

正确的判断条件

改用normalize-space(SurveyDate) = ''来检测是否无有效内容,normalize-space()会自动去除文本前后的空白,并将中间连续空白替换为单个空格,结果为空字符串则说明元素内没有有效日期内容。

你也可以使用等价的写法:

  • not(normalize-space(SurveyDate))
  • empty(normalize-space(SurveyDate))

修改后的完整XSLT示例

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <OutputRecords>
      <xsl:apply-templates select="Records/Record"/>
    </OutputRecords>
  </xsl:template>

  <!-- 仅处理SurveyDate无有效内容的记录 -->
  <xsl:template match="Record[normalize-space(SurveyDate) = '']">
    <ProcessedRecord>
      <xsl:copy-of select="ID"/>
      <xsl:copy-of select="Data"/>
      <!-- 这里添加你的其他处理逻辑 -->
    </ProcessedRecord>
  </xsl:template>

  <!-- 跳过SurveyDate有内容的记录(空模板不输出任何内容) -->
  <xsl:template match="Record[normalize-space(SurveyDate) != '']"/>
</xsl:stylesheet>

测试效果

针对你的测试XML:

<Records>
  <Record>
    <ID>1</ID>
    <SurveyDate>2024-05-20</SurveyDate>
    <Data>内容1</Data>
  </Record>
  <Record>
    <ID>2</ID>
    <SurveyDate></SurveyDate>
    <Data>内容2</Data>
  </Record>
  <Record>
    <ID>3</ID>
    <SurveyDate>   </SurveyDate>
    <Data>内容3</Data>
  </Record>
</Records>

会输出期望的结果:

<OutputRecords>
  <ProcessedRecord>
    <ID>2</ID>
    <Data>内容2</Data>
  </ProcessedRecord>
  <ProcessedRecord>
    <ID>3</ID>
    <Data>内容3</Data>
  </ProcessedRecord>
</OutputRecords>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 12:31:00