XML technologies / DocBook / Conditional elements (profiling) / Automatic insertion through profiling
(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)
Profiling is primarily intended to remove things. However, profiling can also insert things automatically. This section describes the method for both DocBook 4 and DocBook 5.
Suppose your document contains environment variables that should appear in the index. One option is to enter each index entry manually using indexterm. This is tedious, time-consuming, and error-prone. A smarter alternative is profiling: each envar element automatically receives an indexterm. Proceed as follows:
Using the
envarelement
Use the elementenvarto mark up your environment variables. By default, allenvarelements are indexed. For those that should not appear in the index, add the attributeconditionwith the valuenoindex.Creating the stylesheet for automatic insertion
Create a stylesheetprofile-tags.xslwith the following contents:
For DocBook 4:
<xsl:stylesheet version="1.0" xmlns:exsl="http://exslt.org/common" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="exsl">
<xsl:template name="check.index">
<xsl:param name="node" select="."/>
<xsl:choose>
<xsl:when test="$node/@condition = 'noindex'">0</xsl:when>
<xsl:when test="$node/@condition = 'index'">1</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="envar" mode="profile">
<xsl:variable name="do.index">
<xsl:call-template name="check.index"/>
</xsl:variable>
<xsl:variable name="profile.node.rtf">
<xsl:apply-imports/>
</xsl:variable>
<xsl:variable name="profile.node" select="exsl:node-set($profile.node.rtf)/*"/>
<xsl:if test="$profile.node != 0">
<xsl:copy-of select="$profile.node"/>
<xsl:if test="$do.index != 0">
<indexterm>
<primary>Umgebungsvariablen</primary>
<secondary>
<xsl:value-of select="."/>
</secondary>
</indexterm>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match="footnote|indexterm" mode="profile">
<xsl:variable name="profile.node.rtf">
<xsl:apply-imports/>
</xsl:variable>
<xsl:variable name="profile.node" select="exsl:node-set($profile.node.rtf)/*"/>
<xsl:if test="$profile.node != 0">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>Line 1: The root element xsl:stylesheet contains the namespace declarations for XSLT and EXSLT. The latter is required for the extension function exsl:node-set.
Line 2: Checks whether the attribute condition exists. Depending on this and whether it has the value index or noindex, returns 0 or 1.
Line 10: template rule for the element envar. This must run in the mode profile to be recognized by the profiling stylesheet.
Line 11: Stores in the variable do.index the result of calling check.index.
Line 14: Uses xsl:apply-imports to apply the original template rule. Depending on the profiling attribute, the element is retained or removed. Only if it is removed is the result tree fragment (RTF) empty.
Line 17: Converts the preceding RTF into a node-set. This is needed to process the contents of the variable profile.node.rtf using XPath.
Line 18: Tests whether the element survived profiling. If so, it is copied to the output. The second test checks whether to add the automatic index entry.
Line 30: Matches Footnotes and index entries, which need special handling. Neither may contain index entries. This would happen if a footnote contained an envar element (the same applies to indexterm). If either footnote or indexterm survives profiling, it is copied.
For DocBook 5, the stylesheet has the same structure; simply add the prefixes and the namespace for DocBook 5:
<xsl:stylesheet version="1.0" xmlns:d="http://docbook.org/ns/docbook" xmlns="http://docbook.org/ns/docbook" xmlns:exsl="http://exslt.org/common" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="exsl">
<xsl:template name="check.index">
<xsl:param name="node" select="."/>
<xsl:choose>
<xsl:when test="$node/@condition = 'noindex'">0</xsl:when>
<xsl:when test="$node/@condition = 'index'">1</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="d:envar" mode="profile">
<xsl:variable name="do.index">
<xsl:call-template name="check.index"/>
</xsl:variable>
<xsl:variable name="profile.node.rtf">
<xsl:apply-imports/>
</xsl:variable>
<xsl:variable name="profile.node" select="exsl:node-set($profile.node.rtf)/*"/>
<xsl:if test="$profile.node != 0">
<xsl:copy-of select="$profile.node"/>
<xsl:if test="$do.index != 0">
<d:indexterm>
<d:primary>Umgebungsvariablen</d:primary>
<d:secondary>
<xsl:value-of select="."/>
</d:secondary>
</d:indexterm>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match="d:footnote|d:indexterm" mode="profile">
<xsl:variable name="profile.node.rtf">
<xsl:apply-imports/>
</xsl:variable>
<xsl:variable name="profile.node" select="exsl:node-set($profile.node.rtf)/*"/>
<xsl:if test="$profile.node != 0">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>Creating the stylesheet for two-step profiling
To use either one-step or two-step profiling, include the stylesheet created above in a parent stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:import href="DBBASISURI/profiling/profile.xsl"/>
<xsl:output indent="yes" method="xml"/>
<xsl:include href="profile-tags.xsl"/>
</xsl:stylesheet>Line 2: Import the original profiling stylesheet. Replace the placeholder DBBASISURI according to the DocBook version you use.
Line 3: Optional output element. By default, no DOCTYPE declaration is inserted. If you prefer one for DocBook 4, add the attributes doctype-public and doctype-system and enter the appropriate values.
Line 4: Include your customizations from the file profile-tags.xsl.
Creating the stylesheet for one-step profiling
Use the following stylesheet (profile-docbook-html.xsl) to produce HTML:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="DBBASISURI/html/profile-docbook.xsl"/>
<xsl:include href="profile-tags.xsl"/>
</xsl:stylesheet>This is demonstrated using a DocBook 5 document:
<article version="5.0" xml:lang="de" xmlns="http://docbook.org/ns/docbook">
<title>Profiling-Test 01</title>
<para>Umgebungsvariable <envar>XML_CATALOG_FILES</envar>.</para>
<para>Umgebungsvariable <envar condition="noindex">FOO</envar>.</para>
<para>Umgebungsvariable <envar os="win">Path</envar><envar os="lin">PATH</envar></para>
<index/>
</article>To demonstrate how the stylesheets work, assume that Linux documentation is required. Run:
xsltproc --stringparam profile.os "lin" profile.xsl article.xml The result is:
<article xmlns="http://docbook.org/ns/docbook" xml:lang="de" version="5.0">
<title>Profiling-Test 01</title>
<para>Umgebungsvariable <envar>XML_CATALOG_FILES</envar><indexterm><primary>Umgebungsvariablen</primary><secondary>XML_CATALOG_FILES</secondary></indexterm>.</para>
<para>Umgebungsvariable <envar condition="noindex">FOO</envar>.</para>
<para>Umgebungsvariable <envar os="lin">PATH</envar><indexterm><primary>Umgebungsvariablen</primary><secondary>PATH</secondary></indexterm></para>
<index/>
</article> As you can see, the envar element with win in the attribute os has been filtered out. Index entries have also been inserted automatically, except for FOO.
This method also works for other elements you want to index automatically, such as abbrev, acronym, person/ personname and others.
Copyright © 2009 Millin Verlag
You may print the online version for your private use.
Otherwise, this chapter from "DocBook-XML: Medienneutrales und plattformunabhängiges Publizieren" is subject to the same provisions as the printed edition: the work, including all its parts, is protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Millin Verlag, Siebengebirgsring 36, 53797 Lohmar, info(at)millin.de
back to top