简体   繁体   English

如何使XQuery提取XML文档中的所有匹配节点

[英]How do I get XQuery to extract all the matching nodes in an XML Document

I'm very new to XQuery and know that this is probably an easy answer but I just can't wrap my head around this one. 我对XQuery还是很陌生,并且知道这可能是一个简单的答案,但我只是无法解决这个问题。

I have an XML file like so: 我有一个像这样的XML文件:

<EventLog>
  <SongSet>
    <Song SongID="S002">
      <Title>Band on the Run</Title>
      <Composer>Paul McCartney</Composer>
      <Duration>1.15</Duration>
    </Song>
    <Song SongID="S003">
      <Title>Come on Over</Title>
      <Composer>Shania Twain</Composer>
      <Duration>3.15</Duration>
    </Song>
   </SongSet>
  <ContestantSet>
   <Contestant Name="Randy Stuss" Hometown="Ottawa">
      <Repertoire>
        <SongRef>S002</SongRef>
        <SongRef>S003</SongRef>
      </Repertoire>
     </Contestant>
     <Contestant Name="Fletcher Gee" Hometown="Toronto">
      <Repertoire>
        <SongRef>S002</SongRef>
        <SongRef>S003</SongRef>
      </Repertoire>
    </Contestant>
  </ContestantSet>
</EventLog>

I need the output to look like this: 我需要输出看起来像这样:

<songs> 
  <song> 
    <name> SONG NAME1 </name>
    <composer> COMPOSER NAME </composer>
    <singers>
      <singer>Singer1 Name  </singer>  
      <singer>Singer2 Name  </singer>  
    </singers>
  </song>
<songs>

I can do all of it except for getting the names of the singers together. 除了把歌手的名字放在一起,我可以做所有的事情。 My output looks like this: 我的输出如下所示:

<Songs
  <Song>
    <Title>Band on the Run</Title>
    <Composer>Paul McCartney</Composer>
    <Singers>
      <Singer>Fletcher Gee</Singer>
    </Singers>
  </Song>
  <Song>
    <Title>Band on the Run</Title>
    <Composer>Paul McCartney</Composer>
  <Singers>
    <Singer>Randy Stuss</Singer>
  </Singers>
  </Song>
</Songs>

And Here is my XQuery code: 这是我的XQuery代码:

for $x in //SongSet/Song 
for $y in //ContestantSet/Contestant 
where $x/@SongID = $y/Repertoire/SongRef 
return <Song><Title>{data($x/Title)}</Title>
       <Composer>{data($x/Composer)}</Composer>
       <Singers><Singer>{data($y/@Name)}</Singer></Singers></Song>

Where am I going wrong? 我要去哪里错了?

Thank you for your help!!!! 谢谢您的帮助!!!!

One possible way; 一种可能的方式; loop over <Song> elements and then for the return <Singers> part loop over the matching <Contestant> elements : 循环遍历<Song>元素,然后对于return <Singers>部分,遍历匹配的<Contestant>元素:

for $x in //SongSet/Song 
return <Song><Title>{data($x/Title)}</Title>
       <Composer>{data($x/Composer)}</Composer>
       <Singers>{for $y in //ContestantSet/Contestant[Repertoire/SongRef = $x/@SongID]
                 return <Singer>{data($y/@Name)}</Singer>}</Singers></Song>

Xpathtester Demo

when tested aginst the XML in question, the output is as expected : 在有问题的XML上进行测试时,输出是预期的:

<?xml version="1.0" encoding="UTF-8"?>
<Song>
   <Title>Band on the Run</Title>
   <Composer>Paul McCartney</Composer>
   <Singers>
      <Singer>Randy Stuss</Singer>
      <Singer>Fletcher Gee</Singer>
   </Singers>
</Song>
<Song>
   <Title>Come on Over</Title>
   <Composer>Shania Twain</Composer>
   <Singers>
      <Singer>Randy Stuss</Singer>
      <Singer>Fletcher Gee</Singer>
   </Singers>
</Song>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM