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

reversing element ordering, with a caveat

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


  reversing element ordering
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: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.