473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing namespaces from an XML document

Hello,

I have an XML document similar to the following:

<DataItems>
<Data xmlns="http://www.me.com">
<DataInformatio n xmlns:a="http://www.me.com/ASettings"
xsi:type="a:Stu ff1">
<a:Name>Matt</a:Name>
<a:TN>555-5555</a:TN>
</DataInformation >
</Data>
<Data xmlns="http://www.me.com">
<DataInformatio n xmlns:b="http://www.me.com/BSettings"
xsi:type="b:Stu ff2">
<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>
<DataInformatio n type="Stuff1">
<Name>Matt</Name>
<TN>555-5555</TN>
</DataInformation >
</Data>
<Data>
<DataInformatio n 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
Jul 20 '05 #1
6 8294


Matt wrote:
I have an XML document similar to the following:

<DataItems>
<Data xmlns="http://www.me.com">
<DataInformatio n xmlns:a="http://www.me.com/ASettings"
xsi:type="a:Stu ff1">
<a:Name>Matt</a:Name>
<a:TN>555-5555</a:TN>
</DataInformation >
</Data>
<Data xmlns="http://www.me.com">
<DataInformatio n xmlns:b="http://www.me.com/BSettings"
xsi:type="b:Stu ff2">
<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>
<DataInformatio n type="Stuff1">
<Name>Matt</Name>
<TN>555-5555</TN>
</DataInformation >
</Data>
<Data>
<DataInformatio n 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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:output 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:attribut e name="{local-name()}"><xsl:v alue-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.DOMDocum ent.4.0');
sourceDocument. async = false;
sourceDocument. preserveWhiteSp ace = true;
sourceDocument. validateOnParse = false;
var loaded = sourceDocument. load('test20040 408.xml');
if (loaded) {
var xslDocument = new ActiveXObject(' Msxml2.DOMDocum ent.4.0');
xslDocument.asy nc = false;
loaded = xslDocument.loa d('test20040408 Xsl.xml');
if (loaded) {
var resultDocument = new ActiveXObject(' Msxml2.DOMDocum ent.4.0');
sourceDocument. transformNodeTo Object(xslDocum ent, 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:attribut e name="{local-name()}">
<xsl:choose>
<xsl:when test="contains( ., ':')">
<xsl:value-of select="substri ng-after(., ':')" />
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
--

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

Jul 20 '05 #2
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!
Jul 20 '05 #3
Hi there, I have one more question about this.

If I have a document like ...

<ROOT>
<ITEMS>
<ITEM>
<DATA>0123456 7</DATA>
<NAME>Name1</NAME>
</ITEM>
<ITEM>
<DATA>7654321 0</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">01 234567</DATA>
</ITEM>
<ITEM>
<DATA name="Name2">76 543210</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!
Jul 20 '05 #4
Hello, Matt!
You wrote on 12 Apr 2004 17:17:31 GMT:
[Sorry, skipped]

[xslt]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output 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.
Jul 20 '05 #5
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!
Jul 20 '05 #6
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.
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11355
by: Greg Rothlander | last post by:
I posted this a few days ago and didn't get any response. I try again but ask it a little differently. I'm recieving any XML document from a client and I need to convert it to an ASCII delimited string to input into a legacy system. I've put together a VB.Net class that does this using DOM commands such as SelectSingleNode(). It works fine but I've noticed that it can not read past inbedded namespaces that are found throghout the...
1
1993
by: Maziar Aflatoun | last post by:
Hello, I have a string variable that contains XML data with many different namespaces. I like to remove all the namespaces from my XML (clean the XML). What's the quickest way to do this? Ex. <report xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" > change to <report>
11
4305
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
36
4035
by: Wilfredo Sánchez Vega | last post by:
I'm having some issues around namespace handling with XML: >>> document = xml.dom.minidom.Document() >>> element = document.createElementNS("DAV:", "href") >>> document.appendChild(element) <DOM Element: href at 0x1443e68> >>> document.toxml() '<?xml version="1.0" ?>\n<href/>' Note that the namespace wasn't emitted. If I have PyXML,
6
24489
by: fzhang | last post by:
I am relatively new to XML and C#. So, forgive me if this question is too newbie. :-) While assuming this is an easy programming task, I couldn't find a single reference anywhere for how to do it. Here is the situation: I am given an XML file like the one below from other group in my company to load the data into our database. <root xmlns="the-namespace">
7
2972
by: Simon Hart | last post by:
Hi, I have a requirement to remove the xmlns from the DOM in order to pass over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a xmlns present!?! The reason I have a xmlns present is because the Xml I am passing to CRM is a node from a bigger file that does require a xmlns and using the DOM ..OuterXml seems to set the xmlns for you automatically - which I don't want. Any help would be great.
3
7265
by: Keith Patrick | last post by:
I'm doing some document merging where I want to bring in an XmlDocument and import its document element into another document deeper in its tree. However, when serializing my underlying objects, .Net likes to add these namespaces: <RootNode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ChildNode xmlns="MyObjectHierarchyNamespace/> </RootNode> The problem this is causing me is that...
10
3587
by: Andy Fish | last post by:
hi, I have an XSLT which is producing XML output. many of the nodes in the output tree contain namespace declarations for namespaces that are used in the source document even though they are not used in the result document or the stylesheet also I find that (for namespaces that are referenced in the stylesheet) even if I put an explicit namespace declaration on the root element of the result
20
2904
by: Steve | last post by:
With the help of this newsgroup and Google I have got this code working fully in Firefox and can alert the XML in IE but because IE does not impliment the DOM "getElementsByTagNameNS()" function I cannot read the individual rates from the Cube namespace. Is there a wrapper or some other relatively simple method of getting IE to do what in Firefox is straighforward? Here is the code. Any help gratefully received.
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.