我有一个像这样的XML文档:
helper.updateData(id,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
我的输出结果如下:
DROP TABLE #temp
create table #temp (xml_data xml)
insert into #temp (xml_data) values
('<UPDATE><ENTITY DN="test"><ATTRIBUTE N="test name" V="test value" /> </ENTITY></UPDATE>')
select C.value('@DN', 'varchar(max)') as [ENTITY.DN]
,C.value('@N', 'varchar(max)') as [ATTRIBUTE.N]
,C.value('@V', 'varchar(max)') as [ATTRIBUTE.V]
from #temp cross apply
#temp.xml_data.nodes('UPDATE/ENTITY') as X(C)
我想看看:
test | NULL | NULL
有关我如何错误配置的任何想法?
答案 0 :(得分:1)
如果一个ENTITY
可能在您的评论中有多个ATTRIBUTE
子项,那么您需要另一个APPLY
粉碎ATTRIBUTE
,例如:
declare @temp table(xml_data xml)
insert into @temp (xml_data) values
('<UPDATE>
<ENTITY DN="test">
<ATTRIBUTE N="test name" V="test value"/>
<ATTRIBUTE N="foo1" V="bar1"/>
<ATTRIBUTE N="foo2" V="bar2"/>
</ENTITY>
</UPDATE>')
select ent.value('@DN', 'varchar(max)') as [ENTITY.DN]
,attr.value('@N', 'varchar(max)') as [ATTRIBUTE.N]
,attr.value('@V', 'varchar(max)') as [ATTRIBUTE.V]
from @temp t
cross apply t.xml_data.nodes('UPDATE/ENTITY') as X(ent)
cross apply ent.nodes('./ATTRIBUTE') as Y(attr)
<强> SQLFiddle Demo
强>
输出
| ENTITY.DN | ATTRIBUTE.N | ATTRIBUTE.V |
|-----------|-------------|-------------|
| test | test name | test value |
| test | foo1 | bar1 |
| test | foo2 | bar2 |
答案 1 :(得分:0)
更改为
DROP TABLE #temp
create table #temp (xml_data xml)
insert into #temp (xml_data) values
('<UPDATE><ENTITY DN="test"><ATTRIBUTE N="test name" V="test value" /> </ENTITY></UPDATE>')
select C.value('@DN', 'varchar(max)') as [ENTITY.DN]
,C.value('ATTRIBUTE[1]/@N', 'varchar(max)') as [ATTRIBUTE.N]
,C.value('ATTRIBUTE[1]/@V', 'varchar(max)') as [ATTRIBUTE.V]
from #temp cross apply
#temp.xml_data.nodes('UPDATE/ENTITY') as X(C)
答案 2 :(得分:0)
DimaSUN提供的答案将有效。这是另一种查询:
SELECT xml_data.query('data(/UPDATE/ENTITY/@DN)') as [ENTITY.DN],
xml_data.query('data(/UPDATE/ENTITY/ATTRIBUTE/@N)') as [ATTRIBUTE.N],
xml_data.query('data(/UPDATE/ENTITY/ATTRIBUTE/@V)') as [ATTRIBUTE.V]
FROM #temp;