[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

both sorting and grouping set of nodes

Subject: both sorting and grouping set of nodes
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Wed, 02 Dec 2009 22:17:22 -0500
 both sorting and grouping set of nodes
Warning: intended to be read in a monospaced typeface.

This may abe a FAQ, but if so, I didn't find it on D. Pawson's FAQ

The basic idea is that I want to extract a sizeable set of nodes from
a file, sort them, and spit out the output in a 4-column XHTML table.

So let's pretend I have an input document with a few hundred <name>
elements. And presume the content thereof is aready in the desired
format (e.g., "Surname, Forename" -- it's not, but let's start with
the easy case). I know how to spit out a sorted list of them:
     <xsl:for-each select="//name">
       <xsl:sort select="normalize-space(.)"/>
       <li><xsl:value-of select="normalize-space(.)"/></li>

Or, a bit more robustly:
     <xsl:apply-templates select="//name">
       <xsl:sort select="normalize-space(.)"/>
   <xsl:template match="name">
     <li><xsl:value-of select="normalize-space(.)"/></li>

And I've figured out a way to spit out a 4-column table of them
(reading down), although I bet folks on this list could demonstrate a
better method:
   <table border="1">
     <xsl:variable name="Q" select="ceiling( count( //name ) div 4 )"/>
     <xsl:for-each select="//name[ position() &lt;= $Q]">
       <xsl:variable name="Q1" select="position() + (1 * $Q)"/>
       <xsl:variable name="Q2" select="position() + (2 * $Q)"/>
       <xsl:variable name="Q3" select="position() + (3 * $Q)"/>
         <td><xsl:value-of select="normalize-space(.)"/></td>
         <td><xsl:value-of select="normalize-space( /*//name[ $Q1] )"/></td>
         <td><xsl:value-of select="normalize-space( /*//name[ $Q2] )"/></td>
         <td><xsl:value-of select="normalize-space( /*//name[ $Q3] )"/></td>

But how do I do both at once, i.e. generate a 4-column table (read
down) of *sorted* names? E.g. something like either
  Alex       Gaston      Lisa        Richard     
  Bonnie     Hermine     Matthew     Shary       
  Colin      Igor        Nicole      Tomas       
  Danielle   Julia       Otto        Virginie    
  Earl       Karl        Paula       Walter      
  Alex       Gaston      Matthew     Tomas
  Bonnie     Hermine     Nicole      Virginie
  Colin      Igor        Otto        Walter
  Danielle   Julia       Paula   
  Earl       Karl        Richard         
  Fiona      Lisa        Shary           

As recently noted on this list, XPath 2 doesn't have a sort()
function. I'm betting if I knew where to look in the reference guides
on my shelf, this isn't too hard. But I'm really not sure how to get
started. Fancy grouping? Sequences? Sigh.

Note that for this particular application, thinking of the things to
be sorted as a set of strings is fine. But one can imangine wanting to
the same thing with nodes that are more complex. In any case, Thanks
in advance.

Appendix 1
-------- -
Enormously simplified input for testing. 2 points to she who first
posts what this list of names is.

<?xml version="1.0" encoding="UTF-8"?>

Current Thread


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.
First Name
Last Name
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-2013 All Rights Reserved.