Change root name of an XML document

In SQL Server 2005, we can easily change the root name of an xml document. Actually we are not "changing" it, we create a new root and add all the elements of the old root under the new one.
 
The following is saple code to change the root from "Root" to "NewRoot":

declare @y xml,@x xml

SET

@x=N‘<Root xmlns="www.test.com/test"><Test>aaa</Test></Root>’;

SELECT

@y=@x.query(‘declare namespace test="www.test.com/test"; <NewRoot xmlns="www.test.com/test">{/test:Root/test:Test}</NewRoot>’)

SELECT

@y

 
Advertisements
This entry was posted in SQL server 2005. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s