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

XSLT技术问题:移除子节点空命名空间及动态根命名空间配置

Alright, let's fix that annoying empty namespace issue popping up in your child nodes after switching to a dynamic root namespace in XSLT 1.0. The problem happens because when you set a dynamic default namespace on the root <Foo> element, any child elements you output without explicitly specifying the same namespace get tagged with xmlns="" (since XSLT 1.0 doesn't automatically inherit the dynamic default namespace for child elements). Here are two straightforward solutions:

Solution 1: Explicitly Assign the Dynamic Namespace to Each Child Element

Instead of writing static child elements like <Bar>, use <xsl:element> for every child node and reference the same dynamic namespace value as your root. This ensures all elements live in the same namespace, eliminating the empty namespace declaration.

Here's how to adjust your code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/NewDataSet">
    <!-- Store the dynamic namespace in a variable for reuse -->
    <xsl:variable name="rootNS" select="Table/Foo_Dynamic_XMLNS"/>
    
    <xsl:element name="Foo" namespace="{$rootNS}">
      <!-- Use xsl:element for each child to apply the same namespace -->
      <xsl:element name="Bar" namespace="{$rootNS}">
        <xsl:value-of select="Table/Bar_Value"/>
      </xsl:element>
      <xsl:element name="Baz" namespace="{$rootNS}">
        <xsl:value-of select="Table/Baz_Value"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Solution 2: Use a Global Template to Convert All Child Elements to the Dynamic Namespace

If you have lots of child elements, writing <xsl:element> for each one is tedious. Instead, create a reusable template that matches all elements and re-renders them in your target namespace. This automates the process for every child node.

Here's the full implementation:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <!-- Store the dynamic namespace once -->
  <xsl:variable name="targetNS" select="/NewDataSet/Table/Foo_Dynamic_XMLNS"/>

  <xsl:template match="/NewDataSet">
    <xsl:element name="Foo" namespace="{$targetNS}">
      <!-- Apply the global template to all child nodes -->
      <xsl:apply-templates select="Table/*"/>
    </xsl:element>
  </xsl:template>

  <!-- Template to convert any element to the target namespace -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="{$targetNS}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <!-- Preserve attributes as-is (no namespace needed for most attributes) -->
  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>

  <!-- Preserve text, comments, and processing instructions -->
  <xsl:template match="text()|comment()|processing-instruction()">
    <xsl:copy/>
  </xsl:template>
</xsl:stylesheet>

Key Notes:

  • If some child elements in your input XML already have their own specific namespaces, adjust the global template with a more specific match (e.g., match="*[not(namespace-uri())]") to only convert elements without an existing namespace.
  • Using a variable for the dynamic namespace keeps your code DRY and easier to maintain.

内容的提问来源于stack exchange,提问作者Westerlund.io

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:20:47