0

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!

Community
  • 1
  • 1
user3014901
  • 367
  • 2
  • 4
  • 15
  • 3
    How exactly are you testing this? In your first example, ElementB inherits the namespace from its parent; in the second example, it is in no namespace. This is **before** the transformation. You are not showing us the transformation you are using, so we are in no position to tell what is happening. – michael.hor257k Apr 14 '15 at 00:26
  • 2
    XSLT should return `"http://ns1"` in the first case and `""` in the second. Please supply complete runnable code to show exactly what you are doing and exactly what results it is producing. – Michael Kay Apr 14 '15 at 08:01
  • I updated the question above. It is my trying to create an element with null namespace not working as expected. – user3014901 Apr 15 '15 at 23:27
  • 1
    I cannot reproduce your issue. Using a map.xml document that contains only one entry `` the result received for ElementB is ``. Tested with Saxon and Xalan. The result with libxslt is syntactically different, but semantically the same. Which processor are you using, and what is the **full** result for the given example? – michael.hor257k Apr 15 '15 at 23:57
  • I am using "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl" that is included in JDK. I used jaxp interface (TransformerFactory and Transformer) to transform. For the given example, I got the following result where is not correct: – user3014901 Apr 16 '15 at 22:31
  • Found the issue. It works if the namespace is " " instead of "". So it should be . – user3014901 Apr 16 '15 at 23:20
  • Where does `` appear in your stylesheet? – michael.hor257k Apr 16 '15 at 23:27

0 Answers0