Solved

Removing the xmlns attribute

Posted on 2003-11-22
8
1,460 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:charulathak
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:jm60697
ID: 9802469
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
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9804175
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
0
 

Author Comment

by:charulathak
ID: 9804645
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.

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 26

Accepted Solution

by:
rdcpro earned 75 total points
ID: 9805147
Then the XSLT approach is easiest...and it's what I'd recommend.  Also, you might as well add the <Meta> element at the same time.  You can get the actual value of the namespace with the XPath function namespace-uri():

identityStripNamespace.xslt
=======================================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:temp="urn:schemas-rdcpro-com:tempuri"
            exclude-result-prefixes="temp">
    <xsl:output method="xml" encoding="UTF-16" indent="yes"/>
    <xsl:template match="/ | node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:copy/>
    </xsl:template>
    <xsl:template match="temp:article">
        <article>
            <xsl:apply-templates select="@*"/>
            <Meta name="schema" content="{namespace-uri()}"/>
            <xsl:apply-templates select="node()"/>
        </article>
    </xsl:template>
    <xsl:template match="temp:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>


Given this XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="identityStripNamespace.xslt" ?>
<article xmlns="urn:schemas-rdcpro-com:tempuri" testAttr="snafu">
    <foo>Bar</foo>
</article>

This is the result:

<?xml version="1.0" encoding="UTF-16"?>
<?xml-stylesheet type="text/xsl" href="identityStripNamespace.xslt" ?>
<article testAttr="snafu">
<Meta name="schema" content="urn:schemas-rdcpro-com:tempuri" />
<foo>Bar</foo>
</article>

Note that all namespace nodes are stripped, and the namespace-uri is placed in the meta element (which is what I presume you wanted to do...)

Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9805166
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


0
 
LVL 3

Expert Comment

by:jm60697
ID: 9808898
I used in-build XML processor of Red Hat 9.0, which in this case is xsltproc.

Cheers,
Jarkko
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question