[xquery-talk] distance between elements and hierarchical order

Sergio Andreozzi sergio.andreozzi at cnaf.infn.it
Fri Dec 9 03:58:34 PST 2005


David Carlisle wrote:
> In the original problem I don't think you need to explictly iterate over
> the ancestor lists then use count and min, you just walk up the
> ancestors of x which are not ancestors of y , then up one level to the
> nearest common ancestor then down the ancestors of y that are not
> ancestors of x so the distance is
> 
> count($x-ancestors)+count($y-ancestors)+1-2*count($x-ancestors intersect $y-ancestors)
> 
> where x-ancestors is as in the posted code the ancestor-or-self:: axis
> from $x


the final solution for prob. no.2 should be this one then:

let $x := ...
     $y := ...

let $x-ancestors := $x/ancestor-or-self::element(),
     $y-ancestors := $y/ancestor-or-self::element()

return count($x-ancestors)+count($y-ancestors)+1-2*count($x-ancestors 
intersect $y-ancestors)

ex.1

let $x := (doc("test.xml")/S/A/B/F)[1],
     $y := (doc("test.xml")/S/A/B/C/G/H)[3]

result is 7

ex.2

let $x := (doc("test.xml")/S/A/B/F)[2],
     $y := (doc("test.xml")/S/A/B/C/G/H)[3]

result is 5

where test.xml is:

<S>
     <A>
         <B>
             <F>4</F>
             <C>
                 <D>3</D>
                 <E>5</E>
                 <G>
                     <H>3</H>
                 </G>
                 <G>
                     <H>2</H>
                 </G>
             </C>
         </B>
         <B>
	    <F>2</F>
	    <C>
	        <G>
	             <H>2</H>
	        </G>
	    </C>
         </B>
     </A>
</S>



thank you very much for your help.

	Kind regards, Sergio


More information about the talk mailing list