我有一个
XML文件,其中我需要数据的节点都被命名为相同.我理解如何访问第一个(或第二个记录),以便以下查询只给我第二个作者(< a1>标记).我如何将所有作者作为一个列?
DECLARE @MyXML XML SET @MyXML = '<refworks> <reference> <rt>Journal Article</rt> <sr>Print(0)</sr> <id>869</id> <a1>Aabye,Martine G.</a1> <a1>Hermansen,Thomas Stig</a1> <a1>Ruhwald,Morten</a1> <a1>PrayGod,George</a1> <a1>Faurholt-Jepsen,Daniel</a1> <a1>Jeremiah,Kidola</a1> <a1>Faurholt-Jepsen,Maria</a1> <a1>Range,Nyagosya</a1> </reference> </refworks>' SELECT author.value('(a1)[2]','varchar(MAX)') AS 'Author' FROM @MyXML.nodes('/refworks/reference') AS ref(author)
解决方法
试试这个 :-
SELECT author.value('./text()[1]','varchar(MAX)') AS 'Author' FROM @MyXML.nodes('//refworks/reference/child::node()') AS ref(author) where author.value('local-name(.)[1]','varchar(100)') ='a1'
child :: node()表示一个轴说明符,它是child,::是轴分隔符.
要了解用于在节点中向下钻取的子轴,可以在此MSDN文档中找到.
或manipulating xml data in sql server
更新 :-
更简单的方法您处于正确的轨道上.在from子句中指定子节点以过滤数据
SELECT author.value('(.)[1]','varchar(MAX)') AS 'Author' FROM @MyXML.nodes('/refworks/reference/a1') AS ref(author)