Solved

Removing the xmlns attribute

Posted on 2003-11-22
8
1,458 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now