Link to home
Start Free TrialLog in
Avatar of charulathak
charulathak

asked on

Removing the xmlns attribute

My root node is article and has an attribute xmlns which i would like to remove.

<article xmlns="xxxxxxxxx">
 <.
.
.
.
..
.
.
.
.
..
</article>
Basically my question is that i want to remove the xmlns attribute of the article tag.  Can i do this.  I tried using the removeAttribute method in visual basic but this does not seem to be working.  

Help please
Avatar of jm60697
jm60697
Flag of Finland image

Yes it can be removed from the transformation results with XSLT. See below:
[jarkko@hypsu02 development]$ cat jarkko.xml
<?xml version="1.0"?>
<doc xmlns:jarkko="jarkko.com">
   <para>First para</para>
   <para>Second para</para>
</doc>

[jarkko@hypsu02 development]$ cat jarkko.xsl
<?xml version="1.0"?><!--filename.xsl-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0" xmlns:jarkko="jarkko.com"
                exclude-result-prefixes="jarkko"> <!-- define excludes here -->
<xsl:output method="xml"/>                                                    
<xsl:template match="/">                         <!--root rule-->
  <result>
   <xsl:copy-of select="/doc/para"/>
  </result>
</xsl:template>
</xsl:stylesheet>

[jarkko@hypsu02 development]$ xsltproc jarkko.xsl jarkko.xml
<?xml version="1.0"?>
<result><para>First para</para><para>Second para</para></result>
[jarkko@hypsu02 development]$

Cheers,
jm60697
I'm wondering whether charulathak really needs to remove the namespace, or whether he sees that as the only solution to another problem that could be fixed by properly handling the namespace?  

Regards,
Mike Sharp
Avatar of charulathak
charulathak

ASKER

Nope i am not trying to find a solution to another problem by removing the namespace.  I do really need to remove the namespace.

However once i remove the namespace i will be adding another tag

<Meta name="schema" content="xxxxxx" />

Which is the reason why i want to remove the xmlns from my xml document.

ASKER CERTIFIED SOLUTION
Avatar of rdcpro
rdcpro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, for me, Jarkko's code produces this:

<result>
    <para xmlns:jarkko="jarkko.com">First para</para>
    <para xmlns:jarkko="jarkko.com">Second para</para>
</result>

because the xsl:copy-of copies the node, and any associated namespace nodes...since para is a child of doc, any namespace nodes that are in doc, are inherited by para--even though Jarkko's example doesn't qualify any nodes to the namespace!  That is, the jarkko.com namespace is declared, and qualified to the prefix "jarkko", but no element node in the XML is prefixed with jarkko, so no node in the XML is part of that namespace.  If they were, then:

   <xsl:copy-of select="/doc/para"/>

wouldn't work, because it does not select a namespace-qualified node.  It would have to be written as:

   <xsl:copy-of select="/jarkko:doc/jarkko:para"/>

Anyway, even though the Jarkko.com namespace is not attached to any nodes, it is truly there in the document, and should have been copied to the output.  Since Jarkko's XSLT processor did not do that, it seems to me like he's found a bug.  Which processor is it?

Regards,
Mike Sharp


I used in-build XML processor of Red Hat 9.0, which in this case is xsltproc.

Cheers,
Jarkko