473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bulk add prefix to XmlDocument

Hi All,

I have an XML document that looks like this:

<book>
<title>Foo</title>
<description>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Body this is the body</p>
</div>
</description>
</book>

Now I need to append this document into another XmlDocument object. To
correctly do this I want to add a namespace on the original XmlDocument with
"book" as it prefix. But I don't want change the namespace of the <div> since
it has a namespace defined on it.

So for example the result should look something like this:

<book:book xmlns:book="http://localhost/book.xsd">
<book:title>Foo</book:title>
<book:description>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Body this is the body</p>
</div>
</book:description>
</book:book>

So how would I do this? What's the best way to achive the result? I've tried
using Regex but soon stumped on the problem that it couldn't differentiate
non-namespaced nodes with the one with namespace. Or maybe with super funky
Regex I could use that, but is there a nicer XML way of doing it?

Thanks,
--
Victor Hadianto
http://www.synop.com/Products/SauceReader/

Nov 12 '05 #1
1 1769


Victor Hadianto wrote:

I have an XML document that looks like this:

<book>
<title>Foo</title>
<description>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Body this is the body</p>
</div>
</description>
</book>

Now I need to append this document into another XmlDocument object. To
correctly do this I want to add a namespace on the original XmlDocument with
"book" as it prefix. But I don't want change the namespace of the <div> since
it has a namespace defined on it.

So for example the result should look something like this:

<book:book xmlns:book="http://localhost/book.xsd">
<book:title>Foo</book:title>
<book:description>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Body this is the body</p>
</div>
</book:description>
</book:book>

So how would I do this?


You could use XSLT to do this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:book="http://example.com/2004/books">

<xsl:output method="xml" encoding="UTF-8" />

<xsl:template match="xhtml:* | /">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{concat('book:', local-name())}"
namespace="http://example.com/2004/books">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

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

</xsl:stylesheet>

The XslTransform class allows you to run XSLT transformations.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

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

Similar topics

20
by: akej via SQLMonster.com | last post by:
Hi, i have table with 15 columns CREATE TABLE . ( PRIMARY KEY , NULL , NULL , NULL , NULL , (50) NULL , NULL
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
1
by: Rich | last post by:
I am experimenting with the Cryptography API (using c#). I have successfully managed to sign some XML using an RSA algorithm. I can then vaidate the XML with a Verify method that I have developed....
3
by: S | last post by:
I'm having a problem attaching a name space. I'm getting an xml string of information as a result of a third party web service. I can't change the format of the string. The xml file looks like...
5
by: Anupam Godbole | last post by:
Hi everybody, Whenever I load a .HxT/.HxC file thru the XmlDocument::Load function, I get the error "The URI prefix is not recognized.". How do I resolve the error? Waiting for reply... Anupam
1
by: Jeremy Chapman | last post by:
I am trying to use xpath queries on an xmldocument passed to my web service. The document nodes have namespace prefixes. How can I programatically create an xpath query and run it assuming that...
3
by: Andy Fish | last post by:
Hi, I am creating a new XmlDocument from scratch, but I can't see how I can make a namespace declaration and associate a prefix with it. For instance in the following example: XmlDocument doc...
4
by: BizTalk Benjamin | last post by:
Hi, I have an XmlDocument loaded from a memory stream. I set the document element prefix in this way XmlElement e = xDoc.DocumentElement; e.Prefix = "abc" When i simply write the document...
1
by: GR | last post by:
Hi, I have something like the following: (Please note this not actual code and it's very rough) string source = "<abc:Root xmlns:abc="http://mynamespace.com><abc:Record/></abc:Root>"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.