Buffalo
Buffalo

Reputation: 4042

Excluding namespaces from XML

My xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:go="http://www.google.com"
    exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>

    <xsl:template match="/">

        <xsl:call-template name="SomeTemplate">
            <xsl:with-param name="Element" select="'randomParam'"/>
        </xsl:call-template>

    </xsl:template>

</xsl:stylesheet>

The SomeLibrary.xsl file:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
    <xsl:template name="SomeTemplate">
    <xsl:param name="Element"/>
        <Blabla>
            <xsl:value-of select="$Element" />    
        </Blabla>
    </xsl:template>
</xsl:stylesheet>

Input xml: just use an empty XML. The result is this:

<?xml version="1.0" encoding="UTF-8"?>

<Blabla xmlns="http://www.google.com">
    randomParam
</Blabla>

What I want is to have the "Blabla" node without a namespace. How can I remove it or make sure it doesn't get there, without modifying my "SomeLibrary.xsl"?

Upvotes: 2

Views: 1873

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

In case you don't want to edit the imported stylesheet code, the way to remove the namespace is with a two-pass transformation (which in XSLT 1.0 requires using an xxx:node-set() extension function):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:variable name="vrtfPass1">
     <xsl:call-template name="SomeTemplate">
                <xsl:with-param name="Element" select="'randomParam'"/>
       </xsl:call-template>
     </xsl:variable>

     <xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
     <xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
 </xsl:template>

 <xsl:template match="*" mode="pass2">
  <xsl:element name="{name()}">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates mode="pass2"/>
  </xsl:element>
 </xsl:template>

 <xsl:template name="SomeTemplate" xmlns="http://www.google.com">
   <xsl:param name="Element"/>
            <Blabla>
                <xsl:value-of select="$Element" />
            </Blabla>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used), the wanted result (the default namespace removed) is produced:

<Blabla>randomParam</Blabla>

Update:

The OP has indicated in a comment that he is using Xalan 2.07.

Below is almost the same solution, but with namespace and name for the xxx:node-set() function, as used in Xalan:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:variable name="vrtfPass1">
     <xsl:call-template name="SomeTemplate">
                <xsl:with-param name="Element" select="'randomParam'"/>
       </xsl:call-template>
     </xsl:variable>

     <xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
     <xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
 </xsl:template>

 <xsl:template match="*" mode="pass2">
  <xsl:element name="{name()}">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates mode="pass2"/>
  </xsl:element>
 </xsl:template>

 <xsl:template name="SomeTemplate" xmlns="http://www.google.com">
   <xsl:param name="Element"/>
            <Blabla>
                <xsl:value-of select="$Element" />
            </Blabla>
 </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Maestro13
Maestro13

Reputation: 3696

add exclude-result-prefixes="go" to the stylesheet element.
and, of course, update the namespace declaration from the default one to xmlns:go="http://www.google.com"

Upvotes: 0

hroptatyr
hroptatyr

Reputation: 4809

Explicitly create them with:

<xsl:element name="Blabla" namespace="">...</xsl:element>

but this means fiddling with the SomeLibrary.xsl file.

Upvotes: 1

Related Questions