0

I have been doing a homework exercise in Xquery and I am currently stuck. The assignment was to generate infromation in regards to the two continents, that after 50 years would have the largest respectively smallest population increase. All I have left is to take the min and max, it is all saved into $minAndMaxCont and it looks like this:

<Continent name="asia" pop="4243769598" futurePop="7255593125" increase="3011823527" ratio="1.709704770122159681"/>
<Continent name="africa" pop="1043912572" futurePop="3405022718" increase="2361110146" ratio="3.261789166382412339"/>
<Continent name="america" pop="955621605" futurePop="1510928928" increase="555307323" ratio="1.581095404388643976"/>
<Continent name="australia" pop="93146473" futurePop="156995765" increase="63849292" ratio="1.685471923343785653"/>
<Continent name="europe" pop="633227105" futurePop="693248396" increase="60021291" ratio="1.094786357889717939"/>

So what I want to do is to extract the minumum and maximum value in regards to "increase" which seems simple enough. But I do not seem to get it to work, I have tried a lot of different approaches, one such approach being using the max and min functions by looking at other threads as guides.

One thread that I followed was this one: How can I use XPath to find the minimum value of an attribute in a set of elements?

From there I took this code:

let $xml := <foo>
            <bar id="1" score="192" />
            <bar id="2" score="227" />
            <bar id="3" score="105" />
            </foo>
let $min := min($xml/bar/@id)
let $max := max($xml/bar/@id)
return $max

And this works perfectly fine, it will return the min/max value (I can use both Xquery and Datapath solutions btw). However, when I attempt to do something similar inside of my own collection of data, like this:

let $incMin    := min($minAndMaxCont/@increase)
return $incMin 

The generated result becomes this (It behaves the same way with max() too):

3.011823527E9
2.361110146E9
5.55307323E8
6.3849292E7
6.0021291E7

So instead of extracting a minumum (or maximum) value it converts the whole list into another form and does nothing with it. I really want to get this to work, and also I am genuinely curious as to why it converts the entries into another form instead of extracting the max value. I would very much appreciate any help.

//With kind regards.

Augman
  • 61
  • 4
  • 2
    I don't see how that sole `let $incMin := min($minAndMaxCont/@increase) return $incMin` can return various values, you must have some `for` expression involved. See https://stackoverflow.com/questions/49842477/xquery-using-max-and-min for a minimal but complete sample with your input data and the `min`/`max` computations (I used `xs:decimal` explicitly to avoid the exponential notation caused by `xs:double`). So show us a minimal but complete sample of XML and XQuery to allow us to reproduce the problem. – Martin Honnen Apr 15 '18 at 15:41
  • It's not useful to tell us that you tried lots of things and none of them worked. What you need to do is show us one thing that you tried -- in its entirety, so we can try it too -- and explain how the output differed from expectations/requirements. Your fragment of code appears to be incomplete; from the results shown, I don't think it's your whole query. – Michael Kay Apr 16 '18 at 09:11

0 Answers0