Index

1. Automatically Generating Indexes

1.

Automatically Generating Indexes

David Carlisle


This only does two level indices.

You could either just extend to deeper levels, or probably
better parameterise the templates on the level, but anyway
something to get you started...



XML source file


<x> <p><index level1="Accommodation 
" />accom</p>
<p><index level1="Abandonment" 
	level2="international policy
of"/>aband/intl</p> <p><index 
	level1="Accommodation"
/>accom</p> <p><index 
	level1="Accommodation" level2="foreign
funding and"/>accom/foreign</p> <p><index
level1="Abandonment" level2="international policy
of"/>aband/intl</p> <p><index 
	level1="Accommodation"
level2="right wing and"/>accom/rw</p> 
<p><index
level1="Accommodation" level2="foreign funding
and"/>accom/foreign</p> <p>
	<index level1="Abortion"
/>abort</p> <p><index 
level1="Accommodation" />accom</p>
<p><index level1="Accommodation" 
	level2="right wing
and"/>accom/rw</p>

<doindex/>

</x>


Output HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<br>
  [N4]<a name="N4"></a>accom
<br>
  [N9]<a name="N9"></a>aband/intl
<br>
  [N15]<a name="N15"></a>accom
<br>
  [N20]<a name="N20"></a>accom/foreign
<br>
  [N26]<a name="N26"></a>aband/intl
<br>
  [N32]<a name="N32"></a>accom/rw
<br>
  [N38]<a name="N38"></a>accom/foreign
<br>
  [N44]<a name="N44"></a>abort
<br>
  [N49]<a name="N49"></a>accom
<br>
  [N54]<a name="N54"></a>accom/rw

<div class="index"> <h2>Index</h2> <a 
	name="index"></a>
<ul class="nobull"> 
	<li> <b>Abandonment</b> <ul>
<li>international policy of: <a 
	href="#N9">link</a>, <a
href="#N26">link</a> 
	</li> </ul> </li> <li>
<b>Abortion</b>: <a 
	href="#N44">link</a> </li> <li>
<b>Accommodation</b>: <a 
	href="#N4">link</a>, <a
href="#N15">link</a>, <a 
	href="#N49">link</a> <ul>
<li>foreign funding and: <a 
	href="#N20">link</a>, <a
href="#N38">link</a> </li> 
	<li>right wing and: <a
href="#N32">link</a>, <a 
	href="#N54">link</a> </li>
</ul> </li> </ul> </div>

XSL Stylesheet.

<!-- index.xsl  Index Generation Module-->
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Tranform"     
	version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template 
	match="index"> <br/> [<xsl:value-of
   select="generate-id(.)"/>]<a 
	name="{generate-id(.)}"></a>

</xsl:template>

<xsl:template match="doindex">
  <xsl:call-template name="book.index"/>
</xsl:template>


<xsl:template name="book.index">
   <div class="index">
     <h2>Index</h2>
     <a name="index"></a>
     <ul class="nobull">
       <xsl:apply-templates 
        select="//index[
	    not(@level1=following::index/@level1)]" 
        mode="index.mode">
<xsl:sort select="@level1"/>
       </xsl:apply-templates>
     </ul>
   </div>
</xsl:template>

<xsl:template match="index" mode="index.mode">
<li><b><xsl:value-of 
	select="@level1"/></b>
<xsl:apply-templates 
  mode="index.link" 
  select="//index[@level1=current()/@level1 and 
	    not(@level2)]"/>
  <xsl:if test="//index[@level1=current()/@level1 
	    and @level2]">
  <ul>
  <xsl:apply-templates select=
	    "//index[@level1=current()/@level1 and
           @level2 and not(@level2=following::index/@level2)]" 
       mode="index.mode2">
<xsl:sort select="@level2"/>
       </xsl:apply-templates>
  </ul>
  </xsl:if>
</li>
</xsl:template>


<xsl:template match="index" mode="index.mode2">
<li><xsl:value-of select="@level2"/>
<xsl:apply-templates 
  mode="index.link" 
  select="//index[@level1=current()/@level1 
	and @level2=current()/@level2 
	and not(@level3)]"/>
</li>
</xsl:template>

<xsl:template match="index" mode="index.link">
  <xsl:if test="position()=1">: </xsl:if> 
 <a href="#{generate-id(.)}">link</a>
  <xsl:if test="position()!=last()">, </xsl:if> 
</xsl:template>


</xsl:stylesheet>