Peter Lavin

Difficulty: ★☆☆ (easy)
Keywords: acronym, generated list

Problem

You have a glossary with acronyms and you want to generate a sorted list of these acronyms automatically.

Solution

The following prerequisites are needed:

  1. The element glossary and for each glossterm your acronym as shown in the following structure:

    <glossary version="5.0"
      xmlns="http://docbook.org/ns/docbook">
      <title>Acronym Test File</title>
      <info>
        <author>
          <personname>
            <firstname>Thomas</firstname>
            <surname>Schraitle</surname>
          </personname>
        </author>
      </info>
    
      <glossentry xml:id="xml">
        <acronym>XML</acronym>
        <glossdef>
          <para>Lore ipsum</para>
        </glossdef>
      </glossentry>
      <glossentry xml:id="pdf">
        <acronym>PDF</acronym>
        <glossdef>
          <para>Lore ipsum</para>
        </glossdef>
      </glossentry>
      <glossentry xml:id="oasis">
        <acronym>OASIS</acronym>
        <glossdef>
          <para>Lore ipsum</para>
        </glossdef>
      </glossentry>
      <glossentry xml:id="dtd">
        <acronym role="foo" xml:id="dtd-acronym">DTD</acronym>
        <glossdef>
          <para>Lore ipsum</para>
        </glossdef>
      </glossentry>
    </glossary>
  2. The following stylesheet to apply it to your XML structure:

    Example 3.22. make_acronyms.xsl
    <xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:d="http://docbook.org/ns/docbook"
      xmlns="http://docbook.org/ns/docbook"
      exclude-result-prefixes="d">
      
      <xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
      
      <xsl:param name="appendixid">acronyms</xsl:param>
      <xsl:param name="docbookversion">5.0</xsl:param>
      
      <xsl:template match="/">
        <!-- The comment below appears in the output file-->
    <xsl:comment>
     This file is dynamically generated at build time from a glossary file. 
     To change this file, make changes to the appropriate glossary.xml file. 
     In order to generate the acronyms file, any glossary entries with an 
     acronym tag must have an xml:id. 
    </xsl:comment>
        <appendix version="{$docbookversion}">
          <xsl:if test="$appendixid != ''">
            <xsl:attribute name="xml:id">
              <xsl:value-of select="$appendixid"/>
            </xsl:attribute>
          </xsl:if>
          <title>Acronyms</title>
          <para> This appendix displays acronyms sorted alphabetically and 
            linked to their definition in the glossary. </para>
          <xsl:apply-templates select=".//d:glossary[1]"/>
        </appendix>
      </xsl:template>
      
      <xsl:template match="d:glossary">
        <itemizedlist remap="glossary">
          <xsl:apply-templates select="d:glossentry[d:acronym]">
            <xsl:sort select="d:acronym"/>
          </xsl:apply-templates>
        </itemizedlist>
      </xsl:template>
      
      <xsl:template match="d:glossentry[d:acronym]">
        <listitem>
          <xsl:apply-templates select="d:acronym"/>
        </listitem>
      </xsl:template>
      
      <xsl:template match="d:acronym">
        <xsl:variable name="id">
          <xsl:choose>
            <xsl:when test="(ancestor-or-self::d:*/@xml:id)[last()]">
              <xsl:value-of select="(ancestor-or-self::d:*/@xml:id)[last()]"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:message>No ID found!</xsl:message>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
          <para>
            <xsl:copy>
              <xsl:if test="$id != ''">
                <xsl:attribute name="linkend">
                  <xsl:value-of select="$id"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:attribute name="remap">acronym</xsl:attribute>
              <xsl:copy-of select="node()"/>
            </xsl:copy>
          </para>
      </xsl:template>
        
    </xsl:stylesheet>

The result will look like this:

<!--
 This file is dynamically generated at build time from a glossary file.
 To change this file, make changes to the appropriate glossary.xml file.
 In order to generate the acronyms file, any glossary entries with an
 acronym tag must have an xml:id.
-->
<appendix xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="acronyms">
  <title>Acronyms</title>
  <para> This appendix displays acronyms sorted alphabetically and
        linked to their definition in the glossary. </para>
  <itemizedlist remap="glossary">
    <listitem>
      <para>
        <acronym linkend="dtd-acronym" remap="acronym">DTD</acronym>
      </para>
    </listitem>
    <listitem>
      <para>
        <acronym linkend="oasis" remap="acronym">OASIS</acronym>
      </para>
    </listitem>
    <listitem>
      <para>
        <acronym linkend="pdf" remap="acronym">PDF</acronym>
      </para>
    </listitem>
    <listitem>
      <para>
        <acronym linkend="xml" remap="acronym">XML</acronym>
      </para>
    </listitem>
  </itemizedlist>
</appendix>

Discussion

TBD


Project@GitHubIssue#8