Search code examples
xmlxquerybasex

Filtering based on an attribute in deep level


Running Xquery on BaseX.

I want to filter based on an attribute in a deep level (/BLA1/BLA2/BLA3) but return attribute from both the filtering level and from one above it (/BLA1/BLA2).

So, as far as I understand, my "for" must run on the upper level (/BLA1/BLA2) in order to allow it. But I can't get the filtering to work.. when I try running the "for" on the deeper level - it does work but then I can't return the attribute I need from the upper level.

This one works: I get the required results as per the filtering.

for $i in /BLA1/BLA2/BLA3
where $i/@AT1>1000 and $i/@AT2='XX'
return $i/data(@AT3)

This one doesn't work, meaning the filter is not filtering and I get results in which $i/BLA3/@AT2 is NOT XX.

for $i in /BLA1/BLA2
where $i/BLA3/@AT1>1000 and $i/BLA3/@AT2='XX'
return $i/BLA3/data(@AT3)

I don't understand WHY the second one doesn't work...

This is what I actually want to use, i.e. return one attribute from the upper level (/BLA1/BLA2) and another attribute from the filtering level (/BLA1/BLA2/BLA3)

for $i in /BLA1/BLA2
where $i/BLA3/@AT1>1000 and $i/BLA3/@AT2='XX'
return $i/data(@AT4) || ' ' || $i/BLA3/data(@AT3)

Any help will be appreciated!


Solution

  • Use .. to navigate up e.g.

    for $i in /BLA1/BLA2/BLA3
    where $i/@AT1>1000 and $i/@AT2='XX'
    return $i/../data(@AT4) || ' ' || $i/data(@AT3)