[xquery-talk] xquery: iteration simulation

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


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:gkholman at CraneSoftwrights.com
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



More information about the talk mailing list