[xquery-talk] xquery: iteration simulation

G. Ken Holman gkholman at CraneSoftwrights.com
Sun Mar 8 17:57:10 PST 2009

At 2009-03-08 17:16 -0400, I wrote:
>It would help volunteers if you could post a contrived example of 
>the nature of the structures.  Not all 50 levels, but still, some examples.
>>Now, i need to query it. But what i need to do, is to make queries 
>>of the form:

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.

>How are you expressing the "someStr" name?  Is it fixed?  Is it variable?

I made an assumption that there is an XML file listing the keywords.

>>The deep level of the last query may be even around 50 (so, 50 queries).

The solution below works at all depths, but I didn't know what 
criteria you had for counting the ancestors ... I assumed you are 
only counting ancestors of the same name.  And I'm ignoring changes 
in namespace prefixes, relying on the full name for distinction.

>>Is is posible to write iteration in XQuery for that. Or pass it as 
>>a function?

The example below is only one query, not a chain as I first thought.

I hope this helps you create the kind of result that you need.

. . . . . . . . Ken

t:\ftemp>type michal-data.xml
t:\ftemp>type michal-keywords.xml
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 $depths as element(depth)+ :=
   for $each in //*
         { count($each/ancestor-or-self::*[name(.)=name($each)]), name($each) }
       for $each in distinct-values( $depths ),
           $name in doc('michal-keywords.xml')/*/keyword
           where $name = substring-after($each,' ')
           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