[xsl] Removing Namespace declaration from root element [xsltproc XSLT 1.0]

Subject: [xsl] Removing Namespace declaration from root element [xsltproc XSLT 1.0]
From: pankaj.c@xxxxxxxxxxxxxxxxxx
Date: Fri, 17 Sep 2010 12:30:04 +0530
Hello all,

I am trying to remove namespace declaration from root element in my xml 
input. Below is what it looks like:

Root Element:
=========
<chapter xmlns:sb="http://www.elsevier.com/xml/common/struct-bib/dtd"; 
xmlns:ce="http://www.elsevier.com/xml/common/dtd"; xmlns:mml="
http://www.w3.org/1998/Math/MathML"; xmlns:xlink="
http://www.w3.org/1999/xlink"; xmlns:aid="
http://ns.adobe.com/AdobeInDesign/4.0/"; xmlns:aid5="
http://ns.adobe.com/AdobeInDesign/5.0/"; aid:pstyle="" aid5:tablestyle="" 
version="5.2" xml:lang="en" docsubtype="chp" id="c0030">
<ce:footnote id="fn0010">
======

Desired Output
=========

<chapter version="5.2" xml:lang="en" docsubtype="chp" id="c0030">
<ce:footnote id="fn0010">

I tried several ways to get this done but everytime getting odd results.

1st Sample
=======
<xsl:template match="@*|comment()|processing-instruction()|text()">
    <xsl:copy-of select="."/>
</xsl:template>

<!-- elements: create a new element with the same name, but no namespace 
-->
<xsl:template match="chapter">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

Which gives me the output. Notice the namespace declaration added to 
ce:footnote.

<?xml version="1.0" encoding="UTF-8"?>
<chapter xmlns:xlink="http://www.w3.org/1999/xlink"; version="5.2" 
xml:lang="en" docsubtype="chp" id="c0030">
<ce:footnote xmlns:ce="http://www.elsevier.com/xml/common/dtd"; 
id="fn0010">

I WOULD PREFER REMOVING NAMSPACE xmlns:xlink="http://www.w3.org/1999/xlink
" FROM TOP ELEMENT THOUGH THIS IS BEING USED IN OTHER CHILD ELEMENTS LIKE  
xlink:href="www.google.com".

2nd Sample (while commenting above template)
========

<xsl:template match="*|@*|comment()"> 
 <xsl:choose>
         <xsl:when test="name()='xmlns:sb'"/>
         <xsl:when test="name()='xmlns:ce'"/>
         <xsl:when test="name()='xmlns:mml'"/>
        <xsl:otherwise>
        <xsl:copy> 
                <xsl:apply-templates select="text()|*|@*|comment()"/> 
        </xsl:copy> 
        </xsl:otherwise>
 </xsl:choose>
</xsl:template>


Output [Nothing happens]
================

<chapter xmlns:sb="http://www.elsevier.com/xml/common/struct-bib/dtd"; 
xmlns:ce="http://www.elsevier.com/xml/common/dtd"; xmlns:mml="
http://www.w3.org/1998/Math/MathML"; xmlns:xlink="
http://www.w3.org/1999/xlink"; xmlns:aid="
http://ns.adobe.com/AdobeInDesign/4.0/"; xmlns:aid5="
http://ns.adobe.com/AdobeInDesign/5.0/"; version="5.2" xml:lang="en" 
docsubtype="chp" id="c0030">
<ce:footnote id="fn0010">

I did tried:

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

<xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>


Output 
===================================

<chapter xmlns:xlink="http://www.w3.org/1999/xlink"; version="5.2" 
xml:lang="en" docsubtype="chp" id="c0030">
<ce:footnote xmlns:ce="http://www.elsevier.com/xml/common/dtd"; 
id="fn0010">

Notice again the unexpected namespace declaration in <ce:footnote>

I am using commands --noout --novalid in xsltproc.

Other than that I am getting MathML parser warning while transforming:

parser warning : xmlns:mml: '-//W3C//DTD MathML 2.0 Mod ES//EN' is not a 
valid URI
//www.elsevier.com/xml/common/dtd" xmlns:mml="-//W3C//DTD MathML 2.0 Mod 
ES//EN"

Is this not a valid URL? Do the prefix "mml" causing the problem for the 
valid URL or in larger picture IS THIS WARNING COULD BE REASON FOR GETTING 
ARBITRARY RESULT?

Any suggestions will be appreciated.

TIA,
Pankaj

Current Thread