Please help me with the following problem:

I try to construct the current status of a series of objects along the following simple rules:
I use a fold-left to proces these rules with a simple function implementing those rules. This gives me unexpected results.

I've reduced the problem to a single stand alone query in which the last object (
`obj3`) has the property `<status>beëindigen</status>`.

So I would expect this line:
    `let $stand4 := fold-left(($obj3, $obj2, $obj1), (), function($stand, $obj) { local:stand-totaal($stand, $obj) })`
to give me an empty result as `
obj3` has the property <status>beëindigen</status>
but I am getting
`obj1` as demonstrated in the following stand alone query.

When running the stand alone query I would expect:

  2 <-> 1 as shown in the result (`
obj2`)
  3 <-> 1 as shown in the result (empty as
`obj3` has the property `<status>beëindigen</status>`)
  3 <-> 2 as shown in the result (empty as
`obj3` has the property `<status>beëindigen</status>`)

  3 <-> 2 <-> 1 as 3 <-> 2 and 3 <-> 1 (empty as `
obj3` has the property `<status>beëindigen</status>`) but in the result I am getting `obj1`.

What am I doing wrong?
Any help greatly appreciated.


The query I use:

```
      
declare namespace op="http://www.geostandaarden.nl/imow/opobject";
    declare namespace da="http://www.geostandaarden.nl/imow/datatypenalgemeen";
    declare namespace ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing";
    declare namespace gml="http://www.opengis.net/gml/3.2";
    declare namespace l="http://www.geostandaarden.nl/imow/locatie";
    declare namespace ow="http://www.geostandaarden.nl/imow/owobject";
    declare namespace ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand";
    declare namespace r="http://www.geostandaarden.nl/imow/regels" ;
    declare namespace rol="http://www.geostandaarden.nl/imow/regelsoplocatie" ;
    declare namespace sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek" ;
    declare namespace xlink="http://www.w3.org/1999/xlink" ;
    declare namespace xsi="http://www.w3.org/2001/XMLSchema-instance";
    declare namespace rg="http://www.geostandaarden.nl/imow/regelingsgebied";

    declare function local:id
      (: returns the id(s) of ow-object(s) :)
      ($seq as item()*) as item()* {
           $seq//*:owObject/*/*:identificatie | $seq/*/*:identificatie
    };

    declare function local:stand-totaal
      (: returns an object by comparing new to old :)
      (: newest should be returned, except when
         <ow:status>beëindigen</ow:status>
         then no object should be returned :)
      ($new as item()*,
       $old as item()*) as item()* {
         
       let $new_ids := local:id($new)
       let $stand_deel1 :=
         for $obj_oud in $old//*:owObject
           where not(local:id($obj_oud) = $new_ids)
             return <sl:stand>{ $obj_oud }</sl:stand>
       let $stand_deel2 :=  
         for $obj_new in $new//*:owObject[not(.//*:status)]
            return <sl:stand>{ $obj_new }</sl:stand>
           
       return ($stand_deel1, $stand_deel2)
    };

    let $obj1 := <sl:stand>
    <ow-dc:owObject xmlns:ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand" xmlns:sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <r:RegelVoorIedereen xmlns:r="http://www.geostandaarden.nl/imow/regels">
      <r:identificatie>nl.imow-mnre1034.juridischeregel.OR0000000027</r:identificatie>
      <r:idealisatie>http://standaarden.omgevingswet.overheid.nl/idealisatie/id/concept/Exact</r:idealisatie>
      <r:artikelOfLid>
        <r:RegeltekstRef xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.regeltekst.OR0000000027"/>
      </r:artikelOfLid>
      <r:thema>http://standaarden.omgevingswet.overheid.nl/thema/id/concept/WaterEnWatersystemen</r:thema>
      <r:locatieaanduiding>
        <l:LocatieRef xmlns:l="http://www.geostandaarden.nl/imow/locatie" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.ambtsgebied.LND6030B"/>
      </r:locatieaanduiding>
    </r:RegelVoorIedereen>
    </ow-dc:owObject></sl:stand>
         
    let $obj2 := <sl:stand>
    <ow-dc:owObject xmlns:ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand" xmlns:sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <r:Instructieregel xmlns:r="http://www.geostandaarden.nl/imow/regels">
      <r:identificatie>nl.imow-mnre1034.juridischeregel.OR0000000027</r:identificatie>
      <r:idealisatie>http://standaarden.omgevingswet.overheid.nl/idealisatie/id/concept/Exact</r:idealisatie>
      <r:artikelOfLid>
        <r:RegeltekstRef xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.regeltekst.OR0000000027"/>
      </r:artikelOfLid>
      <r:thema>http://standaarden.omgevingswet.overheid.nl/thema/id/concept/WaterEnWatersystemen</r:thema>
      <r:locatieaanduiding>
        <l:LocatieRef xmlns:l="http://www.geostandaarden.nl/imow/locatie" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebied.Kustfundament"/>
      </r:locatieaanduiding>
      <r:gebiedsaanwijzing>
        <ga:GebiedsaanwijzingRef xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.Kustfundament"/>
        <ga:GebiedsaanwijzingRef xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.RGKustfundament"/>
      </r:gebiedsaanwijzing>
      <r:instructieregelInstrument>http://standaarden.omgevingswet.overheid.nl/instrument/id/concept/Omgevingsplan</r:instructieregelInstrument>
    </r:Instructieregel>
    </ow-dc:owObject></sl:stand>

    let $obj3 := <sl:stand>
    <ow-dc:owObject xmlns:ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand" xmlns:sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <r:Instructieregel xmlns:r="http://www.geostandaarden.nl/imow/regels">
      <ow:status xmlns:ow="http://www.geostandaarden.nl/imow/owobject">beëindigen</ow:status>
      <r:identificatie>nl.imow-mnre1034.juridischeregel.OR0000000027</r:identificatie>
      <r:idealisatie>http://standaarden.omgevingswet.overheid.nl/idealisatie/id/concept/Exact</r:idealisatie>
      <r:artikelOfLid>
        <r:RegeltekstRef xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.regeltekst.OR0000000027"/>
      </r:artikelOfLid>
      <r:thema>http://standaarden.omgevingswet.overheid.nl/thema/id/concept/WaterEnWatersystemen</r:thema>
      <r:locatieaanduiding>
        <l:LocatieRef xmlns:l="http://www.geostandaarden.nl/imow/locatie" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebied.Kustfundament"/>
      </r:locatieaanduiding>
      <r:gebiedsaanwijzing>
        <ga:GebiedsaanwijzingRef xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.Kustfundament"/>
        <ga:GebiedsaanwijzingRef xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.RGKustfundament"/>
      </r:gebiedsaanwijzing>
      <r:instructieregelInstrument>http://standaarden.omgevingswet.overheid.nl/instrument/id/concept/Omgevingsplan</r:instructieregelInstrument>
    </r:Instructieregel>
    </ow-dc:owObject></sl:stand>
         
    let $stand1 := fold-left(($obj2, $obj1), (), function($stand, $obj) { local:stand-totaal($stand, $obj) })  
    let $stand2 := fold-left(($obj3, $obj1), (), function($stand, $obj) { local:stand-totaal($stand, $obj) })
    let $stand3 := fold-left(($obj3, $obj2), (), function($stand, $obj) { local:stand-totaal($stand, $obj) })
    let $stand4 := fold-left(($obj3, $obj2, $obj1), (), function($stand, $obj) { local:stand-totaal($stand, $obj) })

    return (    '2 <-> 1 ==================', $stand1,
              '3 <-> 1 ==================', $stand2,
              '3 <-> 2 ==================', $stand3,
              '3 <-> 2 <-> 1 ============', $stand4
           )
```     

The results:

```
   
2 <-> 1 ==================
    <sl:stand xmlns:sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek">
      <ow-dc:owObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand">
        <r:Instructieregel xmlns:r="http://www.geostandaarden.nl/imow/regels">
          <r:identificatie>nl.imow-mnre1034.juridischeregel.OR0000000027</r:identificatie>
          <r:idealisatie>http://standaarden.omgevingswet.overheid.nl/idealisatie/id/concept/Exact</r:idealisatie>
          <r:artikelOfLid>
            <r:RegeltekstRef xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.regeltekst.OR0000000027"/>
          </r:artikelOfLid>
          <r:thema>http://standaarden.omgevingswet.overheid.nl/thema/id/concept/WaterEnWatersystemen</r:thema>
          <r:locatieaanduiding>
            <l:LocatieRef xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:l="http://www.geostandaarden.nl/imow/locatie" xlink:href="nl.imow-mnre1034.gebied.Kustfundament"/>
          </r:locatieaanduiding>
          <r:gebiedsaanwijzing>
            <ga:GebiedsaanwijzingRef xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.Kustfundament"/>
            <ga:GebiedsaanwijzingRef xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ga="http://www.geostandaarden.nl/imow/gebiedsaanwijzing" xlink:href="nl.imow-mnre1034.gebiedsaanwijzing.RGKustfundament"/>
          </r:gebiedsaanwijzing>
          <r:instructieregelInstrument>http://standaarden.omgevingswet.overheid.nl/instrument/id/concept/Omgevingsplan</r:instructieregelInstrument>
        </r:Instructieregel>
      </ow-dc:owObject>
    </sl:stand>
    3 <-> 1 ==================
    3 <-> 2 ==================
    3 <-> 2 <-> 1 ============
    <sl:stand xmlns:sl="http://www.geostandaarden.nl/bestanden-ow/standlevering-generiek">
      <ow-dc:owObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ow-dc="http://www.geostandaarden.nl/imow/bestanden/deelbestand">
        <r:RegelVoorIedereen xmlns:r="http://www.geostandaarden.nl/imow/regels">
          <r:identificatie>nl.imow-mnre1034.juridischeregel.OR0000000027</r:identificatie>
          <r:idealisatie>http://standaarden.omgevingswet.overheid.nl/idealisatie/id/concept/Exact</r:idealisatie>
          <r:artikelOfLid>
            <r:RegeltekstRef xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="nl.imow-mnre1034.regeltekst.OR0000000027"/>
          </r:artikelOfLid>
          <r:thema>http://standaarden.omgevingswet.overheid.nl/thema/id/concept/WaterEnWatersystemen</r:thema>
          <r:locatieaanduiding>
            <l:LocatieRef xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:l="http://www.geostandaarden.nl/imow/locatie" xlink:href="nl.imow-mnre1034.ambtsgebied.LND6030B"/>
          </r:locatieaanduiding>
        </r:RegelVoorIedereen>
      </ow-dc:owObject>
    </sl:stand>
```


-- 
Marco Duiker
LandGoed
Technisch directeur
+31617115114