XQuery获取节点的属性名与值

e.g:

DECLARE @xmlSource XML
SET @xmlSource = '<t topA="1">
  <a x="2" z="3">
    <b message="Hello"/>
  </a>
  <c y="5"/>
</t>'

SELECT @xmlSource.query('data(//@*)');
SELECT @xmlSource.query('for $nodes in //*, $attr in $nodes/@* return (local-name($attr),string($attr), ''&#xA;'')');

结果:

1 2 3 Hello 5


topA 1 
 x 2 
 z 3 
 message Hello 
 y 5 
local-name($attr)获取属性的名称
string($attr)获取属性的值
'&#xA;'可认为是格式化,用于换行,如果没有加则结果如下:
topA 1 x 2 z 3 message Hello y 5
 
 
原文地址:https://www.cnblogs.com/vincentDr/p/3703295.html