使用可变数量的节点粉碎 xml
我有一个 xml 列,其中包含如下数据:
<AuthorList CompleteYN="Y">
<Author ValidYN="Y">
<LastName>Alió</LastName>
<ForeName>J L</ForeName>
<Initials>JL</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Ortiz</LastName>
<ForeName>D</ForeName>
<Initials>D</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Muftuoglu</LastName>
<ForeName>O</ForeName>
<Initials>O</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Garcia</LastName>
<ForeName>M J</ForeName>
<Initials>MJ</Initials>
</Author>
</AuthorList>
作者数量是可变的。我可以毫无问题地提取第一作者等:
SELECT
ID,
AuthorList.value('(Author/ForeName)[1]', 'varchar(max)') as ForeName,
AuthorList.value('(Author/LastName)[1]', 'varchar(max)') as LastName,
AuthorList.value('(Author/Initials)[1]', 'varchar(max)') as Initials
FROM
XMLs CROSS APPLY
xml.nodes('//AuthorList') AS AuthorList(AuthorList)
我只是好奇如何“动态”地实现这样的事情:
1 JL Alió JL 1 D 奥尔蒂斯 D 1 O 穆夫托格鲁 O 1 MJ 加西亚 MJ
谢谢!
I have an xml column which contain data like this:
<AuthorList CompleteYN="Y">
<Author ValidYN="Y">
<LastName>Alió</LastName>
<ForeName>J L</ForeName>
<Initials>JL</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Ortiz</LastName>
<ForeName>D</ForeName>
<Initials>D</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Muftuoglu</LastName>
<ForeName>O</ForeName>
<Initials>O</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Garcia</LastName>
<ForeName>M J</ForeName>
<Initials>MJ</Initials>
</Author>
</AuthorList>
The number of Authors is variable. I have no problem to extract the first authors etc.:
SELECT
ID,
AuthorList.value('(Author/ForeName)[1]', 'varchar(max)') as ForeName,
AuthorList.value('(Author/LastName)[1]', 'varchar(max)') as LastName,
AuthorList.value('(Author/Initials)[1]', 'varchar(max)') as Initials
FROM
XMLs CROSS APPLY
xml.nodes('//AuthorList') AS AuthorList(AuthorList)
I am just curious how I can achieve something like this ‘dynamically’:
1 J L Alió JL
1 D Ortiz D
1 O Muftuoglu O
1 M J Garcia MJ
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎可以完成工作:
This seems to do the job: