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

使用XSLT 2.0忽略命名空间匹配元素的问题求助

问题分析与解决

核心问题

你的XML所有元素都属于默认命名空间Invoice_Summary_Template,但你的XSLT错误地使用了xsi前缀(这个前缀绑定的是XMLSchema-instance命名空间,仅用于schema相关属性,和元素命名空间无关),导致元素匹配失败。

修正步骤

  1. 在XSLT的xsl:stylesheet标签中,为XML的默认命名空间定义一个前缀(比如inv):xmlns:inv="Invoice_Summary_Template"
  2. 所有匹配XML元素的地方,都要加上这个前缀,包括match属性和select属性中的元素
  3. 保留XSLT的基础配置(输出格式、空格处理)

修正后的XSLT代码

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inv="Invoice_Summary_Template">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- 匹配命名空间下的Invoice_Collection元素 -->
<xsl:template match="inv:Invoice_Collection">    
        <!-- 选择命名空间下的Invoice元素进行分组 -->
        <xsl:for-each-group select="inv:Invoice" group-by="@Id">        
            <Item>
                <header>
                    <invoice VAL="Id">
                        <xsl:value-of select="@Id"/>
                    </invoice>
                </header>               
            </Item>        
        </xsl:for-each-group>
</xsl:template>

<!-- 匹配根节点,精准触发目标模板,避免默认模板输出冗余内容 -->
<xsl:template match="/">
    <xsl:apply-templates select="inv:Template/inv:Batches/inv:Collection/inv:Item/inv:Batch/inv:Report/inv:Invoices/inv:Invoice_Collection"/>
</xsl:template>

</xsl:stylesheet>

额外说明

  • 生成的<Item>、<header>、<invoice>等元素不带命名空间,符合你的输出需求
  • 根节点匹配模板用于精准定位目标元素,避免XSLT默认模板输出XML中无关的文本节点

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 08:38:16