May 1, 2009 at 2:52 am
Hi guys,
I have the following xml format in an xml field in my db.
[nodes]
[nodes]
[nodes]
I've done this many a time before but this structure is slightly different and my code isn't working!?
How do I extract the Post Advert nodes?
I'm using this at the moment:
SELECT node.l.value('field[1]','nvarchar(20)') as fieldname
....
FROM my_xml_Data CROSS APPLY xml_data_column.nodes('/MyPostAd/Ad/MyPostAd') node(l)
I've tried a few ways to no avail 🙁
May 1, 2009 at 7:45 am
Hi,
I assume you're missing the namespace declaration.
;WITH XMLNAMESPACES (
)
SELECT node.l.value('text()[1]','nvarchar(20)') as fieldname
FROM my_xml_Data cross apply xml_data_column.nodes('/MyPostAd/Ad/MyPostAd') node(l)
/*
result:
fieldname
---------
[node]
[node]
[node]
*/
May 5, 2009 at 4:47 am
that still doesn't seem to be working. I added the namespace but nothing comes back 🙁
May 5, 2009 at 5:21 am
XML is case sensitive so
FROM my_xml_Data CROSS APPLY xml_data_column.nodes('/MyPostAd/Ad/MyPostAd') node(l)
should probably be
FROM my_xml_Data CROSS APPLY xml_data_column.nodes('/MYPOSTAD/AD/MYPOSTAD') node(l)
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 5, 2009 at 6:19 am
Hi guys,
The case is correct. I've double checked and it's all ok.
I'm wondering if I have set the namespace wrong...
Just to clarify with a bit more detail. Here is my XML and SQL.
XML
nodes....
nodes...
(the case is correct on this...the forum alters it to upper case for some reason!?)
and my SQL is:
;WITH XMLNAMESPACES (
DEFAULT 'http://mywebsite.com/msg/'
)
SELECT node.l.value('Ref[1]','NVARCHAR(50)') AS MyRef
FROM my_xml_Data CROSS APPLY xml_data_column.nodes('/MyPostAd/Ad/MyPostAd') node(l)
May 5, 2009 at 6:31 am
Try this
WITH XMLNAMESPACES (
'http://mywebsite.com/msg/' AS n1, 'http://mywebsite.com/' AS n2
)
SELECT node.l.value('Ref[1]','NVARCHAR(50)') AS MyRef
FROM my_xml_Data CROSS APPLY xml_data_column.nodes('/n1:MyPostAd/n2:Ad/n2:MyPostAd') node(l)
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 5, 2009 at 6:51 am
Hi Mark,
Ok...that's getting a bit further I think. I've done what you say but I now get the correct number of rows, but all NULL values.
May 5, 2009 at 7:00 am
Also change
SELECT node.l.value('Ref[1]','NVARCHAR(50)') AS MyRef
to
SELECT node.l.value('n2:Ref[1]','NVARCHAR(50)') AS MyRef
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 5, 2009 at 7:03 am
Ah I see. I'm with you now.
Fantastic, that worked. Thanks Mark! It's just the 1st time I've encountered namespaces on my XML imports
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply