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

XSLT中<xsl:for-each>匹配失败问题求助

问题分析与解决方案

核心问题是XPath选择器未正确匹配输入XML的命名空间:

  • 输入XML的根元素hrmanifest属于旧命名空间https://github.com/rSmart/ce-tech-docs/tree/master/v2_0,但你的XSLT在xsl:for-each里直接写hrmanifest,没有关联对应的命名空间前缀,导致无法定位元素。
  • 你在输出的h:records上重新定义了h前缀为新命名空间,这会和输入用的h前缀混淆,建议用不同前缀区分新旧命名空间。

修正后的XSLT代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:v2="https://github.com/rSmart/ce-tech-docs/tree/master/v2_0"
                xmlns:v3="https://github.com/KualiCo/ce-tech-docs/tree/master/v3_0"
                exclude-result-prefixes="v2">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <v3:hrmanifest schemaVersion="3.0">
      <xsl:attribute name="statusEmailRecipient">
        <xsl:value-of select="/v2:hrmanifest/@statusEmailRecipient"/>
      </xsl:attribute>
      <xsl:attribute name="recordCount">
        <xsl:value-of select="/v2:hrmanifest/@recordCount"/>
      </xsl:attribute>
      <v3:records>
        <xsl:for-each select="/v2:hrmanifest/v2:records/v2:record">
          <v3:record>
            Text
          </v3:record>
        </xsl:for-each>
      </v3:records>
    </v3:hrmanifest>
  </xsl:template>
</xsl:stylesheet>

关键修改点说明:

  • 用v2前缀绑定旧命名空间,v3绑定新命名空间,避免前缀混淆
  • 所有输入XML的元素选择都加上v2:前缀,确保XPath能正确定位
  • 输出的新格式元素统一用v3:前缀,显式前缀更清晰
  • 修正根元素的XPath路径,从/*改为/v2:hrmanifest,定位更精准

这样修改后,xsl:for-each就能正确匹配到输入XML里的record元素了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 13:10:50