SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to filter the namespace from the xml in SQLServer..plz help


How to filter the namespace from the xml in SQLServer..plz help

Author
Message
suvo.kundu
suvo.kundu
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 80
i have fetched a big xml from db.there is one namespace attached with it.i want to remove the namespace.plz help
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3250 Visits: 1779
AFAIK you can't remove the namespace without doing some kind hack like casting the XML to a varchar(max) and then replacing out the namespace and any prefixes that it uses before casting back to xml again. This isn't a nice thing to do and prone to problems for obvious reasons. You could use a xslt to do this though and would be more reliable than using REPLACE() within TSQL.

Working with namespaces is pretty easy though and all you need is to have something like this before your query:

WITH XMLNAMESPACES(DEFAULT 'http://somenamespace')



If you want to post an example xml document showing what your namespace looks like, we can help further with the syntax so that you can query the xml without removing out the namespace.
suvo.kundu
suvo.kundu
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 80
Actually what i am doing is my query is fetching xml from db which contains the name space
like
<XML xmmlns:https://temp.../test.xsd>

Declare @xml xml

select @xml =xmlcontent from tablename where id =4
this @xml looks like
<XML xmmlns:https://temp.../test.xsd>
<>
<>
.....


i want to remove the namespace 'xmmlns:https://temp.../test.xsd'
plz suggest me the query how to use
'WITH XMLNAMESPACES(DEFAULT 'http://somenamespace')'
in this sql
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3250 Visits: 1779
Here you go, here are two example queries querying an xml structure that has a namespace. The first query will actually include some auto generated prefixes by SQL, but the second query doesn't:


DECLARE @xml XML
SET @xml = '
<XML xmlns="https://temp.../test.xsd">
<Content>
<Data>
<Node1>1</Node1>
<Node2>2</Node2>
</Data>
<Data>
<Node1>1</Node1>
<Node2>2</Node2>
</Data>
</Content>
</XML>'

--using WITH XMLNAMESPACES
;WITH XMLNAMESPACES(DEFAULT 'https://temp.../test.xsd')
SELECT @xml.query('//Content')

--OR to return xml without the p1 prefixes:
SELECT @xml.query(
'declare default element namespace "https://temp.../test.xsd";
//Content')


suvo.kundu
suvo.kundu
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 80
actually i need to get the output of filtered xml as


<XML >
<>
<>
.....


plz suggest a way
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3250 Visits: 1779
As I mentioned previously, AFAIK, there is no real way to just strip out a namespace from an xml structure by using TSQL other than to cast the xml to a varchar(max) and hack it out by using REPLACE(). But as shown, working with xml namespaces is relatively straight forward....
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search