Tumeco
Tumeco

Reputation: 115

Let/While doesn't work in XQuery

I made this XQuery query but it doesn't work, and I don't know why. My XML is like this :

    <entries xmlns="http://ref.otcnice.com/webservice/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://ref.otcnice.com/webservice/entries.xsd">
        <entry>
             <ID>1021</ID>
             <name_fr>AC BY MARRIOTT NICE</name_fr>
             <standings_levels>
                <standings_level>1 étoiles</standings_level>
             </standings_levels>
             <tariffs>
                <tariff>
                    <name>Chambre simple</name>
                    <min>120</min>
                </tariff>
             </tariffs>
      </entry>
    </entries>

And I want to make an avg on 'min' because I have a lot of other entry like this, so I made this :

let $hotel := doc('data/entries_hotels.xml')/entries/entry where $hotel/standings_levels/standings_level = '1 étoile' 
return 
let $prixUneEtoile :=  avg($hotel/tariffs/tariff/min) 
return
<data>
<y>{$prixUneEtoile}</y>
</data>

The problem is the While doesn't work, and the avg is made on all entries and not only entries with '1 étoile' I have tried this : let $hotel := doc('data/entries_hotels.xml')/entries/entry where $hotel/standings_levels/standings_level[text() eq '1 étoiles'] and the same problem appears. I don't understand where is the problem. Any Help? Thanks

Upvotes: 0

Views: 186

Answers (2)

wcandillon
wcandillon

Reputation: 2176

There are many mistakes in this query. Here's a working version:

declare namespace e = "http://ref.otcnice.com/webservice/";

let $entries := <entries xmlns="http://ref.otcnice.com/webservice/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://ref.otcnice.com/webservice/entries.xsd">
        <entry>
             <ID>1021</ID>
             <name_fr>AC BY MARRIOTT NICE</name_fr>
             <standings_levels>
                <standings_level>1 étoiles</standings_level>
             </standings_levels>
             <tariffs>
                <tariff>
                    <name>Chambre simple</name>
                    <min>120</min>
                </tariff>
             </tariffs>
      </entry>
</entries>
let $hotel := $entries/e:entry[e:standings_levels/e:standings_level = '1 étoiles']
let $prixUneEtoile :=  avg($hotel/e:tariffs/e:tariff/e:min) 
return
<data>
<y>{$prixUneEtoile}</y>
</data>

You can play with it at http://try.zorba.io/queries/xquery/hzdl%2F7TkVt6nErwmYTqCcyb10MY%3D

Upvotes: 2

Josh
Josh

Reputation: 513

The first problem is that the elements you are querying are in a namespace but your path expressions aren't using this namespace. One solution is to change the default element namespace. You can add this at the beginning of your query:

declare default element namespace "http://ref.otcnice.com/webservice/";

The second problem is that you need to change '1 étoile' to '1 étoiles' in your query.

Upvotes: 1

Related Questions