This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Making sorted balanced two-column tables from one-column data from unsorted data


I am trying to create a two column table from unsorted data.
The idea is very similar to the XSL FAQ: Making balanced two-column tables
from one-column data (see
http://www.dpawson.freeserve.co.uk/xsl/N7450.html#N13669 ).  However, my
data is initially unsorted.  
I initially tried adding  <xsl:sort> as a subelement to <xsl:for-each>, and
I get the following results (using MSXML3):

	A H
	B E
	C G
	D K
	E J
	F

The first column is sorted and is correct, the second column is not.  Looks
like  "select=$comp-list[$here+$half]" is using the unsorted list of
COMPONENTS rather than the sorted list.  This kinda makes sense.  

So I tried to create a variable that contains the sorted list of nodes like
this, but this yields no nodes:
	  <xsl:variable name="sorted-comp-list">
  		<xsl:for-each select="COMPONENT">
			<xsl:sort select="." />
		</xsl:for-each>
	  </xsl:variable>

Anybody know of a way to create a sorted list of nodes and be able store in
a variable for later use?

Thanks in advance.
<Tom/>


==================
Source is attached below.
==================
<!-- tabletest.xml  -->
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="tabletest.xsl"?>
<TASKS>
  <TASK>
    <COMPONENTS>
	<!-- same data as FAQ, but not in alpha order -->
      <COMPONENT>F</COMPONENT>
      <COMPONENT>B</COMPONENT>
      <COMPONENT>I</COMPONENT>
      <COMPONENT>C</COMPONENT>
      <COMPONENT>A</COMPONENT>
      <COMPONENT>D</COMPONENT>
      <COMPONENT>H</COMPONENT>
      <COMPONENT>E</COMPONENT>
      <COMPONENT>G</COMPONENT>
      <COMPONENT>K</COMPONENT>
      <COMPONENT>J</COMPONENT>
    </COMPONENTS>
  </TASK>
</TASKS>

<!-- tabletest.xsl  -->
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template match="/">

  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="TASKS/TASK/COMPONENTS">
  <xsl:variable name="t-size" select="count(COMPONENT)"/>
  <xsl:variable name="half" select="ceiling($t-size div 2)"/>
   
  <xsl:variable name="comp-list" select="COMPONENT"/>

<TABLE>
  <xsl:for-each select="$comp-list" >
  	<xsl:sort select="."/>
	<xsl:variable name="here" select="position()"/>

	<xsl:if test="$here &lt; $half+1">
   	<TR>
   		<TD><xsl:value-of select="."/></TD>
   		<TD>
       		<xsl:choose>
  				<xsl:when test="$comp-list[$here+$half]">
    				<xsl:value-of
select="$comp-list[$here+$half]"/>
       			</xsl:when>
       			<xsl:otherwise></xsl:otherwise>
       		</xsl:choose>
      	</TD>
   	 </TR>
	 </xsl:if>
  </xsl:for-each>
</TABLE>

</xsl:template>
</xsl:stylesheet>


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]