How can I get the value of an attribute out of an XML document as a standard SQL Server data type? For instance, if my XML document were:
<RootNode>
<Object ID = "1234">
<AnElement>Some Other Value</AnElement>
</Object>
</RootNode>
What would be the correct XQuery to get the value of the ID attribute in the Object element?
Thanks in advance.
Kevin J Lambert
create table #xml_temp (
xml_col xml
)
insert into #xml_temp values('<RootNode>
<Object ID = "1234">
<AnElement>Some Other Value</AnElement>
</Object>
</RootNode>')
--With query() function
select xml_col.query('data(/RootNode/Object/@.ID)') from #xml_temp
--With value() function
select xml_col.value('data(/RootNode/Object/@.ID)[1]','int') from #xml_temp