searching for the nearest ancestor
Wednesday, 25 May 2016 · 4 messages · last reply 26 May 2016
RS
Rob Stapper25 May 2016 16:23 #
Hi,
While processing a XML-file with xquery I need to find the nearest ancestor that has a child-element with a certain ID-value( $refID).
I found this solution [1] but I was wondering if also a solution is possible that makes use of the ancestor being a reverse-axis[ 2].
That way I could replace the [ last()]-predicate with a [1]-predicate, which I find more transparant. Also my gut-feeling tells me that would be a more efficient solution then the current one.
[ 1] ( $element/ancestor::*/child::*[ @ID = $refID] ) [ last()]
[ 2] https://www.w3.org/TR/xquery-30/#id-predicate
TIA for your replies,
Take care,
Rob Stapper
CG
Christian Grün25 May 2016 16:40 #
Hi Rob,
Quoted text · Rob Stapper · 2 lines
> I found this solution [1] but I was wondering if also a solution is possible > that makes use of the ancestor being a reverse-axis[ 2].
[ 1] ( $element/ancestor::*/child::*[ @ID = $refID] ) [ last()]
You probably need two id tests:
$element/ancestor::*[*/@ID = $refID][1]/*[@ID = $refID]I guess it depends mostly on the input if the first or second alternative will be faster.
Hope this helps,
Christian
RS
Rob Stapper26 May 2016 08:49 #
Hi Christian,
I've been thinking about that solution too.
Thanx for your reply,
Rob
-----Oorspronkelijk bericht-----
Quoted text · Christian Grün · 14 lines
Van: Christian Grün [mailto:christian.gruen at gmail.com] Verzonden: woensdag 25 mei 2016 16:40 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] searching for the nearest ancestor Hi Rob, > I found this solution [1] but I was wondering if also a solution is > possible that makes use of the ancestor being a reverse-axis[ 2]. [ 1] ( $element/ancestor::*/child::*[ @ID = $refID] ) [ last()] You probably need two id tests: $element/ancestor::*[*/@ID = $refID][1]/*[@ID = $refID] I guess it depends mostly on the input if the first or second alternative will be faster. Hope this helps, Christian
RS
PS. I opted for your version because it's consistent with the
map:contains, map:get functions.
-----Oorspronkelijk bericht-----Quoted text · Christian Grün · 14 lines
Van: Christian Grün [mailto:christian.gruen at gmail.com] Verzonden: woensdag 25 mei 2016 16:40 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] searching for the nearest ancestor Hi Rob, > I found this solution [1] but I was wondering if also a solution is > possible that makes use of the ancestor being a reverse-axis[ 2]. [ 1] ( $element/ancestor::*/child::*[ @ID = $refID] ) [ last()] You probably need two id tests: $element/ancestor::*[*/@ID = $refID][1]/*[@ID = $refID] I guess it depends mostly on the input if the first or second alternative will be faster. Hope this helps, Christian
Written for BaseX 8.4. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery