Looks to me xslt does not differentiate default namespace and null namespace.
Should ElementB.namespace-uri()
be "http://ns1"
or ""
below?
<ElementA xmlns="http://ns1">
<ElementB/>
</ElementA>
How about ElementB.namespace-uri()
below:
<ElementA xmlns="http://ns1">
<ElementB xmlns=""/>
</ElementA>
Should ElementB.namespace-uri()
be different for the above two cases? When I tried with xslt 1.0, however, it returns ""
for both cases.
I wonder how to differentiate the two cases (default namespace and null namespace)? How to add an element with null namespace with xsl to generate <ElementB xmlns=""/>
?
New update: I think it is my debugger issue. Now I see the default namespace is returned as expected in case 1.
Howver, for case2 (with null namespace), how can I create an element with null namespace? Using xslt, I'd like to change the following xml
<ElementA xmlns="http://ns1">
<ElementB xmlns=""/>
</ElementA>
To:
<ElementA xmlns="http://ns1-new">
<ElementB xmlns=""/>
</ElementA>
I'm using a stylesheet adapted from 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>
With the above xsl, I see the following
<ElementB xmlns=""/>
is wrongly transformed to
<ElementB/>
So I guess the question should be:how to add a null namespace using xsl?
Update 2:
It is the right way to add null namespace. It is the processor problem. The processor I was using is "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" (the default one in JDK?). The problem is gone after I change to "org.apache.xalan.processor.TransformerFactoryImpl" and add the needed xalan jars.
Thank you for the input!