HTML and CSS   XSLT   JavaScript   Images   Soft   Etc  
Dmitry Filatov

Indexes by means of XSLT 17 March 2006


Task: Create a universal template for making indexes of unsorted data.

We’d like to guarantee future usefulness of the template, so lets complicate the task and suppose that:

— initial set has random structure;
— initial data is unsorted;
— number of columns is unknown;
— alphabetical blocks are arranged into columns evenly.

What it’s going to look like
If the index has several columns there are two ways to arrange them:

Vertically

A

  • Adams
  • Allen

B

  • Brooks

C

  • Cameron
  • Campbell
  • Clark
  • Cooper
  • Cox

E

  • Egan
  • Elkin
  • Enfield

F

  • Fisher
  • Foster

H

  • Hall
  • Harris
  • Hughes

J

  • Jackson

L

  • Lewis

M

  • Murray
  • Myers

R

  • Reed
  • Robert

T

  • Taylor
  • Thompson

W

  • Watson
  • Wright


Horizontally

A

  • Adams
  • Allen

B

  • Brooks

C

  • Cameron
  • Campbell
  • Clark
  • Cooper
  • Cox

E

  • Egan
  • Elkin
  • Enfield

F

  • Fisher
  • Foster

H

  • Hall
  • Harris
  • Hughes

J

  • Jackson

L

  • Lewis

M

  • Murray
  • Myers

R

  • Reed
  • Robert

T

  • Taylor
  • Thompson

W

  • Watson
  • Wright

Processing random set of data
We need a universal technique, so the initial set of data must be processed regardless of its structure. To enable this we can render it for the index template as nodelist. Other properties include:

key-name — name of a key based on first letter (I am going to specify its purpose further on; at this point, it’s important that the number of nodes to which we assign a key value equals to the number of nodes implemented by nodelist);
count-columns — number of columns for arranging the data;
direction — way of filling the columns (0 — vertical, 1 — horizontal).

Here is a sample script activating the index template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:import href="alpha-indexes.xslt" />
	
	<!-- Assigning first-letter key to the nodes -->
	<xsl:key name="items-key" match="/list/item" use="substring(., 1, 1)" />
	
	<xsl:template match="/list">	
		<!-- Activating common template (four columns filled vertically) -->
		<xsl:call-template name="make-indexes">
			<xsl:with-param name="nodelist" select="item" />
			<xsl:with-param name="key-name" select="'items-key'" />
			<xsl:with-param name="count-columns" select="4" />
			<xsl:with-param name="direction" select="0" />
		</xsl:call-template>
	</xsl:template>	
</xsl:stylesheet>

Order a design...