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
--
Jens Erat
[phone]: tel:+49-151-56961126
[mail]: mailto:email@jenserat.de
[jabber]: xmpp:jabber@jenserat.de
[web]:
http://www.jenserat.de
PGP: 350E D9B6 9ADC 2DED F5F2 8549 CBC2 613C D745 722B
Am 20.05.2013 um 14:51 schrieb Marco Lettere
m.lettere@websynapsis.com:
> 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.
> _______________________________________________
> BaseX-Talk mailing list
> BaseX-Talk@mailman.uni-konstanz.de
>
https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk