Xquery Values

I get my desired circuit. But the only thing I need to do is eliminate duplicate values. Therefore, I use different values, but what this function does is it displays all the values ​​inside one element.

Input Scheme:

<?xml version="1.0" encoding="UTF-8"?> <hotel> <food> <name>Burger</name> <cost>20</cost> </food> <food> <name>Pizza</name> <cost>20</cost> </food> <food> <name>Cola</name> <cost>5</cost> </food> <food> <name>Lemonade</name> <cost>5</cost> </food> <food> <name>Ice Cream</name> <cost>10</cost> </food> <food> <name>Cookies</name> <cost>10</cost> </food> </hotel> 

Conclusion currently:

 <type> <cost>01</cost> <food>burger fries cola</food> </type> 

Desired:

 <type> <cost>01</cost> <food>burger</food> <food>fries</food> <food>cola</food> </type> 

Basically, get all the food at this price.

My Xquery looks like this:

 let $cost:= doc("Untitled7.xml")/hotel/food/cost for $unique in distinct-values(doc("Untitled7.xml")/hotel/food/cost) let $food:= distinct-values(doc("Untitled7.xml")hotel/food[cost=$unique]/name) where $unique = $cost return <type> <year>{$unique}</year> <food>{$food}</food> </type> 

Any help would be appreciated :)

+6
source share
1 answer

distinct-values ​​() will return a sequence of strings: ('burger', 'fries', 'cola'), but now you are outputting it immediately. What you need to do is iterate over your $ food variable in another FLWOR statement:

 return <type> <year>{$unique}</year> { for $f in $food return <food>{$f}</food> } </type> 
+4
source

Source: https://habr.com/ru/post/957228/


All Articles