XQuery - 如何 return XML 元素列表
XQuery - how to return a list of XML elements
我正在做一个关于最近的橄榄球世界杯的 XML/XQuery 项目。我一直致力于设计一个 XQuery,它将 return 每个球队参加过的体育场的列表。
我的 XQuery 如下所示:
for $a in doc("squads.xml")/tournament/squad,
$b in doc("match.xml")/tournament/match
where (($a/@country = $b/team1) or ($a/country = $b/team2))
return <team country ="{$a/@country}">
<stadia>
<stadium>{data($b/hostedBy)}</stadium>
</stadia>
</team>
并给出输出:
<team country="Argentina">
<stadia>
<stadium>Twickenham</stadium>
</stadia>
</team
依此类推,其中每个体育场都列在单独的团队元素下。
理想情况下,我希望输出以列表形式生成体育场,如下所示:
<team country = "Argentina">
<stadia>
<stadium>Twikenham</stadium>
<stadium>Leicester City Stadium</stadium>
<stadium>Kingsholm Stadium</stadium>
</stadia>
</team>
有没有简单的方法 return $b/hostedBy 就像显示的列表一样?
squads.xml的样本:
<tournament>
<squad country = "Argentina">
<tier>1</tier>
<result>4th</result>
<games_played>7</games_played>
...
</squad>
...
</tournament>
样本 match.xml:
<tournament>
<match>
<matchNumber>8</matchNumber>
<pool>C</pool>
<team1>New Zealand</team1>
<team2>Argentina</team2>
<attendance>89,019</attendance>
<hostedBy>Wembley Stadium</hostedBy>
</match>
...
</tournament>
这就是您使用 for $x in ..., $y in ...
时发生的情况 - 它本质上是将两个序列相乘。这常常使人们从 SQL 转向 XQuery。你想要的是分组,你可以简单地添加一个嵌套循环,从外循环加入团队值:
for $squad in doc("squads.xml")/tournament/squad
return
<team country ="{$squad/@country}">
<stadia>{
for $match in doc("match.xml")/tournament/match
where ($match/(team1 | team2) = $squad/@country)
return <stadium>{ $match/hostedBy/string() }</stadium>
}</stadia>
</team>
如果需要对体育场馆进行去重,可以使用distinct-values
:
for $squad in doc("squads.xml")/tournament/squad
let $hosted-by :=
for $match in doc("match.xml")/tournament/match
where ($match/(team1 | team2) = $squad/@country)
return $match/hostedBy/string()
return
<team country ="{$a/@country}">
<stadia>{
for $stadium in distinct-values($hosted-by)
return <stadium>{ $stadium }</stadium>
}</stadia>
</team>
我正在做一个关于最近的橄榄球世界杯的 XML/XQuery 项目。我一直致力于设计一个 XQuery,它将 return 每个球队参加过的体育场的列表。
我的 XQuery 如下所示:
for $a in doc("squads.xml")/tournament/squad,
$b in doc("match.xml")/tournament/match
where (($a/@country = $b/team1) or ($a/country = $b/team2))
return <team country ="{$a/@country}">
<stadia>
<stadium>{data($b/hostedBy)}</stadium>
</stadia>
</team>
并给出输出:
<team country="Argentina">
<stadia>
<stadium>Twickenham</stadium>
</stadia>
</team
依此类推,其中每个体育场都列在单独的团队元素下。
理想情况下,我希望输出以列表形式生成体育场,如下所示:
<team country = "Argentina">
<stadia>
<stadium>Twikenham</stadium>
<stadium>Leicester City Stadium</stadium>
<stadium>Kingsholm Stadium</stadium>
</stadia>
</team>
有没有简单的方法 return $b/hostedBy 就像显示的列表一样?
squads.xml的样本:
<tournament>
<squad country = "Argentina">
<tier>1</tier>
<result>4th</result>
<games_played>7</games_played>
...
</squad>
...
</tournament>
样本 match.xml:
<tournament>
<match>
<matchNumber>8</matchNumber>
<pool>C</pool>
<team1>New Zealand</team1>
<team2>Argentina</team2>
<attendance>89,019</attendance>
<hostedBy>Wembley Stadium</hostedBy>
</match>
...
</tournament>
这就是您使用 for $x in ..., $y in ...
时发生的情况 - 它本质上是将两个序列相乘。这常常使人们从 SQL 转向 XQuery。你想要的是分组,你可以简单地添加一个嵌套循环,从外循环加入团队值:
for $squad in doc("squads.xml")/tournament/squad
return
<team country ="{$squad/@country}">
<stadia>{
for $match in doc("match.xml")/tournament/match
where ($match/(team1 | team2) = $squad/@country)
return <stadium>{ $match/hostedBy/string() }</stadium>
}</stadia>
</team>
如果需要对体育场馆进行去重,可以使用distinct-values
:
for $squad in doc("squads.xml")/tournament/squad
let $hosted-by :=
for $match in doc("match.xml")/tournament/match
where ($match/(team1 | team2) = $squad/@country)
return $match/hostedBy/string()
return
<team country ="{$a/@country}">
<stadia>{
for $stadium in distinct-values($hosted-by)
return <stadium>{ $stadium }</stadium>
}</stadia>
</team>