# [xquery-talk] distance between elements and hierarchical order

Charles Brooking charles.brooking at research.canon.com.au
Fri Dec 9 11:01:13 PST 2005

```Wolfgang Hoschek wrote:
> Both can probably be solved by walking the ancestor axis of the
> elements towards the root.
> For 2) find the nearest common ancestor, and add up the steps.
People can possibly correct me on this, but:

let \$a :=
element a
{
attribute id {"1"},
element b
{
attribute id {"1-1"},
element c {attribute id {"1-1-1"}},
element c {attribute id {"1-1-2"}}
},
element b
{
attribute id {"1-2"},
element c {attribute id {"1-2-1"}},
element c {attribute id {"1-2-2"}}
}
}
let
\$x := \$a[@id="1"]/b[@id="1-1"]/c[@id="1-1-1"],
\$y := \$a[@id="1"]/b[@id="1-2"]/c[@id="1-2-2"]
let \$possible-distances :=
let
\$x-ancestors := \$x/ancestor-or-self::element(),
\$y-ancestors := \$y/ancestor-or-self::element()
for
\$x-ancestor at \$x-ancestor-position in \$x-ancestors,
\$y-ancestor at \$y-ancestor-position in \$y-ancestors
where
\$x-ancestor is \$y-ancestor
return
count(\$x-ancestors) - \$x-ancestor-position +
count(\$y-ancestors) - \$y-ancestor-position
return
min(\$possible-distances)

Now a question:

If I had the following as one of the for-clauses:

\$x-ancestor at \$x-ancestor-position in \$x/ancestor-or-self::element()

would \$x itself (always) be at position 1, or should it be last?

Later
Charlie

--
Charles Brooking
Associate Software Engineer
Canon Information Systems Research Australia

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xquery.com/pipermail/talk/attachments/20051209/ef0b7409/attachment.htm
```