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

 

 
 
posted @ 2014-05-01 21:05  Vincent.Dr  阅读(1070)  评论(0编辑  收藏  举报