Displaying XML node attribute with XQuery and TSQL

MrDavidThorn
MrDavidThorn used Ask the Experts™
on
I have an xml file that is not particularly well formed, I can bulk load the XML file into a sql server xml Field then Im using xquery to query the xml into relational data, the problem that I have is a key value is set as an attribute of a node rather than the text, with my current query I dont know how to select and display the attribute value, any help would be appricated, have an example below

I want to return the child number,

The xml structure
<parents>
<Parent>
<ParentName>Smith</FamilyName>
<Postoode>MidOfNo</PostCode>
<Child ChildNumber =1>
          <ChildName>Billy</ChildName>
          <ChildAge>2</ChildName>
</Child>
<Child ChildNumber =2>
          <ChildName>Teddy</ChildName>
          <ChildAge>3</ChildName>
</Child>
</Parent>
</parents>

The Tsql/xQuery im using is as follows

Select T1.child.query('data(childName)') as ChildName, 
T1.child.query('data(childAge)') as ChildAge
From FamilyXML
CROSS APPLY XMLField.Nodes('(/parents/parent/child)')
as T1(Child)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I have now taken a difference approach to the problem

Author

Commented:
becuase no one else knew the answer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial