Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > removing namespaces from an XML document

Reply
Thread Tools

removing namespaces from an XML document

 
 
Matt
Guest
Posts: n/a
 
      04-07-2004
Hello,

I have an XML document similar to the following:

<DataItems>
<Data xmlns="http://www.me.com">
<DataInformation xmlns:a="http://www.me.com/ASettings"
xsi:type="a:Stuff1">
<a:Name>Matt</a:Name>
<a:TN>555-5555</a:TN>
</DataInformation>
</Data>
<Data xmlns="http://www.me.com">
<DataInformation xmlns:b="http://www.me.com/BSettings"
xsi:type="b:Stuff2">
<b:Name>Bob</b:Name>
<b:TN>555-6666</b:TN>
</DataInformation>
</Data>
</DataItems>

What I would like to do is take all the namespaces and throw them in
the garbage! For example, I want to remove the xmlns attribute from
the Data node, I want to remove all the "a" and "b" prefixes from all
the nodes, etc. I want the final outcome to look like the following:

<DataItems>
<Data>
<DataInformation type="Stuff1">
<Name>Matt</Name>
<TN>555-5555</TN>
</DataInformation>
</Data>
<Data>
<DataInformation type="Stuff2">
<Name>Bob</Name>
<TN>555-6666</TN>
</DataInformation>
</Data>
</DataItems>

I am using C++ and Microsoft's xml implementation (DOM). I have no
choice but to use the raw Microsoft interfaces.

Does anyone have a good idea of how to do this?

Thanks,
Matt
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-08-2004


Matt wrote:

> I have an XML document similar to the following:
>
> <DataItems>
> <Data xmlns="http://www.me.com">
> <DataInformation xmlns:a="http://www.me.com/ASettings"
> xsi:type="a:Stuff1">
> <a:Name>Matt</a:Name>
> <a:TN>555-5555</a:TN>
> </DataInformation>
> </Data>
> <Data xmlns="http://www.me.com">
> <DataInformation xmlns:b="http://www.me.com/BSettings"
> xsi:type="b:Stuff2">
> <b:Name>Bob</b:Name>
> <b:TN>555-6666</b:TN>
> </DataInformation>
> </Data>
> </DataItems>
>
> What I would like to do is take all the namespaces and throw them in
> the garbage! For example, I want to remove the xmlns attribute from
> the Data node, I want to remove all the "a" and "b" prefixes from all
> the nodes, etc. I want the final outcome to look like the following:
>
> <DataItems>
> <Data>
> <DataInformation type="Stuff1">
> <Name>Matt</Name>
> <TN>555-5555</TN>
> </DataInformation>
> </Data>
> <Data>
> <DataInformation type="Stuff2">
> <Name>Bob</Name>
> <TN>555-6666</TN>
> </DataInformation>
> </Data>
> </DataItems>
>
> I am using C++ and Microsoft's xml implementation (DOM). I have no
> choice but to use the raw Microsoft interfaces.
>
> Does anyone have a good idea of how to do this?


XSLT is good for such tasks, the following stylesheet throws out all
namespace prefixes from element and attribute nodes:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput method="xml" encoding="UTF-8" />

<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."
/></xsl:attribute>
</xsl:template>

<xsl:template match="text() | processing-instruction() | comment()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

The transformation is done with a JavaScript and MSXML 4 as follows (I
know you asked about C++ but I have never used MSXML with C++ thus I
hope you will be able to translate the JavaScript into C++):

var sourceDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
sourceDocument.async = false;
sourceDocument.preserveWhiteSpace = true;
sourceDocument.validateOnParse = false;
var loaded = sourceDocument.load('test20040408.xml');
if (loaded) {
var xslDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xslDocument.async = false;
loaded = xslDocument.load('test20040408Xsl.xml');
if (loaded) {
var resultDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
sourceDocument.transformNodeToObject(xslDocument, resultDocument);
resultDocument.save('whatever.xml');
}
}

While I tested I have seen that the transformtion is not quite doing
what you want as you also seem to want to remove any "prefixes" in
attribute values so you need to change the stylesheet to use the
following template for attribute nodes:

<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:choose>
<xsl:when test="contains(., ':')">
<xsl:value-of select="substring-after(., ':')" />
</xsl:when>
<xsltherwise>
<xsl:value-of select="." />
</xsltherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
Matt Muise
Guest
Posts: n/a
 
      04-08-2004
Hey,

That's awesome!!! It works perfectly!

Thank you very much!!

Matt



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Matt Muise
Guest
Posts: n/a
 
      04-12-2004
Hi there, I have one more question about this.

If I have a document like ...

<ROOT>
<ITEMS>
<ITEM>
<DATA>01234567</DATA>
<NAME>Name1</NAME>
</ITEM>
<ITEM>
<DATA>76543210</DATA>
<NAME>Name2</NAME>
</ITEM>
</ITEMS>
</ROOT>

... how would I add the value of the NAME node as an attribute of the
DATA node ... for example ...

<ROOT>
<ITEMS>
<ITEM>
<DATA name="Name1">01234567</DATA>
</ITEM>
<ITEM>
<DATA name="Name2">76543210</DATA>
</ITEM>
</ITEMS>
</ROOT>

Sorry for my non understanding of XSL.

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Alex Shirshov
Guest
Posts: n/a
 
      04-13-2004
Hello, Matt!
You wrote on 12 Apr 2004 17:17:31 GMT:


[Sorry, skipped]

[xslt]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ITEM">
<ITEM>
<DATA name="{NAME}">
<xsl:value-of select="DATA"/>
</DATA>
</ITEM>
</xsl:template>
</xsl:stylesheet>
[/xslt]

With best regards, Alex Shirshov.


 
Reply With Quote
 
Matt Muise
Guest
Posts: n/a
 
      04-13-2004
Cool ... that does work, except for one problem. The DATA node has
several subnodes underneath it which get lost when the transform takes
place. I probably should have mentioned the fact that there are subnodes
underneath, eh?

So the document is more like:

<ROOT>
<ITEMS>
<ITEM>
<DATA>
<THIS>121212</THIS>
<THAT>121212</THAT>
</DATA>
<NAME>name1</name>
</ITEM>
<ITEM>
<DATA>
<ONE>121212</ONE>
<TWO>121212</TWO>
</DATA>
<NAME>name2</name>
</ITEM>
</ITEMS>
</ROOT>

I want to preserve all the nodes underneath data. Data can contain
several different types of nodes. Sorry I didn't mention this in the
initial posting.

Thanks for your assistance!

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Alex Shirshov
Guest
Posts: n/a
 
      04-13-2004
Hello, Matt!
You wrote on 13 Apr 2004 12:57:24 GMT:


[Sorry, skipped]

Oh, it's simple.
Replace the
[xslt]
<DATA name="{NAME}">
<xsl:value-of select="DATA"/>
</DATA>
[/xslt]
on
[xslt]
<DATA name="{NAME}">
<xsl:apply-templates select="*[not(self::NAME)]"/>
</DATA>

[/xslt]

With best regards, Alex Shirshov.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing a tag from an xml document Chris Gallagher Ruby 2 11-24-2006 12:43 PM
Refer to an XML document from within an XML document Manish Hatwalne XML 1 07-13-2004 10:24 AM
Removing an XML element from an XML document. How can I do that? GhislainTanguay ASP .Net 3 05-21-2004 07:48 PM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM
Newbie: XML 2 XML and namespaces Mark Smits XML 2 09-21-2003 09:51 PM



Advertisments