dbTalk Databases Forums  

xml parsing with xquery returning null records

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss xml parsing with xquery returning null records in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dan
 
Posts: n/a

Default xml parsing with xquery returning null records - 04-04-2011 , 05:08 PM






I've recently begun receiving xml files and ran into a small problem
that seems syntax related, but I simply can't find where it is. Below
is the xml file and sample code of what I'm doing. I cut up the xml
data, it's far more complex then what I'm presenting below. I think
it has something to do what how I'm address eventId but am struggling
with figuring it out. My preference is to stay out of SSIS if at all
possible on this project.

I'm getting rows to return, but they are all null when there is data
in the file.

If you see something with what I'm doing (and replied), I'd be greatly
thankful.


<?xml version="1.0" encoding="UTF-8"?>
<pitchMediaList>
<pitchMedia eventId="243">
<media type="FLASH" url="xx.html"/>
</pitchMedia>
</pitchMediaList>


select x.value('eventId[1]/eventId[1]','int') as url
FROM (
SELECT CAST(x AS XML)
FROM OPENROWSET(
BULK 'c:\videofile.xml',
SINGLE_BLOB) AS T(x)
) AS T(x)
CROSS APPLY x.nodes('pitchMediaList/pitchMedia') AS X(pitchMedia);

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: xml parsing with xquery returning null records - 04-05-2011 , 02:20 AM






Dan (dquill1 (AT) gmail (DOT) com) writes:
Quote:
?xml version="1.0" encoding="UTF-8"?
pitchMediaList
pitchMedia eventId="243"
media type="FLASH" url="xx.html"/
/pitchMedia
/pitchMediaList


select x.value('eventId[1]/eventId[1]','int') as url
FROM (
SELECT CAST(x AS XML)
FROM OPENROWSET(
BULK 'c:\videofile.xml',
SINGLE_BLOB) AS T(x)
) AS T(x)
CROSS APPLY x.nodes('pitchMediaList/pitchMedia') AS X(pitchMedia);
When you retrieve attribute, you need to put an @ in front:


DECLARE @x xml
SELECT @x = '<?xml version="1.0" encoding="UTF-8"?>
<pitchMediaList>
<pitchMedia eventId="243">
<media type="FLASH" url="xx.html"/>
</pitchMedia>
</pitchMediaList>'

select X.c.value('@eventId[1]','int') as url
FROM @x.nodes('pitchMediaList/pitchMedia') AS X(c);



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.