まず、はい、これは宿題です - どこが間違っているのか教えてください。
私は XQuery を学んでいます。私のタスクの 1 つは、パフォーマンスの曲 ID のリストを取得し、パフォーマンスの合計時間を決定することです。以下のスニペットから、songID をパフォーマンスから曲の長さに相互参照する方法を特定できる場所を教えてもらえますか?
質問の最後に私の試みをリストしました。
私の現在の XQuery コードは次のようになります。
let $songIDs := doc("C:/Users/rob/Downloads/A4_FLOWR.xml")
//SongSet/Song
for $performance in doc("C:/Users/rob/Downloads/A4_FLOWR.xml")
//ContestantSet/Contestant/Performance
return if($performance/SongRef[. =$songIDs/@SongID])
then <performanceDuration>{
data($performance/SongRef)
}</performanceDuration>
else ()
どの出力:
<performanceDuration>S005 S003 S004</performanceDuration>
<performanceDuration>S001 S007 S002</performanceDuration>
<performanceDuration>S008 S009 S006</performanceDuration>
<performanceDuration>S002 S004 S007</performanceDuration>
各 S00x は曲の ID であり、参照されている xml ドキュメント (部分的なドキュメント) で見つけました。
<SongSet>
<Song SongID="S001">
<Title>Bah Bah Black Sheep</Title>
<Composer>Mother Goose</Composer>
<Duration>2.99</Duration>
</Song>
<Song SongID="S005">
<Title>Thank You Baby</Title>
<Composer>Shania Twain</Composer>
<Duration>3.02</Duration>
</Song>
</SongSet>
パフォーマンス セクションは次のようになります。
<Contestant Name="Fletcher Gee" Hometown="Toronto">
<Repertoire>
<SongRef>S001</SongRef>
<SongRef>S002</SongRef>
<SongRef>S007</SongRef>
<SongRef>S010</SongRef>
</Repertoire>
<Performance>
<SongRef>S001</SongRef>
<SongRef>S007</SongRef>
<SongRef>S002</SongRef>
</Performance>
</Contestant>
私の試み
ネストされたループを使用すると思っていましたが、失敗しました:
let $songs := doc("C:/Users/rob/Downloads/A4_FLOWR.xml")
//SongSet/Song
for $performance in doc("C:/Users/rob/Downloads/A4_FLOWR.xml")
//ContestantSet/Contestant/Performance
return if($performance/SongRef[. =$songs/@SongID])
for $song in $songIDs
(: gives an error in BaseX about incomplete if :)
then <performanceDuration>{
data($performance/SongRef)
}</performanceDuration>
else ()
- 編集 -
内側のループを修正しましたが、ID に一致する曲だけでなく、すべての曲の長さを取得しています。これは可変スコープによるものだと感じていますが、よくわかりません:
let $songs := doc("C:/Users/rob/Downloads/A4_FLOWR.xml")//SongSet/Song
for $performance in doc("C:/Users/rob/Downloads/A4_FLOWR.xml")//ContestantSet/Contestant/Performance
return if($performance/SongRef[. =$songs/@SongID])
then <performanceDuration>{
for $song in $songs
return if($performance/SongRef[. =$songs/@SongID])
then
sum($song/Duration)
else ()
}</performanceDuration>
else ()
}
出力:
<performanceDuration>2.99 1.15 3.15 2.2 3.02 2.25 3.45 1.29 2.33 3.1</performanceDuration>