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

xquery: iteration simulation

G. Ken Holman gkholman at CraneSoftwrights.com
Sun Mar 8 18:15:28 PST 2009


  xquery: iteration simulation
At 2009-03-08 17:57 -0400, I wrote:
>Below is a query that gives those results taken from a very 
>different angle.  It won't be fast because of the ancestor counting 
>at every level, and the use of distinct-values() requires me to work 
>with strings.  It might improve with a recursive call that tracks 
>depth and passes it ... but you didn't say that performance would be an issue.

After filing this I realized I was counting everything and filtering 
the results, rather than only counting what needed to be counted.  So 
the code below should execute faster than the earlier post.

I hope this helps.

. . . . . . . . . . Ken

t:\ftemp>type michal-data.xml
<mainNode>
   <x>
     <someStr>
       <someStr>
         <y/>
         <someStr>
           <z>
             <someStr/>
           </z>
         </someStr>
       </someStr>
       <anotherStr>
         <anotherStr>
           <anotherStr/>
         </anotherStr>
         <anotherStr>
           <anotherStr/>
           <anotherStr>
             <anotherStr/>
           </anotherStr>
         </anotherStr>
       </anotherStr>
     </someStr>
   </x>
   <someStr>
     <y/>
     <someStr>
       <z>
         <someStr/>
       </z>
     </someStr>
   </someStr>
</mainNode>
t:\ftemp>type michal-keywords.xml
<keywords>
   <keyword>someStr</keyword>
   <keyword>anotherStr</keyword>
</keywords>
t:\ftemp>call xquery michal-data.xml michal.xq
Element named "anotherStr" at depth 1 occurred 1 times.
Element named "anotherStr" at depth 2 occurred 2 times.
Element named "anotherStr" at depth 3 occurred 3 times.
Element named "anotherStr" at depth 4 occurred 1 times.
Element named "someStr" at depth 1 occurred 2 times.
Element named "someStr" at depth 2 occurred 2 times.
Element named "someStr" at depth 3 occurred 2 times.
Element named "someStr" at depth 4 occurred 1 times.
t:\ftemp>type michal.xq
declare namespace saxon = "http://saxon.sf.net/";
declare option saxon:output "method=text";

let $names as xs:string+ := doc('michal-keywords.xml')/*/keyword/string(.)
let $depths as element(depth)+ :=
   for $each in //*
       where name($each) = $names
       return
       <depth>
         { count($each/ancestor-or-self::*[name(.)=name($each)]), name($each) }
       </depth>
   return
     string-join(
       for $each in distinct-values( $depths )
           order by substring-after($each,' '),
                    xs:integer( substring-before($each,' ') )
           return ( concat( 'Element named "',substring-after($each,' '),
                            '" at depth ',substring-before($each,' '),
                            ' occurred ',string(count($depths[.=$each])),
                            ' times.') ),
       '&#xa;' )



t:\ftemp>rem Done!



--
XQuery/XSLT training in Prague, CZ 2009-03 http://www.xmlprague.cz
XQuery/XSLT/XSL-FO training in Los Angeles/Anaheim - 2009-06-01/10
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:http://x-query.com/mailman/listinfo/talk
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



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.