Re: [xsl] Sorting in A for-each-group

Subject: Re: [xsl] Sorting in A for-each-group
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 20 Jun 2007 14:00:34 +0100
> (i left out the top level elements and started  with the topmost div):
which meant the stylesheet as posted didn't select anything.

I suspect that your only problem is that 10 is sorting before 3 as it's
doing strings not numbers.
<xsl:sort data-type="number" select="@n"/>


I can't readthis xml by eye to really spot what is being sorted where
but i get:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template name="body">
<xsl:variable name="docs" as="node()*" select="document(('file1.xml','file2.xml'))"/>
<xsl:variable name="divs" as="node()*" 
select="$docs/div"/>
<xsl:for-each-group select="$divs" group-by="@id">
   <div type="{current-grouping-key()}" id="{current-group()[1]/@id}">
       <xsl:apply-templates select="current-group()[1]/head"/>
       <xsl:for-each-group select="current-group()/node()" group-by="@id">
           <div type="{current-group()[1]/@type}" 
id="{current-grouping-key()}" n="{current-group()[1]/@n}">
               <!-- I want to insert a xsl:sort here so that i can sort 
the current-group() according to the @n -->
               <xsl:apply-templates select="current-group()[1]/head"/>
               <xsl:for-each-group select="current-group()/div" 
group-by="@n">
<xsl:sort data-type="number" select="@n"/>
                   <div type="{current-group()[1]/@type}" 
id="{current-group()[1]/@id}" n="{current-grouping-key()}">
                       <xsl:apply-templates 
select="current-group()[1]/head"/>
                        <xsl:apply-templates select="div"/>
                   </div>
               </xsl:for-each-group>
           </div>
       </xsl:for-each-group>
   </div>
</xsl:for-each-group>
</xsl:template>

<!-- indentity template here -->
<xsl:template match="*">
  <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>




$ saxon8 -it body file.xsl
<?xml version="1.0" encoding="UTF-8"?>
<div type="PLBAT" id="PLBAT">
   <head>
      <hi rend="b">Some text</hi>
   </head>
   <div type="papyrus_number" id="PLBAT1" n="1">
      <head>
         <hi rend="b">1</hi>
      </head>
      <div type="nr" id="PLBAT1_3" n="3">
         <head>
            <hi rend="b">3</hi>
         </head>
         <div type="zeile" id="PLBAT1_10_-" n="-">
            <head>-</head>
            <div type="entry" id="PLBAT1_10_-_BL5" n="10" part="-" vol="BL5" lang="ger"
                 page="59">
               <p>Some Text</p>
            </div>
         </div>
      </div>
      <div type="nr" id="PLBAT1_9" n="9">
         <head>
            <hi rend="b">9</hi>
         </head>
         <div type="zeile" id="PLBAT1_14_11-12" n="11-12">
            <head>11-12</head>
            <div type="entry" id="PLBAT1_14_11-12_BL5" n="14" part="11-12" vol="BL5" lang="ger"
                 page="59">
               <p>Sone text</p>
            </div>
         </div>
      </div>
      <div type="nr" id="PLBAT1_10" n="10">
         <head>
            <hi rend="b">10</hi>
         </head>
         <div type="zeile" id="PLBAT1_10_-" n="-">
            <head>-</head>
            <div type="entry" id="PLBAT1_10_-_BL5" n="10" part="-" vol="BL5" lang="ger"
                 page="59">
               <p>Some Text</p>
            </div>
         </div>
      </div>
      <div type="nr" id="PLBAT1_14" n="14">
         <head>
            <hi rend="b">14</hi>
         </head>
         <div type="zeile" id="PLBAT1_14_11-12" n="11-12">
            <head>11-12</head>
            <div type="entry" id="PLBAT1_14_11-12_BL5" n="14" part="11-12" vol="BL5" lang="ger"
                 page="59">
               <p>Sone text</p>
            </div>
         </div>
      </div>
   </div>
</div>

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

Current Thread