Hi Chistian,
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
Unfortunately it only returns a bunch of: 1.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
TIA for your reply,
Rob Stapper
Hello Rob,
sorry, but Christian is currently on a well-deserved vacation so you will have to either wait or I (and maybe others) try as good as we can to help as well.
However, I didn't really get what you want to achieve. You say you want to get "positions of each node amongst its siblings", but in your query you access ancestors? Maybe you could give a small example with a small example data set and your expected output.
Cheers, Dirk
On 05/17/2016 03:51 PM, Rob Stapper wrote:
Hi Chistian,
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
Unfortunately it only returns a bunch of: 1.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
TIA for your reply,
Rob Stapper
Hi Rob,
maybe you think of something like the following function:
declare function local:getInfo( $anyelement as item() ){ if ($anyelement instance of element()) then string-join( for $e in $anyelement/ancestor-or-self::* return if (exists($e/@*)) then concat( name($e), string-join( for $attr in $e/@* return "[@" || $attr/name() || "=" || '''' || $attr/string() || '''' || "]", '' ) ) else if ($e/preceding-sibling::*[name(.) = name($e)]) then let $pos := count($e/preceding-sibling::*[name(.) = name($e)]) + 1 return name($e) || "[" || $pos || "]" else if ($e/following-sibling::*[name(.) = name($e)]) then name($e) || "[1]" else name($e), '/' ) else () };
Christoph
Am 17.05.2016 um 15:51 schrieb Rob Stapper:
Hi Chistian,
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
Unfortunately it only returns a bunch of: 1.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
TIA for your reply,
Rob Stapper
If you want the path to the context node from the document root, there's this handy path() function which includes position values.
If that's not what you want I'm confused.
-- Graydon
On Tue, May 17, 2016, 10:14 Christoph Gaukel christoph.gaukel@gmx.de wrote:
Hi Rob,
maybe you think of something like the following function:
declare function local:getInfo( $anyelement as item() ){ if ($anyelement instance of element()) then string-join( for $e in $anyelement/ancestor-or-self::* return if (exists($e/@*)) then concat( name($e), string-join( for $attr in $e/@* return "[@" || $attr/name() || "=" || '''' || $attr/string() || '''' || "]", '' ) ) else if ($e/preceding-sibling::*[name(.) = name($e)]) then let $pos := count($e/preceding-sibling::*[name(.) = name($e)])
- 1 return name($e) || "[" || $pos || "]" else if ($e/following-sibling::*[name(.) = name($e)]) then name($e) || "[1]" else name($e), '/' ) else ()
};
Christoph
Am 17.05.2016 um 15:51 schrieb Rob Stapper:
Hi Chistian,
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
Unfortunately it only returns a bunch of: 1.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
TIA for your reply,
Rob Stapper
Hi Rob,
Am 17.05.2016 um 15:51 schrieb Rob Stapper:
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
no, that this does not work because the `fn:position()` function returns the position of the context item in the innermost path expression, which in your case is `./fn:position()`.
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
Graydon already mentioned the `fn:path($node)` function, which returns a valid XPath expression that uniquely identifies the given node. If you just want to get the position of each ancestor inside its parent, you can simply count all its preceding siblings and add one:
$element/ancestor-or-self::*/(count(preceding-sibling::*) + 1)
Hope that helps, Leo
Thanx all you guys,
Great to see this much response.
fn:path() is exactly what I needed. I do feel a little awkward though that I missed this one.
Cheers, Rob
-----Oorspronkelijk bericht----- Van: Leonard Wörteler [mailto:leonard.woerteler@uni-konstanz.de] Verzonden: dinsdag 17 mei 2016 16:34 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] question
Hi Rob,
Am 17.05.2016 um 15:51 schrieb Rob Stapper:
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
no, that this does not work because the `fn:position()` function returns the position of the context item in the innermost path expression, which in your case is `./fn:position()`.
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
Graydon already mentioned the `fn:path($node)` function, which returns a valid XPath expression that uniquely identifies the given node. If you just want to get the position of each ancestor inside its parent, you can simply count all its preceding siblings and add one:
$element/ancestor-or-self::*/(count(preceding-sibling::*) + 1)
Hope that helps, Leo
Cheers from abroad ;) And thanks to Dirk (and everyone else) for all the replies on the mailing list,
Christian Am 17.05.2016 18:40 schrieb "Rob Stapper" r.stapper@lijbrandt.nl:
Thanx all you guys,
Great to see this much response.
fn:path() is exactly what I needed. I do feel a little awkward though that I missed this one.
Cheers, Rob
-----Oorspronkelijk bericht----- Van: Leonard Wörteler [mailto:leonard.woerteler@uni-konstanz.de] Verzonden: dinsdag 17 mei 2016 16:34 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] question
Hi Rob,
Am 17.05.2016 um 15:51 schrieb Rob Stapper:
In order to get some path to the element that is being processed I hoped that the following statement [ 1] would return a sequence of positions of each node amongst its siblings.
[ 1] $element/ancestor-or-self::* ! ./fn:position()
Should this work?
no, that this does not work because the `fn:position()` function returns the position of the context item in the innermost path expression, which in your case is `./fn:position()`.
Is there some other ;-) clever way to retrieve a path to a random element that is being processed?
Graydon already mentioned the `fn:path($node)` function, which returns a valid XPath expression that uniquely identifies the given node. If you just want to get the position of each ancestor inside its parent, you can simply count all its preceding siblings and add one:
$element/ancestor-or-self::*/(count(preceding-sibling::*) + 1)
Hope that helps, Leo
basex-talk@mailman.uni-konstanz.de