Reading XML in SQL Server with default and custom namespace   Leave a comment

To read the XML document in SQL which contains the Name Spaces (default and custom) follow the below mentioned steps

1) Declare a variable of XML data type and assign the XML data to it.
2) Note down the list of name spaces used in XML document which need to be registered while extracting data.
3) Defined name spaces using “:with xmlnamespaces” to extract the data contains the name spaces.
4) Use the “value” and “nodes” on XML document object to extract the data. SQL example:

Declare @xmlOut xml
Set @xmlOut = 
'<xbrl xmlns="" xmlns:test="">
   <test:Method1Result>Element Data</test:Method1Result>

declare @xDoc as xml
set @xDoc = cast(@xmlOut as xml)

declare @hdoc int
exec sp_xml_preparedocument @hdoc OUTPUT, @xDoc

;with xmlnamespaces('' as [default], '' as test)
select T.N.value('.', 'nvarchar(max)') As Data
from @xDoc.nodes('/default:xbrl/default:Method1Response/test:Method1Result') as T(N)


Posted October 9, 2014 by Izharuddin Shaikh in SQL

Tagged with

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: