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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -