我之前从未做过XQuery,我需要使用变量而不是硬编码来编写语句,因为这是一个实时XML提要。 (抱歉这样一个基本问题)
每个活动都有一个名称,一些地方,以及一些可用的空位。我不想在4和11中进行硬编码,而是希望将这些变量作为特定事件的变量。
活动是/ event / name =" Rock concert"我想要
nb_places = that of "Rock Concert" and nb_empty_places >= that of "Rock Concert"
这是我到目前为止所做的:
<bib>
{
for $n in doc("http://tinyurl.com/")/event
where $n/nb_places = 4 and $n/nb_empty_places >= 11
return
<result>
{ $n/nb_empty_places }
{ $n/nb_places }
{ $n/name }
</result>
}
</bib>
谢谢:)
示例XML:
<event>
<name>Rock Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>11</nb_empty_places>
</event>
<event>
<name>Indie Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>9</nb_empty_places>
</event>
<event>
<name>RnB Concert</name>
<nb_places>2</nb_places>
<nb_empty_places>14</nb_empty_places>
</event>
<event>
<name>Pop Concert</name>
<nb_places>3</nb_places>
<nb_empty_places>32</nb_empty_places>
</event>
<event>
<name>House Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>20</nb_empty_places>
</event>
期望的结果,具有相同数字的事件将作为摇滚音乐会,以及相同或更多数量的空位:
<result>
<nb_empty_places>32</nb_empty_places>
<nb_places>4</nb_places>
<name>Rock Concert</name>
</result>
<result>
<nb_empty_places>20</nb_empty_places>
<nb_places>4</nb_places>
<name>House Concert</name>
</result>
答案 0 :(得分:1)
这是一种可能的方式:
declare variable $nb_places := root/event[name='Rock Concert']/nb_places;
declare variable $nb_empty_places := root/event[name='Rock Concert']/nb_empty_places;
<bib>
{
for $n in root/event
where $n/nb_places = $nb_places and $n/nb_empty_places/number() >= $nb_empty_places
return
<result>
{ $n/nb_empty_places }
{ $n/nb_places }
{ $n/name }
</result>
}
</bib>
演示:xpathtester