Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display xml attributes in sql syntax with xquery

Status
Not open for further replies.

batteam

Programmer
Sep 12, 2003
374
0
0
US
I have the following (snippet) of xml code in a SQL table colum designated as xml datatype.

<RequirementInfo AppliesToPartyID="INS_1_L9019226" FulfillerPartyID="VENDOR_EMSI" id="REQ_L9019226_476043_0000_5">
<ReqCode tc="5">Collect Urine Specimen (HOS)</ReqCode>
<RequirementInfoKey>L9019226_476043_0000_5</RequirementInfoKey>
<RequirementInfoSysKey SystemCode="PLUS" VendorCode="0029">476043</RequirementInfoSysKey>
<HORequirementRefID>5</HORequirementRefID>
...etc

I can easily pull out the xml element data using the following:

SELECT xml_data.query ('data(//RequirementInfo[1]//ReqCode)') as [XML Sections]
FROM dbo.XmlImportTest

My question is, what would the syntax be to read out an ATTRIBUTE in the above xml code snippet, like the tc attribute = "5" in the element 'ReqCode' ??

Thanks for any help you can provide.
 
I found the answer to my own question:

SELECT xml_id, xml_data.query ('data(//RequirementInfo[2]//ReqCode//@tc)') as [XML_Sections]

FROM dbo.XmlImportTest
where xml_id = 3


Regards.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top