xsl:sort order problems

Subject: xsl:sort order problems
From: DPawson@xxxxxxxxxxx
Date: Mon, 29 Nov 1999 10:30:49 -0000
(long post)

I'm trying to do a double sort on <qna> elements,
using the child <topic> firstly, then <q> as the sort keys.

Objective:

To output

<h2>topic</h2>  <!-- single topic header -->
   <p> All q elements, sorted by content </p>

<h2>next topic header </h2>

The stylesheet and xml example below, nearly give me this.

My problems centre/center around a lack of understanding 
about what happens when the named template 'l2' is invoked.

I'm assuming that
1. The qna elements are double sorted as requested.
2. The node list available to the named template is in sorted order.
[Are my assumptions right please]

This would give me the qna elements in order of topics

A
BB******
HHA 

(which indeed I have.)
I do have the single h2 element containing BB**** ,
but its positioned 'wrongly'.  I want it prior to the first q child
of topic B**** 
The document order of the q elements under topic BB*** is
Zelia Norman Arabella, which makes me suspect that the 
named template is being passed a node list in document order,
rather than sort order.
The <xsl:if statement is intended to only put out the <h2> element
if the topic is the first of any number of similar topic elements
(I'm ignoring normalise for the moment).

any help appreciated, DaveP. Using XT.
(only just found out that James has implemented preceding axis
by forgetting to use saxon and it still outputs what I wanted :-)

XML file

<?xml version="1.0"?>



<doc>
<qna>
    <topic>A</topic>
    <q>xy</q> <a/>
</qna>

<qna>
    <topic>X</topic>
<q>axy</q> <a/>
</qna>

<qna>
    <topic>HHH</topic>
<q>Joey </q> <a/>
</qna>

<qna>
    <topic>HHA</topic>
<q>Angela </q> <a/>
</qna>

<qna>
    <topic>BB********</topic>
<q>Zelie </q> <a/>
</qna>

<qna>
    <topic>BB********</topic>
<q>Norman </q> <a/>
</qna>

<qna>
    <topic>BB********</topic>
<q>Arabella </q> <a/>
</qna>
<qna>
    <topic>NNN</topic>
<q>Fred </q> <a/>
</qna>


</doc>
 
=========== XSL file =======================
<?xml version="1.0" ?>

<!DOCTYPE xsl:stylesheet [
<!ENTITY sp "<xsl:text> </xsl:text>">
<!ENTITY dot "<xsl:text>.</xsl:text>">
<!ENTITY nl "&#10;&#xD;"> 
]>


 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 version="1.0">

<xsl:output  method="xml" indent="yes" />


<xsl:template match="/">root  <!-- match on parent of group --> 
  <xsl:for-each select="doc/qna">
    <xsl:sort select="topic"/>    <!-- sort on topic -->
      <xsl:sort select="q"/>      <!-- sort on q     -->
      <xsl:call-template name="l2"/> <!-- Output Result of sort-->
    </xsl:for-each>
</xsl:template>

<xsl:template name ="l2" >
  <xsl:if test="not(topic = preceding-sibling::qna[1]/topic)">
    <h2>TOPIC IS:  <xsl:value-of select="topic"/> </h2>
  </xsl:if>
  <p>  <xsl:value-of select="q"/>(<xsl:value-of select="topic"/>) </p>
</xsl:template> 



 <xsl:template match="*" priority="-1">
************ Default **************
 </xsl:template>
</xsl:stylesheet>


=========== Present output =============

root   
  
<h2>TOPIC IS:  A</h2>
<p>xy(A) </p>
<p>Arabella (BB********) </p>
<p>Norman (BB********) </p>
<h2>TOPIC IS:  BB********</h2>
<p>Zelie (BB********) </p>
<h2>TOPIC IS:  HHA</h2>
<p>Angela (HHA) </p>
<h2>TOPIC IS:  HHH</h2>
<p>Joey (HHH) </p>
<h2>TOPIC IS:  NNN</h2>
<p>Fred (NNN) </p>
<h2>TOPIC IS:  X</h2>
<p>axy(X) </p>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread