[xquery-talk] Absolute Path of Node

Michael Kay mhk at mhk.me.uk
Mon May 30 18:06:47 PDT 2005


On reason there is no standard solution to this is that there's a range of
different requirements. For example, some people want the path
 
   *[3]/*[2]/*[2]
 
which has the advantage that it's not namespace-sensitive, while others want
 
   alpha/beta/gamma
 
and others want
 
   alpha[3]/beta[2]/gamma[2]
 
(The last of these can be obtained in Saxon using the saxon:path() extension
function.)
 
If you want the alpha/beta/gamma form, you can get it as
 
    string-join(ancestor-or-self::*/name(), '/')
 
assuming that your query processor supports the ancestor axis.
 
Part 2: I'd like to compare absolute pathes of two (or more) different
nodes. So if one has a path like 
/a/b/c/d 
and the other has 
/a/b/e/f 
Is it possible, to make b the current node and do operations on it and its
children? 
 
The fact that both paths start a/b doesn't actually mean that the two nodes
have a common ancestor named b. They might not have any common ancestor at
all!
 
You can find the lowest common ancestor of two nodes $a and $b as
($a/ancestor-or-self::node() intersect $b/ancestor-or-self::node())[last()].
This will be the empty sequence if there is no common ancestor.
 
The way to make a node $n the context node in XQuery is with the expression
$n/(EXPR). However, this has the side-effect of forcing the results of EXPR
into document order.
 
Michael Kay
http://www.saxonica.com/
 
 


  _____  

From: talk-bounces at xquery.com [mailto:talk-bounces at xquery.com] On Behalf Of
EXTERNAL Kruse Peter (Praktikant;CR/AEF4)
Sent: 30 May 2005 15:05
To: talk at xquery.com
Subject: [xquery-talk] Absolute Path of Node



Hi folks, 

I was searching the archives but i can't find a straight forward solution
for my problem. Please excuse if it already exists and i simply didn't find
it.

Part 1: After doing a "let" or a "for" with //, is it posible to get the
path from the root-node to the selected node? Maybe as string?

Like in this example: 
--------------- 
let $anydepth := doc("file.xml")//somepath 
for $element in $anydepth/special/* 
  return 
    {fn:path-from-root($element)} 
--------------- 
I know the function path-from-root does not exist but is there something
similar? 

Part 2: I'd like to compare absolute pathes of two (or more) different
nodes. So if one has a path like 
/a/b/c/d 
and the other has 
/a/b/e/f 
Is it possible, to make b the current node and do operations on it and its
children? Something like nearest common ancestor? There had been discussions
on the xsl lists but is there a xquery/xpath only solution?


Mit freundlichen Grüßen / Kind Regards, 
Peter Kruse 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xquery.com/pipermail/talk/attachments/20050530/8300810f/attachment.htm


More information about the talk mailing list