[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

distance between elements and hierarchical order

Wolfgang Hoschek wolfgang.hoschek at mac.com
Thu Dec 8 18:24:16 PST 2005


xml distance between nodes
problem 1) the recursive query can be reduced to

let $x := doc("test.xml")/S/A
return count($x/ancestor-or-self::element())

Regards,
Wolfgang.

On Dec 8, 2005, at 6:16 PM, Sergio Andreozzi wrote:

> 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.
>>
>
>
> for problem 1. (compute the hierarchical level) I've this solution  
> so far. It uses the recursion to generate the list of ancestors,  
> then count them:
>
> declare function local:ancestors-self($n as node()?) {
>
>   if (empty($n))
>   then ()
>   else (local:ancestors-self($n/..), $n)
>
> };
>
> declare function local:Hlevel($n as node()?) as xs:integer{
>
>   count( local:ancestors-self($n) )-1
>
> }
>
> ex:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <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>
>     </A>
> </S>
>
> for $A in doc("test.xml")/S/A
> return <Result>{ local:Hlevel(($A)[1]) }</Result>
>
> <Result>2</Result>
>
> for $A in doc("test.xml")/S/A
> return <Result>{ local:Hlevel(($A/B)[1]) }</Result>
>
> <Result>3</Result>
>
> for $A in doc("test.xml")/S/A
> return <Result>{ local:Hlevel(($A/B/C/G/H)[1]) }</Result>
>
> <Result>6</Result>
>
> still working on no.2.
>
>
>     Sergio
>



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.