1

I need to get the values of attributes present in XML document using MS SQL query

Ex : I have a XML which looks below

<trade xmlns="www.somewebsite.com" Action = "Insert" TradeNumber = "1053" Volume = "25" DateTime = "2013-12-06T10:22:47.497" PNC = "false">
     <Specifier Specifierid = "112" Span = "Single" Name = "Indian"/>
</trade>

I need to fetch

  1. The values of "TradeNumber", "Volume", "DateTime" in trade tag

  2. "Name" from Specifier tag

in a single row under their specific columns

Like

TradeNumber  Volume   DateTime    Name
1053      25  2013-12-06T10:22:47.497 Indian

I tried using many ways but couldn't figure it out. Please help

1
  • i couldn't get how to create table in question section Commented Dec 6, 2013 at 14:24

2 Answers 2

1
declare @data xml ='
<trade xmlns="www.somewebsite.com" Action = "Insert" TradeNumber = "1053" Volume = "25" DateTime = "2013-12-06T10:22:47.497" PNC = "false">
     <Specifier Specifierid = "112" Span = "Single" Name = "Indian"/>
</trade>'

;with xmlnamespaces(default 'www.somewebsite.com')
select 
    @data.value('trade[1]/@TradeNumber', 'int') as TradeNumber,
    @data.value('trade[1]/@Volume', 'int') as Volume,
    @data.value('trade[1]/@DateTime', 'datetime') as [DateTime],
    @data.value('(trade/Specifier)[1]/@Name', 'nvarchar(max)') as Name

--------------------------------------------------------
TradeNumber Volume  DateTime                Name
       1053     25  2013-12-06 10:22:47.497 Indian

Or, if there're could be more than one trades:

;with xmlnamespaces(default 'www.somewebsite.com')
select 
    t.c.value('@TradeNumber', 'int') as TradeNumber,
    t.c.value('@Volume', 'int') as Volume,
    t.c.value('@DateTime', 'datetime') as [DateTime],
    t.c.value('Specifier[1]/@Name', 'nvarchar(max)') as Name
from @data.nodes('trade') as t(c)
Sign up to request clarification or add additional context in comments.

1 Comment

Faster than me :) You have the possibility to make research based on your attribute, instead of 1 you can specify contains(@attribute, "value"). Take care Value can always only give back one value
0

Another variant:

declare @doc xml

select @doc= '
<trade xmlns="www.somewebsite.com" Action = "Insert" TradeNumber = "1053" Volume = "25" DateTime = "2013-12-06T10:22:47.497" PNC = "false">
     <Specifier Specifierid = "112" Span = "Single" Name = "Indian"/>
</trade>
'



;WITH XMLNAMESPACES('www.somewebsite.com' AS p)
SELECT 
      ActionAttribute = Y.i.value('(@Action)[1]', 'varchar(40)')
    , TradeNumber = Y.i.value('@TradeNumber[1]', 'varchar(40)') 
    , Specifierid = Y.i.value('(./p:Specifier)[1]/@Specifierid', 'nvarchar(max)') 
FROM 
    @doc.nodes('/p:trade') AS Y(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.