xslt:创建新元素时的默认命名空间和空命名空间

时间:2015-04-14 00:20:51

标签: xslt

在我看来,xslt没有区分默认命名空间和空命名空间。

ElementB.namespace-uri()下方应"http://ns1"还是""

<ElementA xmlns="http://ns1">
  <ElementB/>
</ElementA>

下面ElementB.namespace-uri()如何:

<ElementA xmlns="http://ns1">
  <ElementB xmlns=""/>
</ElementA>

上述两种情况ElementB.namespace-uri()应该不同吗?但是,当我尝试使用xslt 1.0时,它会为两种情况返回""

我想知道如何区分这两种情况(默认命名空间和空命名空间)?如何使用xsl添加具有null命名空间的元素以生成<ElementB xmlns=""/>

新更新:我认为这是我的调试器问题。现在我看到在案例1中按预期返回默认命名空间。

Howver,对于case2(使用null命名空间),如何创建具有null命名空间的元素?使用xslt,我想更改以下xml

<ElementA xmlns="http://ns1">
  <ElementB xmlns=""/>
</ElementA>

要:

<ElementA xmlns="http://ns1-new">
  <ElementB xmlns=""/>
</ElementA>

我正在使用改编自michael.hor257k(Replace namespace node string value with xslt---what is wrong with my xsl)的样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  <xsl:param name="nsmapdoc" select="document('maps.xml')"/>

  <xsl:template match="*">
    <xsl:variable name="old-ns" select="namespace-uri()"/>
    <xsl:variable name="map-entry" select="$nsmapdoc//namespace[@source=$old-ns]"/>
    <xsl:variable name="new-ns">
      <xsl:choose>
        <xsl:when test="$map-entry">
            <xsl:value-of select="$map-entry/@target"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$old-ns"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>   
    <xsl:element name="{local-name()}" namespace="{$new-ns}">
      <xsl:apply-templates select="@*|node()"/>
   </xsl:element>
  </xsl:template>

  <xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy/>
  </xsl:template>

</xsl:stylesheet>

使用上面的xsl,我看到以下

<ElementB xmlns=""/>

错误地转变为

<ElementB/>

所以我想问题应该是:如何使用xsl添加空名称空间?

更新2:

添加null命名空间是正确的方法。这是处理器问题。我使用的处理器是“com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl”(JDK中的默认处理器?)。更改为“org.apache.xalan.processor.TransformerFactoryImpl”并添加所需的xalan jar后,问题就消失了。

感谢您的投入!

0 个答案:

没有答案
相关问题