使用可变数量的节点粉碎 xml

发布于 2024-08-28 08:48:51 字数 1198 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-09-04 08:48:51

这似乎可以完成工作:

select 
    AuthorList.value('(ForeName/text())[1]', 'varchar(max)') as ForeName,
    AuthorList.value('(LastName/text())[1]', 'varchar(max)') as LastName,
    AuthorList.value('(Initials/text())[1]', 'varchar(max)') as Initials
from 
    XMLs CROSS APPLY 
      xml.nodes('//AuthorList/Author') AS AuthorList(AuthorList)

This seems to do the job:

select 
    AuthorList.value('(ForeName/text())[1]', 'varchar(max)') as ForeName,
    AuthorList.value('(LastName/text())[1]', 'varchar(max)') as LastName,
    AuthorList.value('(Initials/text())[1]', 'varchar(max)') as Initials
from 
    XMLs CROSS APPLY 
      xml.nodes('//AuthorList/Author') AS AuthorList(AuthorList)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文