在SQL Server中,我想获取XML对象记录中的第n个(第二个)标记值
declare @xml xml =
'<animals>
<dog>
<petname>fred</petname>
</dog>
<cat>
<petname>bill</petname>
</cat>
</animals>'
select
n.value('petname[1]','varchar(10)') as name,
n.value('../(*).name()[1]','varchar(10)') as animalType -- don't know how to get this???
from
@xml.nodes('animals/*') as a(n)
我想要的结果:
name animalType
-----------------
fred dog
bill cat
由于
答案 0 :(得分:1)
您是否尝试过本地名称?
declare @xml xml =
'<animals>
<dog>
<petname>fred</petname>
</dog>
<cat>
<petname>bill</petname>
</cat>
</animals>'
select
n.value('petname[1]','varchar(10)') as name,
n.value('local-name(.)','varchar(10)') as animalType
from @xml.nodes('animals/*') as a(n)