Reputation: 359
I have never done XQuery before and I need to write a statement using a variable instead of hard-coding in values as this is a live XML feed. (Sorry for such a basic question)
Each event has a name, and a number of places taken, and a number of empty places that are available. Instead of hard-coding in 4 and 11, I want to make these a variable which is that of a specific event.
The event is /event/name = "Rock concert" and I want
nb_places = that of "Rock Concert" and nb_empty_places >= that of "Rock Concert"
This is what I have so far:
<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>
Thank you :)
Example 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>
Desired result, events with the same number places as Rock Concert, and the same or more number of empty places:
<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>
Upvotes: 1
Views: 33
Reputation: 89285
This is one possible way :
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>
Demo : xpathtester
Upvotes: 1