<?xml version="1.0" encoding="US-ASCII"?>
<!--This file was created automatically by html2xhtml-->
<!--from the HTML stylesheets. Do not edit this file.-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="urn:cz-kosek:functions:index" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:func="http://exslt.org/functions" xmlns:k="java:com.isogen.saxoni18n.Saxoni18nService" xmlns:exslt="http://exslt.org/common" xmlns="http://www.w3.org/1999/xhtml" extension-element-prefixes="func exslt" exclude-result-prefixes="func exslt i l k" version="1.0">

<!-- ********************************************************************
     $Id: autoidx.xsl,v 1.10 2006/06/06 07:09:58 sorin Exp $
     ********************************************************************

     This file is part of the XSL DocBook Stylesheet distribution.
     See ../README or http://nwalsh.com/docbook/xsl/ for copyright
     and other information.

     ******************************************************************** -->

<!-- ==================================================================== -->
<!-- The "english" method derived from Jeni Tennison's work. -->
<!-- The "kosek" method contributed by Jirka Kosek. -->
<!-- The "kimber" method contributed by Eliot Kimber of Innodata Isogen. -->

<xsl:key name="letter" match="indexterm" use="translate(substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

<xsl:key name="primary" match="indexterm" use="normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))"/>

<xsl:key name="secondary" match="indexterm" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])))"/>

<xsl:key name="tertiary" match="indexterm" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])))"/>

<xsl:key name="endofrange" match="indexterm[@class='endofrange']" use="@startref"/>

<xsl:key name="primary-section" match="indexterm[not(secondary) and not(see)]" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]))"/>

<xsl:key name="secondary-section" match="indexterm[not(tertiary) and not(see)]" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]))"/>

<xsl:key name="tertiary-section" match="indexterm[not(see)]" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]))"/>

<xsl:key name="see-also" match="indexterm[seealso]" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, seealso)"/>

<xsl:key name="see" match="indexterm[see]" use="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, see)"/>

<xsl:key name="sections" match="*[@id]" use="@id"/>

<!-- The following key is used in the kimber indexing method.
     To use this method, either copy this key to a
     customization layer, or xsl:import common/autoidx.xsl. -->
<!--
<xsl:key name="k-group"
         match="indexterm"
         use="k:getIndexGroupKey(&lang;, &primary;)"/>
-->


<xsl:template name="generate-index">
  <xsl:param name="scope" select="(ancestor::book|/)[last()]"/>

  <xsl:choose>
    <xsl:when test="$index.method = 'kosek'">
      <xsl:call-template name="generate-kosek-index">
        <xsl:with-param name="scope" select="$scope"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$index.method = 'kimber'">
      <xsl:call-template name="generate-kimber-index">
        <xsl:with-param name="scope" select="$scope"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="generate-english-index">
        <xsl:with-param name="scope" select="$scope"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
      
<xsl:template name="generate-english-index">
  <xsl:param name="scope" select="NOTANODE"/>

  <xsl:variable name="role">
    <xsl:if test="$index.on.role != 0">
      <xsl:value-of select="@role"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:if test="$index.on.type != 0">
      <xsl:value-of select="@type"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="terms" select="//indexterm                         [count(.|key('letter',                           translate(substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1),                              'abcdefghijklmnopqrstuvwxyz',                              'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))                           [count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1                           and not(@class = 'endofrange')]"/>

  <xsl:variable name="alphabetical" select="$terms[contains(concat('abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),                                         substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1))]"/>

  <xsl:variable name="others" select="$terms[not(contains(concat('abcdefghijklmnopqrstuvwxyz',                                                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),                                              substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1)))]"/>
  <div class="index">
    <xsl:if test="$others">
      <div class="indexdiv">
        <h3>
          <xsl:call-template name="gentext">
            <xsl:with-param name="key" select="'index symbols'"/>
          </xsl:call-template>
        </h3>
        <dl>
          <xsl:apply-templates select="$others[count(.|key('primary',                                        normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]" mode="index-symbol-div">
            <xsl:with-param name="position" select="position()"/>                                
            <xsl:with-param name="scope" select="$scope"/>
            <xsl:with-param name="role" select="$role"/>
            <xsl:with-param name="type" select="$type"/>
            <xsl:sort select="translate(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
          </xsl:apply-templates>
        </dl>
      </div>
    </xsl:if>

    <xsl:apply-templates select="$alphabetical[count(.|key('letter',                                  translate(substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1),                                            'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]" mode="index-div-english">
      <xsl:with-param name="position" select="position()"/>
      <xsl:with-param name="scope" select="$scope"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="type" select="$type"/>
      <xsl:sort select="translate(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    </xsl:apply-templates>
  </div>
</xsl:template>

<xsl:template name="generate-kosek-index">
  <xsl:param name="scope" select="(ancestor::book|/)[last()]"/>

  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
  <xsl:if test="contains($vendor, 'libxslt')">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kosek' index method does not </xsl:text>
      <xsl:text>work with the xsltproc XSLT processor.</xsl:text>
    </xsl:message>
  </xsl:if>


  <xsl:if test="not(function-available('exslt:node-set'))">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kosek' index method requires the </xsl:text>
      <xsl:text>exslt:node-set() function. Use a processor that </xsl:text>
      <xsl:text>has it, or use a different index method.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:if test="not(function-available('i:group-index'))">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kosek' index method requires the
</xsl:text>
      <xsl:text>index extension functions be imported:
</xsl:text>
      <xsl:text>  xsl:import href="common/autoidx-ng.xsl"</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:variable name="role">
    <xsl:if test="$index.on.role != 0">
      <xsl:value-of select="@role"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:if test="$index.on.type != 0">
      <xsl:value-of select="@type"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="terms" select="//indexterm[count(.|key('group-code',                                           i:group-index(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))))                                           [count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1                                     and not(@class = 'endofrange')]"/>

  <div class="index">
    <xsl:apply-templates select="$terms" mode="index-div-kosek">
      <xsl:with-param name="scope" select="$scope"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="type" select="$type"/>
      <xsl:sort select="i:group-index(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))" data-type="number"/>
    </xsl:apply-templates>
  </div>
</xsl:template>

<xsl:template name="generate-kimber-index">
  <xsl:param name="scope" select="NOTANODE"/>

  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
  <xsl:if test="not(contains($vendor, 'SAXON '))">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kimber' index method requires the </xsl:text>
      <xsl:text>Saxon version 6 XSLT processor.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:if test="not(function-available('k:getIndexGroupKey'))">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kimber' index method requires the </xsl:text>
      <xsl:text>Innodata Isogen 
Java extensions for </xsl:text>
      <xsl:text>internationalized indexes. 
Install those </xsl:text>
      <xsl:text>extensions, or use a different index method.
</xsl:text>
      <xsl:text>For more information, see:
</xsl:text>
      <xsl:text>http://www.innodata-isogen.com/knowledge_center/tools_downloads/i18nsupport</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:if test="not(function-available('i:group-index'))">
    <xsl:message terminate="yes">
      <xsl:text>ERROR: the 'kimber' index method requires the
</xsl:text>
      <xsl:text>index extension functions be imported:
</xsl:text>
      <xsl:text>  xsl:import href="common/autoidx-ng.xsl"</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:variable name="role">
    <xsl:if test="$index.on.role != 0">
      <xsl:value-of select="@role"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:if test="$index.on.type != 0">
      <xsl:value-of select="@type"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="terms" select="//indexterm[count(.|key('k-group',                    k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))))                    [count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1                    and not(@class = 'endofrange')]"/>

  <xsl:variable name="alphabetical" select="$terms[not(starts-with(                 k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))),                 '#NUMERIC'                 ))]"/>

  <xsl:variable name="others" select="$terms[starts-with(                 k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))),                 '#NUMERIC'                 )]"/>

  <div class="index">
    <xsl:if test="$others">
      <div class="indexdev">
        <h3>
          <xsl:call-template name="gentext">
            <xsl:with-param name="key" select="'index symbols'"/>
          </xsl:call-template>
        </h3>
        <dl>
          <xsl:apply-templates select="$others" mode="index-symbol-div">
            <xsl:with-param name="scope" select="$scope"/>
            <xsl:with-param name="role" select="$role"/>
            <xsl:with-param name="type" select="$type"/>
            <xsl:sort lang="concat(/*/@lang, /*/@xml:lang, &#34;en&#34;)" select="k:getIndexGroupSortKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;),                         k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))))"/>
          </xsl:apply-templates>
        </dl>
      </div>
    </xsl:if>

    <xsl:apply-templates select="$alphabetical" mode="index-div-kimber">
      <xsl:with-param name="scope" select="$scope"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="type" select="$type"/>
      <xsl:sort lang="concat(/*/@lang, /*/@xml:lang, &#34;en&#34;)" select="k:getIndexGroupSortKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;),                      k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))))"/>
    </xsl:apply-templates>
  </div>

</xsl:template>

<xsl:template match="indexterm" mode="index-div-english">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="translate(substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1),                          'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

  <xsl:if test="key('letter', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                 [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]">
    <div class="indexdiv">
      <xsl:if test="contains(concat('abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), $key)">
        <h3>
          <xsl:value-of select="translate($key, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </h3>
      </xsl:if>
      <dl>
        <xsl:apply-templates select="key('letter', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                                      [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))                                      [count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])=1]" mode="index-primary">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
      </dl>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="index-symbol-div">
  <xsl:param name="scope" select="/"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="translate(substring(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 1, 1),                                              'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

  <xsl:apply-templates select="key('letter', $key)                                [count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[1]) = 1]" mode="index-primary">
    <xsl:with-param name="position" select="position()"/>
    <xsl:with-param name="scope" select="$scope"/>
    <xsl:with-param name="role" select="$role"/>
    <xsl:with-param name="type" select="$type"/>
    <xsl:sort select="translate(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="indexterm" mode="index-div-kosek">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="i:group-index(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))"/>

  <xsl:variable name="lang">
    <xsl:call-template name="l10n.language"/>
  </xsl:variable>

  <xsl:if test="key('group-code', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                 [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]">
    <div class="indexdiv">
      <h3>
        <xsl:value-of select="i:group-letter($key)"/>
      </h3>
      <dl>
        <xsl:apply-templates select="key('group-code', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                                      [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])=1]" mode="index-primary">
          <xsl:sort select="normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))" lang="{$lang}"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
        </xsl:apply-templates>
      </dl>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="index-div-kimber">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="k:getIndexGroupKey(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))"/>

  <xsl:variable name="label" select="k:getIndexGroupLabel(concat(/*/@lang, /*/@xml:lang, &#34;en&#34;), $key)"/>

  <xsl:if test="key('k-group', $label)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                 [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]">
    <div class="indexdiv">
      <h3>
        <xsl:value-of select="$label"/>
      </h3>
      <dl>
        <xsl:apply-templates select="key('k-group', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                             [count(.|key('primary', normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]                             [1])=1]" mode="index-primary">
          <xsl:sort select="normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))" lang="concat(/*/@lang, /*/@xml:lang, &#34;en&#34;)"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
        </xsl:apply-templates>
      </dl>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="index-primary">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;]))"/>
  <xsl:variable name="refs" select="key('primary', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]"/>
  <dt>
    <xsl:value-of select="primary"/>
    <xsl:for-each select="$refs[generate-id() = generate-id(key('primary-section', concat($key, &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]">
      <xsl:apply-templates select="." mode="reference">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
      </xsl:apply-templates>
    </xsl:for-each>

    <xsl:if test="$refs[not(secondary)]/*[self::see]">
      <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, &#34; &#34;, &#34; &#34;, see))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-see">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
        <xsl:sort select="translate(see, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
      </xsl:apply-templates>
    </xsl:if>
  </dt>
  <xsl:if test="$refs/secondary or $refs[not(secondary)]/*[self::seealso]">
    <dd>
      <dl>
        <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, &#34; &#34;, &#34; &#34;, seealso))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-seealso">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(seealso, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="$refs[secondary and count(.|key('secondary', concat($key, &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;]))))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]" mode="index-secondary">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
      </dl>
    </dd>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="index-secondary">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])))"/>
  <xsl:variable name="refs" select="key('secondary', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]"/>
  <dt>
    <xsl:value-of select="secondary"/>
    <xsl:for-each select="$refs[generate-id() = generate-id(key('secondary-section', concat($key, &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]">
      <xsl:apply-templates select="." mode="reference">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
      </xsl:apply-templates>
    </xsl:for-each>

    <xsl:if test="$refs[not(tertiary)]/*[self::see]">
      <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, &#34; &#34;, see))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-see">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
        <xsl:sort select="translate(see, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
      </xsl:apply-templates>
    </xsl:if>
  </dt>
  <xsl:if test="$refs/tertiary or $refs[not(tertiary)]/*[self::seealso]">
    <dd>
      <dl>
        <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, &#34; &#34;, seealso))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-seealso">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(seealso, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="$refs[tertiary and count(.|key('tertiary', concat($key, &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;]))))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]) = 1]" mode="index-tertiary">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
      </dl>
    </dd>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="index-tertiary">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:variable name="key" select="concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])))"/>
  <xsl:variable name="refs" select="key('tertiary', $key)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]"/>
  <dt>
    <xsl:value-of select="tertiary"/>
    <xsl:for-each select="$refs[generate-id() = generate-id(key('tertiary-section', concat($key, &#34; &#34;, generate-id((ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()])))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]">
      <xsl:apply-templates select="." mode="reference">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
      </xsl:apply-templates>
    </xsl:for-each>

    <xsl:if test="$refs/see">
      <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, see))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-see">
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
        <xsl:sort select="translate(see, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
      </xsl:apply-templates>
    </xsl:if>
  </dt>
  <xsl:if test="$refs/seealso">
    <dd>
      <dl>
        <xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(normalize-space(concat(primary/@sortas, primary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(secondary/@sortas, secondary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas) or @sortas = &#34;&#34;])), &#34; &#34;, seealso))[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1])]" mode="index-seealso">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:sort select="translate(seealso, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        </xsl:apply-templates>
      </dl>
    </dd>
  </xsl:if>
</xsl:template>

<xsl:template match="indexterm" mode="reference">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>
  <xsl:param name="position"/>
  
  <xsl:variable name="term.separator">
    <xsl:call-template name="index.separator">
      <xsl:with-param name="key" select="'index.term.separator'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="number.separator">
    <xsl:call-template name="index.separator">
      <xsl:with-param name="key" select="'index.number.separator'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="range.separator">
    <xsl:call-template name="index.separator">
      <xsl:with-param name="key" select="'index.range.separator'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$position = 1">
      <xsl:value-of select="$term.separator"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$number.separator"/>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:choose>
    <xsl:when test="@zone and string(@zone)">
      <xsl:call-template name="reference">
        <xsl:with-param name="zones" select="normalize-space(@zone)"/>
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <a>
        <xsl:variable name="title">
          <xsl:choose>
            <xsl:when test="(ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]/titleabbrev and $index.prefer.titleabbrev != 0">
              <xsl:apply-templates select="(ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]" mode="titleabbrev.markup"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="(ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]" mode="title.markup"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <xsl:attribute name="href">
          <xsl:call-template name="href.target">
            <xsl:with-param name="object" select="(ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]"/>
            <xsl:with-param name="context" select="//index[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]"/>
          </xsl:call-template>
        </xsl:attribute>

        <xsl:value-of select="$title"/> <!-- text only -->
      </a>

      <xsl:if test="key('endofrange', @id)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]">
        <xsl:apply-templates select="key('endofrange', @id)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][last()]" mode="reference">
          <xsl:with-param name="position" select="position()"/>
          <xsl:with-param name="scope" select="$scope"/>
          <xsl:with-param name="role" select="$role"/>
          <xsl:with-param name="type" select="$type"/>
          <xsl:with-param name="separator" select="$range.separator"/>
        </xsl:apply-templates>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="reference">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>
  <xsl:param name="zones"/>

  <xsl:choose>
    <xsl:when test="contains($zones, ' ')">
      <xsl:variable name="zone" select="substring-before($zones, ' ')"/>
      <xsl:variable name="target" select="key('sections', $zone)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]"/>

      <a>
        <xsl:attribute name="href">
          <xsl:call-template name="href.target">
            <xsl:with-param name="object" select="$target[1]"/>
            <xsl:with-param name="context" select="//index[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]"/>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:apply-templates select="$target[1]" mode="index-title-content"/>
      </a>
      <xsl:text>, </xsl:text>
      <xsl:call-template name="reference">
        <xsl:with-param name="zones" select="substring-after($zones, ' ')"/>
        <xsl:with-param name="position" select="position()"/>
        <xsl:with-param name="scope" select="$scope"/>
        <xsl:with-param name="role" select="$role"/>
        <xsl:with-param name="type" select="$type"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="zone" select="$zones"/>
      <xsl:variable name="target" select="key('sections', $zone)[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))]"/>

      <a>
        <xsl:attribute name="href">
          <xsl:call-template name="href.target">
            <xsl:with-param name="object" select="$target[1]"/>
            <xsl:with-param name="context" select="//index[count(ancestor::node()|$scope) = count(ancestor::node())                 and ($role = @role or $type = @type or                 (string-length($role) = 0 and string-length($type) = 0))][1]"/>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:apply-templates select="$target[1]" mode="index-title-content"/>
      </a>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="indexterm" mode="index-see">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:text> (</xsl:text>
  <xsl:call-template name="gentext">
    <xsl:with-param name="key" select="'see'"/>
  </xsl:call-template>
  <xsl:text> </xsl:text>
  <xsl:value-of select="see"/>
  <xsl:text>)</xsl:text>
</xsl:template>

<xsl:template match="indexterm" mode="index-seealso">
  <xsl:param name="scope" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="type" select="''"/>

  <xsl:for-each select="seealso">
    <xsl:sort select="translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    <dt>
    <xsl:text>(</xsl:text>
    <xsl:call-template name="gentext">
      <xsl:with-param name="key" select="'seealso'"/>
    </xsl:call-template>
    <xsl:text> </xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>)</xsl:text>
    </dt>
  </xsl:for-each>
</xsl:template>

<xsl:template match="*" mode="index-title-content">
  <xsl:variable name="title">
    <xsl:apply-templates select="(ancestor-or-self::set                      |ancestor-or-self::book                      |ancestor-or-self::part                      |ancestor-or-self::reference                      |ancestor-or-self::partintro                      |ancestor-or-self::chapter                      |ancestor-or-self::appendix                      |ancestor-or-self::preface                      |ancestor-or-self::article                      |ancestor-or-self::section                      |ancestor-or-self::sect1                      |ancestor-or-self::sect2                      |ancestor-or-self::sect3                      |ancestor-or-self::sect4                      |ancestor-or-self::sect5                      |ancestor-or-self::refentry                      |ancestor-or-self::refsect1                      |ancestor-or-self::refsect2                      |ancestor-or-self::refsect3                      |ancestor-or-self::simplesect                      |ancestor-or-self::bibliography                      |ancestor-or-self::glossary                      |ancestor-or-self::index                      |ancestor-or-self::webpage)[last()]" mode="title.markup"/>
  </xsl:variable>

  <xsl:value-of select="$title"/>
</xsl:template>

<xsl:template name="index.separator">
  <xsl:param name="key" select="''"/>
  <xsl:param name="lang">
    <xsl:call-template name="l10n.language"/>
  </xsl:param>

  <xsl:choose>
    <xsl:when test="$key = 'index.term.separator'">
      <xsl:choose>
        <!-- Use the override if not blank -->
        <xsl:when test="$index.term.separator != ''">
          <xsl:copy-of select="$index.term.separator"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="gentext.template">
            <xsl:with-param name="lang" select="$lang"/>
            <xsl:with-param name="context">index</xsl:with-param>
            <xsl:with-param name="name">term-separator</xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$key = 'index.number.separator'">
      <xsl:choose>
        <!-- Use the override if not blank -->
        <xsl:when test="$index.number.separator != ''">
          <xsl:copy-of select="$index.number.separator"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="gentext.template">
            <xsl:with-param name="lang" select="$lang"/>
            <xsl:with-param name="context">index</xsl:with-param>
            <xsl:with-param name="name">number-separator</xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$key = 'index.range.separator'">
      <xsl:choose>
        <!-- Use the override if not blank -->
        <xsl:when test="$index.range.separator != ''">
          <xsl:copy-of select="$index.range.separator"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="gentext.template">
            <xsl:with-param name="lang" select="$lang"/>
            <xsl:with-param name="context">index</xsl:with-param>
            <xsl:with-param name="name">range-separator</xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
