Solved

Removing the xmlns attribute

Posted on 2003-11-22
8
1,473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

623 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