I have the following SQL query:
DECLARE @XMLDOC XML
SET @XMLDOC = '<Feed><Product><Name>Foo</Name></Product></Feed>'
SELECT x.u.value('Name[1]', 'varchar(100)') as Name
from @XMLDOC.nodes('/Feed/Product') x(u)
This returns:
Name
----
Foo
However, if my <Feed>
node has an xmlns
attribute, then this doesn't return any results:
DECLARE @XMLDOC XML
SET @XMLDOC = '<Feed xmlns="bar"><Product><Name>Foo</Name></Product></Feed>'
SELECT x.u.value('Name[1]', 'varchar(100)') as Name
from @XMLDOC.nodes('/Feed/Product') x(u)
Returns:
Name
----
This only happens if I have an xmlns
attribute, anything else works fine.
Why is this, and how can I modify my SQL query to return results regardless of the attributes?
If your XML document has XML namespaces, then you need to consider those in your queries!
So if your XML looks like your sample, then you need:
Or if you prefer to have explicit control over which XML namespace to use (e.g. if you have multiple), use XML namespace prefixes:
As well as the
XMLNAMESPACES
solution, you can also use the hideously bulkylocal-name
syntax...You can define namespaces like: