Solved

Removing the xmlns attribute

Posted on 2003-11-22
8
1,457 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do i send an api key in a request header 3 44
Problem to event 3 52
How do I bind the results to a grid 3 24
Create XML 5 34
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

747 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

11 Experts available now in Live!

Get 1:1 Help Now