Reputation: 11
<base>
<restaurant nom="la tour d'argent" etoile="3" ville="Paris">
<fermeture>dimanche et lundi</fermeture>
<menu nom="buffet" prix="200"/>
<menu nom="gourmet" prix="300"/>
</restaurant>
<ville nom="Paris" departement="75">
<plusBeauMonument nom="tour Eiffel" tarif="30"/>
</ville>
</base>
I am using XQuery to do some search for this xml file.
I want to display the "nom" and the "departement" together. For example, for this xml file above, I want to show like this:
<restaurant nom="la tour d'argent" departement="75"/>
As you see, the "ville" of "la tour d'argent" is "Paris". If there is a whose "nom" is "Paris" too, we will take its "departement" and display it with the "nom" of restaurant.
Here is my xquery code:
for $r in //restaurant
for $v in //ville
return if($v/@nom=$r/@ville)
then <restaurant nom="{data($r/@nom)}" departement="{data($v/@departement)}"/>
else()
But this code gives me a result below:
<restaurant departement="75" nom="la tour d'argent"/>
you see, the order is not what I want. I want to make sure that "nom" is shown first, however the "departement" is first now.
Could you help me?
Upvotes: 1
Views: 170
Reputation: 2514
It's counter intuitive, however when I switch the attributes in your then statement, I get the correct result.
for $r in //restaurant
for $v in //ville
return if($v/@nom=$r/@ville)
then <restaurant departement="{data($v/@departement)}" nom="{data($r/@nom)}" />
else()
Upvotes: 0