RE: [xsl] Sorting Nested Loops

Subject: RE: [xsl] Sorting Nested Loops
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Tue, 9 Jul 2002 22:07:48 +0100
> I am having trouble
> getting the desired results when I try to sort by an
> attribute in a nested loop. I want to sort by the
> sequence attribute in ascending order. I get them back
> in this order
> 
> 1, 7, 8, 14, 3, 4, 9, 10, 5, 6, 11, 12, 2, 13

Well, you have divided the list into groups of four, and then you have
sorted each group of four, so this is precisely what you should expect.

If you want to sort the list before grouping it, the simplest way is to
do two passes. In the first pass, sort the entire list into a temporary
tree, in the second (using xx:node-set()) process this temporary tree
into groups of four.

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> 
> Any ideas?
> 
> This is the xml
> 
> <?xml version="1.0" encoding="iso-8859-1" ?>
> <leadsite>
> <tabs>
> 
> 		<tab typeid="" sequence="1" active="1">
> 			<label>Tab 1</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="14" active="1">
> 			<label>Tab 14</label>
> 			<url></url>
> 		</tab>
> 
> 				<tab typeid="" sequence="7" active="1">
> 			<label>Tab 7</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="8" active="1">
> 			<label>Tab 8</label>
> 			<url></url>
> 		</tab>
> 				<tab typeid="" sequence="10" active="1">
> 			<label>Tab 10</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="9" active="1">
> 			<label>Tab 9</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="3" active="">
> 			<label>Tab 3</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="4" active="1">
> 			<label>Tab 4</label>
> 			<url></url>
> 		</tab>
> 				<tab typeid="" sequence="5" active="1">
> 			<label>Tab 5</label>
> 			<url>/controlcenter/applications/tyler.asp</url>
> 		</tab>
> 		<tab typeid="" sequence="6" active="1">
> 			<label>Tab 6</label>
> 			<url></url>
> 		</tab>
> 				<tab typeid="" sequence="12" active="1">
> 			<label>Tab 12</label>
> 			<url>/controlcenter/applications/tyler.asp</url>
> 		</tab>
> 		<tab typeid="" sequence="11" active="1">
> 			<label>Tab 11</label>
> 			<url></url>
> 		</tab>
> 				<tab typeid="" sequence="13" active="1">
> 			<label>Tab 13</label>
> 			<url></url>
> 		</tab>
> 		<tab typeid="" sequence="2" active="1">
> 			<label>Tab 2</label>
> 			<url></url>
> 		</tab>
> 	</tabs>
> </leadsite>
> 
> This is the xsl
> 
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 	<xsl:template name="tabs" mode="tabs"
> match="leadsite/tabs">
> 	<xsl:for-each select="tab[position() mod 4 = 1]">
> 		
> 		<table width="100%" cellpadding="0" 
> cellspacing="0" border="0">
> 			<tr>
> 			<xsl:for-each select=". |
> following-sibling::tab[position() &lt; 4]">
> 			<xsl:sort select="@sequence" 
> data-type="number" />
> 			<xsl:call-template name="tabcells" />
> 			</xsl:for-each>
> 			</tr>
> 			<![CDATA[</tr>]]>
> 		</table>
> 	</xsl:for-each>
> 	</xsl:template>
> 
> 	<xsl:template name="tabcells">
> 		<td valign="Top" align="Left" width="8" 
> class="tabs" background="images/Agnt_J_Tabrig_Bg.Gif">
> 			<img height="15" src="images/Agnt_J_Tabrig.Gif"
> width="8" />
> 		</td>
> 		<td valign="Center" align="Middle" class="tabs"
> nowrap="yes">
> 			<xsl:variable name="url" select="url/." />
> 			<a href="{$url}">
> 				<xsl:value-of select="label/." />
> 			</a>
> 		</td>
> 		<td valign="Top" align="Right" width="8" 
> class="tabs" background="images/Agnt_J_Tablef.Gif">
> 			<img height="15" src="images/Agnt_J_Tablef.Gif"
> width="8" />
> 		</td>
> 		<td width="1">
> 			<img height="1" src="Dot.Gif" width="1" />
> 		</td>
> 	</xsl:template>
> 	
> </xsl:stylesheet>
> 
> 
> 	
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread