HighTechTalks DotNet Forums  

How to add prefix to root element?

Dotnet XML microsoft.public.dotnet.xml


Discuss How to add prefix to root element? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
sashaxp
 
Posts: n/a

Default How to add prefix to root element? - 12-23-2007 , 03:26 AM






i am receiving following xml from one party:


#1
<Request xmlns="http://somens.com">
<SomeElement1>Element data</SomeElement1>
<SomeElement1>Another element data</SomeElement1>
</Request>

and before processing want to add prefix to the root element so the
document should be:

#2
<ns0:Request xmlns:ns0="http://somens.com">
<SomeElement1>Element data</SomeElement1>
<SomeElement1>Another element data</SomeElement1>
</ns0:Request>

trying to add prefix to DocumentElement results in addition another
xmlns with prefix instead of adding prefix to already existent xmlns,
after specifying prefix xml looks like this

#3
<ns0:Request xmlns="http://somens.com" xmlns:ns0="http://somens.com">>
<SomeElement1>Element data</SomeElement1>
<SomeElement1>Another element data</SomeElement1>
</ns0:Request>

Is there any way to overcome that behaviour and get xml #2?

Thanks in advance,
Sasha

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: How to add prefix to root element? - 12-23-2007 , 06:58 AM






sashaxp wrote:
Quote:
i am receiving following xml from one party:


#1
Request xmlns="http://somens.com"
<SomeElement1>Element data</SomeElement1
<SomeElement1>Another element data</SomeElement1
/Request

and before processing want to add prefix to the root element so the
document should be:

#2
ns0:Request xmlns:ns0="http://somens.com"
<SomeElement1>Element data</SomeElement1
<SomeElement1>Another element data</SomeElement1
/ns0:Request
So you are renaming the root element and the child elements as the
default namespace is missing and that way the child elements are now in
no namespace.

Such tasks are solvable with an XSLT stylesheet:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://somens.com"
version="1.0">

<xslutput method="xml"/>

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

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

</xsl:stylesheet>

You can apply XSLT tranformations using
System.Xml.Xsl.XslCompiledTransform in .NET 2.0 and later and using
System.Xml.Xsl.XslTransform in .NET 1.x.

Doing that with the DOM is rather tedious as you would need to recreate
each element with the proper prefix and namespace.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
sashaxp
 
Posts: n/a

Default Re: How to add prefix to root element? - 12-23-2007 , 08:43 AM



On 23 ฤลห, 15:58, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
sashaxp wrote:
i am receiving following xml from one party:

#1
Request xmlns="http://somens.com"
š š<SomeElement1>Element data</SomeElement1
š š<SomeElement1>Another element data</SomeElement1
/Request

and before processing want to add prefix to the root element so the
document should be:

#2
ns0:Request xmlns:ns0="http://somens.com"
š š<SomeElement1>Element data</SomeElement1
š š<SomeElement1>Another element data</SomeElement1
/ns0:Request

So you are renaming the root element and the child elements as the
default namespace is missing and that way the child elements are now in
no namespace.

Such tasks are solvable with an XSLT stylesheet:

xsl:stylesheet
š šxmlns:xsl="http://www.w3.org/1999/XSL/Transform"
š šxmlns:ns0="http://somens.com"
š šversion="1.0"

š š<xslutput method="xml"/

š š<xsl:template match="/ns0:Request"
š š š<ns0:Request
š š š š<xsl:apply-templates/
š š š</ns0:Request
š š</xsl:template

š š<xsl:template match="*/*"
š š š<xsl:element name="{local-name()}" namespace=""
š š š š<xsl:apply-templates/
š š š</xsl:element
š š</xsl:template

/xsl:stylesheet

You can apply XSLT tranformations using
System.Xml.Xsl.XslCompiledTransform in .NET 2.0 and later and using
System.Xml.Xsl.XslTransform in .NET 1.x.

Doing that with the DOM is rather tedious as you would need to recreate
each element with the proper prefix and namespace.

--

š š š š Martin Honnen --- MVP XML
š š š šhttp://JavaScript.FAQTs.com/- ๓หาูิุ รษิษาีลอูส ิลหำิ -

- ๐ฯหมฺมิุ รษิษาีลอูส ิลหำิ -
Thanks Martin,
Works fine


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.