[xquery-talk] reversing element ordering, with a caveat

G. Ken Holman gkholman at CraneSoftwrights.com
Mon Mar 16 07:54:13 PST 2009


At 2009-03-15 19:23 -0700, Brian Maso wrote:
>In XQuery, I think its just a matter of running over the distinct 
>values of //book/@pubYear in descending order. Something like:

I don't think that level of complexity is needed at all.

XQuery clause 3.8.3 includes in one of the examples the text:

   "the keyword stable indicates that their input order is preserved"

>This will retain original document ordering for elements with the 
>same @pubYear value, but years will be descending from highest value to lowest.

I suspect, Howard, this is all you need:

<library>
  {
   for $each in library/book
       stable order by $each/@pubYear descending
         return $each
  }
</library>

A running example is below.  Note that if your 
implementation-dependent default mode for ordering is unordered or 
you want a portable query across all implementations, then a safe 
portable addition would be to add at the top:

   default ordering ordered;

... and would be required in Brian's solution as well to guarantee a 
correct order across all implementations.

But I'm using Saxon below so I don't have to.

I hope this helps.  You shouldn't need to go into distinct-values at all.

. . . . . . . Ken

t:\ftemp>type howard.xml
<library>
         <book pubYear='1942' catid='1' >
                 <otherStuff /></book>
         <book pubYear='1942' catid='2' >
                 <otherStuff /></book>
         <book pubYear='1942' catid=' 3 ' >
                 <otherStuff /></book>
         ...

         <book pubYear='2009' catid=' 2011' >
                 <otherStuff /></book>
         <book pubYear='2009' catid=' 2012 ' >
                 <otherStuff /></book>
         <book pubYear='2009' catid=' 2013 ' >
                 <otherStuff /></book>
</library>

t:\ftemp>xquery howard.xml howard.xq
<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book pubYear="2009" catid=" 2011">
                     <otherStuff/>
    </book>
    <book pubYear="2009" catid=" 2012 ">
                     <otherStuff/>
    </book>
    <book pubYear="2009" catid=" 2013 ">
                     <otherStuff/>
    </book>
    <book pubYear="1942" catid="1">
                     <otherStuff/>
    </book>
    <book pubYear="1942" catid="2">
                     <otherStuff/>
    </book>
    <book pubYear="1942" catid=" 3 ">
                     <otherStuff/>
    </book>
</library>
t:\ftemp>type howard.xq
<library>
  {
   for $each in library/book
       stable order by $each/@pubYear descending
         return $each
  }
</library>
t:\ftemp>

--
XQuery/XSLT/XSL-FO training in Los Angeles (New dates!) 2009-06-08
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