使用 SQL 选择单个 xml 节点 [英] selecting individual xml node using SQL
本文介绍了使用 SQL 选择单个 xml 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含许多节点的大型 XML 注释.
I have a large XML note with many nodes.
有没有一种方法可以让我从较大的 XML 中只选择一个节点及其所有内容?
is there a way that I can select only a single node and all of its contents from the larger XML?
我使用的是 sql 2005
i am using sql 2005
推荐答案
您应该使用 query()方法,如果你想得到你的 XML 的一部分.
You should use the query() Method if you want to get a part of your XML.
declare @XML xml
set @XML =
'
<root>
<row1>
<value>1</value>
</row1>
<row2>
<value>2</value>
</row2>
</root>
'
select @XML.query('/root/row2')
结果:
<row2>
<value>2</value>
</row2>
如果您想要来自特定节点的值,您应该使用 value() 方法.
If you want the value from a specific node you should use value() Method.
select @XML.value('(/root/row2/value)[1]', 'int')
结果:
2
更新:
如果要将 XML 分解为多行,请使用 nodes() 方法.
If you want to shred your XML to multiple rows you use nodes() Method.
获取值:
declare @XML xml
set @XML =
'
<root>
<row>
<value>1</value>
</row>
<row>
<value>2</value>
</row>
</root>
'
select T.N.value('value[1]', 'int')
from @XML.nodes('/root/row') as T(N)
结果:
(No column name)
1
2
要获取整个 XML:
select T.N.query('.')
from @XML.nodes('/root/row') as T(N)
结果:
(No column name)
<row><value>1</value></row>
<row><value>2</value></row>
这篇关于使用 SQL 选择单个 xml 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文