We help IT Professionals succeed at work.
Get Started

Displaying XML node attribute with XQuery and TSQL

MrDavidThorn asked
Last Modified: 2012-06-27
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
<Child ChildNumber =1>
<Child ChildNumber =2>

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

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE