question

Tuesday, 17 May 2016 · 7 messages
RS
Rob Stapper17 May 2016 15:51 #
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
DK
Dirk Kirsten17 May 2016 16:00 #
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
Quoted text · Rob Stapper · 29 lines
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 >
-- Dirk Kirsten, BaseX GmbH, http://basexgmbh.de |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 91 68 276, Fax: 0049 7531 20 05 22
CG
Christoph Gaukelin reply to Rob Stapper, 17 May 2016, 15:5117 May 2016 16:13 #
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
Quoted text · Rob Stapper · 21 lines
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 >
GS
Graydon Saunders17 May 2016 16:26 #
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
Quoted text · Christoph Gaukel · 73 lines
On Tue, May 17, 2016, 10:14 Christoph Gaukel <christoph.gaukel at 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 > > >
LW
Leonard Wörtelerin reply to Rob Stapper, 17 May 2016, 15:5117 May 2016 16:33 #
Hi Rob,
Quoted text · Rob Stapper · 8 lines
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().
Quoted text · Rob Stapper · 2 lines
> 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
RS
Rob Stapper17 May 2016 17:40 #
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-----
Quoted text · Leonard Wörteler · 26 lines
Van: Leonard Wörteler [mailto:leonard.woerteler at 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
CG
Christian Grün17 May 2016 19:55 #
Cheers from abroad ;) And thanks to Dirk (and everyone else) for all the replies on the mailing list, Christian
Quoted text · Rob Stapper · 48 lines
Am 17.05.2016 18:40 schrieb "Rob Stapper" <r.stapper at 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 at 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 > > >

⚡Generated with XQuery