Hello all, sorry this email is not properly basex specific but rather XQuery related. What is the most correct, most efficient in general best way of treating sequences sequentially? In particular I have an XML describing a sequence of set operations that have to be applied sequentially to the contained sequences of <tem/>s resulting into a final set represented by a sequence of <item/>s. Unfortunately they are expressed as siblings (flat) rather than dependent nodes (nested hierarchy) which could be handled easily with recursion. As an example the following sequence shall add the first three items to the empty set then cut down the resulting set to only contain item 2 and 3 and finally remove item 2 from the set resulting in a final set containing only item 3.
<set operator="UNION"> <item name="1"/> <item name="2"/> <item name="3"/> </set> <set operator="INTERSECT"> <item name="2"/> <item name="3"/> <item name="4"/> <item name="5"/> </set> <set operator="DIFFERENCE"> <item name="2"/> </set>
I'd prefer to not have to alter the input data structure. Thanks a lot for any help, M.
hi Marco,
using recursion is easily possible (and the only way to solve that problem without using maps), just write a small XQuery function like
declare function local:interprete($operations as item()*, $set as item()*) as item()* { let $op := head($operations) let $ops := tail($operations) let $intermediate := switch($op/@operator) case "UNION" return ($set, $op/item[not(@name = $set/@name)]) case "INTERSECT" return $set[@name = $op/item/@name] case "DIFFERENCE" return $set[not(@name = $op/item/@name)] default return error() return if ($ops) then local:interprete($ops, $intermediate) else $intermediate };
Which you would call using `local:interpret(//set, ())`. I guess the code should be small enough to be self-explaining. I seems to work, but I didn't test with much more than the input you provided, better do some additional tests on production data before using it.
If you want to compare whole nodes instead of their name attribute, use `fn:deep-equal()` instead of the name comparisons. You will have to use some explicit loop, [quantified expressions] should get handy for that.
Regards from Lake Constance, Germany, Jens Erat
[quantified expressions]: http://www.w3.org/TR/xpath20/#id-quantified-expressions
Thanks a lot. I'm really wondering how I was able to overlook "head" and "tail" .... maybe the age .... :-(
M.
On 05/20/2013 03:44 PM, Jens Erat wrote:
hi Marco,
using recursion is easily possible (and the only way to solve that problem without using maps), just write a small XQuery function like
declare function local:interprete($operations as item()*, $set as item()*) as item()* { let $op := head($operations) let $ops := tail($operations) let $intermediate := switch($op/@operator) case "UNION" return ($set, $op/item[not(@name = $set/@name)]) case "INTERSECT" return $set[@name = $op/item/@name] case "DIFFERENCE" return $set[not(@name = $op/item/@name)] default return error() return if ($ops) then local:interprete($ops, $intermediate) else $intermediate };
Which you would call using `local:interpret(//set, ())`. I guess the code should be small enough to be self-explaining. I seems to work, but I didn't test with much more than the input you provided, better do some additional tests on production data before using it.
If you want to compare whole nodes instead of their name attribute, use `fn:deep-equal()` instead of the name comparisons. You will have to use some explicit loop, [quantified expressions] should get handy for that.
Regards from Lake Constance, Germany, Jens Erat
You don't really need them, they're just syntactical sugar making the code more readable for everybody familiar with functional programming.
- `head($sequence)` could be written as `$sequence[1]` - `tail($sequence)` could be written as `$sequence[position()>1]`
Yep! Of course I meant it conceptually! (ROFL). Luckily not that burned out yet! :-D Thanks again. M.
On 05/20/2013 04:35 PM, Jens Erat wrote:
You don't really need them, they're just syntactical sugar making the code more readable for everybody familiar with functional programming.
- `head($sequence)` could be written as `$sequence[1]`
- `tail($sequence)` could be written as `$sequence[position()>1]`
basex-talk@mailman.uni-konstanz.de