sql - Querying XML data types which have xmlns node attributes -
i have following sql query:
declare @xmldoc xml set @xmldoc = '<feed><product><name>foo</name></product></feed>' select x.u.value('name[1]', 'varchar(100)') name @xmldoc.nodes('/feed/product') x(u)
this returns:
name ---- foo
however, if <feed>
node has xmlns
attribute, doesn't return results:
declare @xmldoc xml set @xmldoc = '<feed xmlns="bar"><product><name>foo</name></product></feed>' select x.u.value('name[1]', 'varchar(100)') name @xmldoc.nodes('/feed/product') x(u)
returns:
name ----
this happens if have xmlns
attribute, else works fine.
why this, , how can modify sql query return results regardless of attributes?
if xml document has xml namespaces, need consider in queries!
so if xml looks sample, need:
-- define default xml namespace use ;with xmlnamespaces(default 'bar') select x.u.value('name[1]', 'varchar(100)') name @xmldoc.nodes('/feed/product') x(u)
or if prefer have explicit control on xml namespace use (e.g. if have multiple), use xml namespace prefixes:
-- define xml namespace ;with xmlnamespaces('bar' b) select x.u.value('b:name[1]', 'varchar(100)') name @xmldoc.nodes('/b:feed/b:product') x(u)
Comments
Post a Comment