troubleshooting Question

Displaying XML node attribute with XQuery and TSQL

Avatar of MrDavidThorn
MrDavidThorn asked on
Windows Server 2008
2 Comments1 Solution1092 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
MrDavidThorn

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros