使用 t-SQL 检索 XML 元素名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3180834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:46:48 来源:igfitidea点击:
Retrieving XML element name using t-SQL
提问by Matt W
If I have:
如果我有:
<quotes>
<quote>
<name>john</name>
<content>something or other</content>
</quote>
<quote>
<name>mary</name>
<content>random stuff</content>
</quote>
</quotes>
How do I get a list of the element names 'name' and 'content' using T-SQL?
如何使用 T-SQL 获取元素名称“名称”和“内容”的列表?
The best I've got so far is:
到目前为止我得到的最好的是:
declare @xml xml
set @xml = ...
select r.value('quotes/name()[1]', 'nvarchar(20)' as ElementName
from @xml.nodes('/quotes') as records(r)
But, of course, I can't get this to work.
但是,当然,我无法让它发挥作用。
回答by Matt W
Actually, sorry, the best I've got is:
实际上,对不起,我所拥有的最好的是:
select distinct r.value('fn:local-name(.)', 'nvarchar(50)') as t
FROM
@xml.nodes('//quotes/*/*') AS records(r)
Guess I answered my own question...
猜猜我回答了我自己的问题......
回答by Susmeet Khaire
DECLARE @xml as xml
SET @xml = '<Address><Home>LINE1</Home></Address>'
SELECT Nodes.Name.query('local-name(.)') FROM @xml.nodes('//*') As Nodes(Name)
This will give the list of all elements
这将给出所有元素的列表