We help IT Professionals succeed at work.
Get Started

Displaying XML node attribute with XQuery and TSQL

MrDavidThorn
MrDavidThorn asked
on
1,089 Views
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
<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
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