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

G. Ken Holman gkholman at CraneSoftwrights.com
Mon Mar 16 08:37:32 PST 2009


At 2009-03-15 18:04 -0700, Howard Katz wrote:
>I'd be happy to see a solution either in XQuery or in XSLT. Here's some
>sample input and the output.

Below, Howard, is a solution in XSLT.  Note that the XSLT 
specification defaults access to the source node tree in document 
order, so there is no need for the portable provisions I commented on 
regarding XQuery.

I hope this helps.

. . . . . . . . . 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>xslt howard.xml howard.xsl
<?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.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
   <library>
     <xsl:for-each select="library/book">
       <xsl:sort select="@pubYear" order="descending"/>
       <xsl:copy-of select="."/>
     </xsl:for-each>
   </library>
</xsl:template>

</xsl:stylesheet>

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