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

XSL转换中如何添加命名空间使输出节点携带tns前缀

问题原因

你当前的XSL代码仅在输出的my_node节点上声明了tns命名空间,但没有将my_node、my_child两个元素本身关联到该命名空间下,因此生成的元素不会自动带上tns前缀。

修改后的完整XSL代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:tns="http://www.example.com" exclude-result-prefixes="array fn map math xhtml xs err" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
    
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="*">
        <tns:my_node>
            <tns:my_child>
                <xsl:value-of select="my_value"/>
            </tns:my_child>
        </tns:my_node>
    </xsl:template>
  
</xsl:stylesheet>

修改说明

  • 在xsl:stylesheet根节点上提前声明tns命名空间,无需再手动用<xsl:namespace>标签插入命名空间声明
  • 直接使用带tns前缀的字面量元素创建输出节点,节点会自动关联到tns对应的命名空间,生成的结果完全符合预期
  • 如果你需要保留<xsl:element>的写法,也可以将name属性值改为tns:my_node、tns:my_child,效果完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 10:39:03