# XSL Library Template Reference

### The DocBook Project

$Id: lib.xweb 6369 2006-10-19 08:40:35Z xmldoc $

Copyright © 19992000200120022005 by Norman Walsh. No Warranty.

## Introduction

This is technical reference documentation for the DocBook XSL Stylesheets; it documents (some of) the parameters, templates, and other elements of the stylesheets.

This is not intended to be “user” documentation. It is provided for developers writing customization layers for the stylesheets, and for anyone who's interested in “how it works”.

Although I am trying to be thorough, this documentation is known to be incomplete. Don't forget to read the source, too :-)

# General Library Templates

## Name

dot.count — Returns the number of “.” characters in a string

## Description

    <xsl:template name="dot.count">
      <!-- Returns the number of "." characters in a string -->
      <xsl:param name="string"></xsl:param>
      <xsl:param name="count" select="0"></xsl:param>
      <xsl:choose>
        <xsl:when test="contains($string, '.')">
          <xsl:call-template name="dot.count">
            <xsl:with-param name="string" select="substring-after($string, '.')"></xsl:with-param>
            <xsl:with-param name="count" select="$count+1"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$count"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

copy-string — Returns “count” copies of a string

## Description

    <xsl:template name="copy-string">
      <!-- returns 'count' copies of 'string' -->
      <xsl:param name="string"></xsl:param>
      <xsl:param name="count" select="0"></xsl:param>
      <xsl:param name="result"></xsl:param>

      <xsl:choose>
        <xsl:when test="$count>0">
          <xsl:call-template name="copy-string">
            <xsl:with-param name="string" select="$string"></xsl:with-param>
            <xsl:with-param name="count" select="$count - 1"></xsl:with-param>
            <xsl:with-param name="result">
              <xsl:value-of select="$result"></xsl:value-of>
              <xsl:value-of select="$string"></xsl:value-of>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$result"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

string.subst — Substitute one text string for another in a string

## Description

The `string.subst` template replaces all occurances of `target` in `string` with `replacement` and returns the result.

    <xsl:template name="string.subst">
      <xsl:param name="string"></xsl:param>
      <xsl:param name="target"></xsl:param>
      <xsl:param name="replacement"></xsl:param>

      <xsl:choose>
        <xsl:when test="contains($string, $target)">
          <xsl:variable name="rest">
            <xsl:call-template name="string.subst">
              <xsl:with-param name="string" select="substring-after($string, $target)"></xsl:with-param>
              <xsl:with-param name="target" select="$target"></xsl:with-param>
              <xsl:with-param name="replacement" select="$replacement"></xsl:with-param>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="concat(substring-before($string, $target),                                    $replacement,                                    $rest)"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$string"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

xpointer.idref — Extract IDREF from an XPointer

## Description

The `xpointer.idref` template returns the ID portion of an XPointer which is a pointer to an ID within the current document, or the empty string if it is not.

In other words, `xpointer.idref` returns “foo” when passed either `#foo` or `#xpointer(id('foo'))`, otherwise it returns the empty string.

    <xsl:template name="xpointer.idref">
      <xsl:param name="xpointer">http://...</xsl:param>
      <xsl:choose>
        <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
          <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"></xsl:variable>
          <xsl:variable name="quote" select="substring($rest, 1, 1)"></xsl:variable>
          <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"></xsl:value-of>
        </xsl:when>
        <xsl:when test="starts-with($xpointer, '#')">
          <xsl:value-of select="substring-after($xpointer, '#')"></xsl:value-of>
        </xsl:when>
        <!-- otherwise it's a pointer to some other document -->
      </xsl:choose>
    </xsl:template>

## Name

length-magnitude — Return the unqualified dimension from a length specification

## Description

The `length-magnitude` template returns the unqualified length ("20" for "20pt") from a dimension.

    <xsl:template name="length-magnitude">
      <xsl:param name="length" select="'0pt'"></xsl:param>

      <xsl:choose>
        <xsl:when test="string-length($length) = 0"></xsl:when>
        <xsl:when test="substring($length,1,1) = '0'                     or substring($length,1,1) = '1'                     or substring($length,1,1) = '2'                     or substring($length,1,1) = '3'                     or substring($length,1,1) = '4'                     or substring($length,1,1) = '5'                     or substring($length,1,1) = '6'                     or substring($length,1,1) = '7'                     or substring($length,1,1) = '8'                     or substring($length,1,1) = '9'                     or substring($length,1,1) = '.'">
          <xsl:value-of select="substring($length,1,1)"></xsl:value-of>
          <xsl:call-template name="length-magnitude">
            <xsl:with-param name="length" select="substring($length,2)"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
      </xsl:choose>
    </xsl:template>

## Name

length-units — Return the units from a length specification

## Description

The `length-units` template returns the units ("pt" for "20pt") from a length. If no units are supplied on the length, the `defauilt.units` are returned.

    <xsl:template name="length-units">
      <xsl:param name="length" select="'0pt'"></xsl:param>
      <xsl:param name="default.units" select="'px'"></xsl:param>
      <xsl:variable name="magnitude">
        <xsl:call-template name="length-magnitude">
          <xsl:with-param name="length" select="$length"></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="units">
        <xsl:value-of select="substring($length, string-length($magnitude)+1)"></xsl:value-of>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="$units = ''">
          <xsl:value-of select="$default.units"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$units"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

length-spec — Return a fully qualified length specification

## Description

The `length-spec` template returns the qualified length from a dimension. If an unqualified length is given, the `default.units` will be added to it.

    <xsl:template name="length-spec">
      <xsl:param name="length" select="'0pt'"></xsl:param>
      <xsl:param name="default.units" select="'px'"></xsl:param>

      <xsl:variable name="magnitude">
        <xsl:call-template name="length-magnitude">
          <xsl:with-param name="length" select="$length"></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="units">
        <xsl:value-of select="substring($length, string-length($magnitude)+1)"></xsl:value-of>
      </xsl:variable>

      <xsl:value-of select="$magnitude"></xsl:value-of>
      <xsl:choose>
        <xsl:when test="$units='cm'                     or $units='mm'                     or $units='in'                     or $units='pt'                     or $units='pc'                     or $units='px'                     or $units='em'">
          <xsl:value-of select="$units"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = ''">
          <xsl:value-of select="$default.units"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:message>
            <xsl:text>Unrecognized unit of measure: </xsl:text>
            <xsl:value-of select="$units"></xsl:value-of>
            <xsl:text>.</xsl:text>
          </xsl:message>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

length-in-points — Returns the size, in points, of a specified length

## Description

The `length-in-points` template converts a length specification to points and returns that value as an unqualified number.

There is no way for the template to infer the size of an `em`. It relies on the default `em.size` which is initially `10` (for 10pt). Similarly, converting pixels to points relies on the `pixels.per.inch` parameter which is initially `90`.

    <xsl:template name="length-in-points">
      <xsl:param name="length" select="'0pt'"></xsl:param>
      <xsl:param name="em.size" select="10"></xsl:param>
      <xsl:param name="pixels.per.inch" select="90"></xsl:param>

      <xsl:variable name="magnitude">
        <xsl:call-template name="length-magnitude">
          <xsl:with-param name="length" select="$length"></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="units">
        <xsl:value-of select="substring($length, string-length($magnitude)+1)"></xsl:value-of>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="$units = 'pt'">
          <xsl:value-of select="$magnitude"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'cm'">
          <xsl:value-of select="$magnitude div 2.54 * 72.0"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'mm'">
          <xsl:value-of select="$magnitude div 25.4 * 72.0"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'in'">
          <xsl:value-of select="$magnitude * 72.0"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'pc'">
          <xsl:value-of select="$magnitude * 12.0"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'px'">
          <xsl:value-of select="$magnitude div $pixels.per.inch * 72.0"></xsl:value-of>
        </xsl:when>
        <xsl:when test="$units = 'em'">
          <xsl:value-of select="$magnitude * $em.size"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:message>
            <xsl:text>Unrecognized unit of measure: </xsl:text>
            <xsl:value-of select="$units"></xsl:value-of>
            <xsl:text>.</xsl:text>
          </xsl:message>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

pi-attribute — Extract a pseudo-attribute from a PI

## Description

The `pi-attribute` template extracts a pseudo-attribute from a processing instruction. For example, given the PI “`<?foo bar="1" baz='red'?>`”,

    <xsl:call-template name="pi-attribute">
      <xsl:with-param name="pis" select="processing-instruction('foo')"/>
      <xsl:with-param name="attribute" select="'baz'"/>
    </xsl:call-template>

will return “red”. This template returns the first matching attribute that it finds. Presented with processing instructions that contain badly formed pseudo-attributes (missing or unbalanced quotes, for example), the template may silently return erroneous results.

    <xsl:template name="pi-attribute">
      <xsl:param name="pis" select="processing-instruction('BOGUS_PI')"></xsl:param>
      <xsl:param name="attribute">filename</xsl:param>
      <xsl:param name="count">1</xsl:param>

      <xsl:choose>
        <xsl:when test="$count>count($pis)">
          <!-- not found -->
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="pi">
            <xsl:value-of select="$pis[$count]"></xsl:value-of>
          </xsl:variable>
          <xsl:variable name="pivalue">
            <xsl:value-of select="concat(' ', normalize-space($pi))"></xsl:value-of>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="contains($pivalue,concat(' ', $attribute, '='))">
              <xsl:variable name="rest" select="substring-after($pivalue,concat(' ', $attribute,'='))"></xsl:variable>
              <xsl:variable name="quote" select="substring($rest,1,1)"></xsl:variable>
              <xsl:value-of select="substring-before(substring($rest,2),$quote)"></xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="pi-attribute">
                <xsl:with-param name="pis" select="$pis"></xsl:with-param>
                <xsl:with-param name="attribute" select="$attribute"></xsl:with-param>
                <xsl:with-param name="count" select="$count + 1"></xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

lookup.key — Retrieve the value associated with a particular key in a table

## Description

Given a table of space-delimited key/value pairs, the `lookup.key` template extracts the value associated with a particular key.

    <xsl:template name="lookup.key">
      <xsl:param name="key" select="''"></xsl:param>
      <xsl:param name="table" select="''"></xsl:param>

      <xsl:if test="contains($table, ' ')">
        <xsl:choose>
          <xsl:when test="substring-before($table, ' ') = $key">
            <xsl:variable name="rest" select="substring-after($table, ' ')"></xsl:variable>
            <xsl:choose>
              <xsl:when test="contains($rest, ' ')">
                <xsl:value-of select="substring-before($rest, ' ')"></xsl:value-of>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$rest"></xsl:value-of>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="lookup.key">
              <xsl:with-param name="key" select="$key"></xsl:with-param>
              <xsl:with-param name="table" select="substring-after(substring-after($table,' '), ' ')"></xsl:with-param>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </xsl:template>

## Name

xpath.location — Calculate the XPath child-sequence to the current node

## Description

The `xpath.location` template calculates the absolute path from the root of the tree to the current element node.

    <xsl:template name="xpath.location">
      <xsl:param name="node" select="."></xsl:param>
      <xsl:param name="path" select="''"></xsl:param>

      <xsl:variable name="next.path">
        <xsl:value-of select="local-name($node)"></xsl:value-of>
        <xsl:if test="$path != ''">/</xsl:if>
        <xsl:value-of select="$path"></xsl:value-of>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="$node/parent::*">
          <xsl:call-template name="xpath.location">
            <xsl:with-param name="node" select="$node/parent::*"></xsl:with-param>
            <xsl:with-param name="path" select="$next.path"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>/</xsl:text>
          <xsl:value-of select="$next.path"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

comment-escape-string — Prepare a string for inclusion in an XML comment

## Description

The `comment-escape-string` template returns a string that has been transformed so that it can safely be output as an XML comment. Internal occurrences of "--" will be replaced with "- -" and a leading and/or trailing space will be added to the string, if necessary.

    <xsl:template name="comment-escape-string">
      <xsl:param name="string" select="''"></xsl:param>

      <xsl:if test="starts-with($string, '-')">
        <xsl:text> </xsl:text>
      </xsl:if>

      <xsl:call-template name="comment-escape-string.recursive">
        <xsl:with-param name="string" select="$string"></xsl:with-param>
      </xsl:call-template>

      <xsl:if test="substring($string, string-length($string), 1) = '-'">
        <xsl:text> </xsl:text>
      </xsl:if>
    </xsl:template>

## Name

comment-escape-string.recursive — Internal function used by comment-escape-string

## Description

The `comment-escape-string.recursive` template is used by `comment-escape-string`.

    <xsl:template name="comment-escape-string.recursive">
      <xsl:param name="string" select="''"></xsl:param>
      <xsl:choose>
        <xsl:when test="contains($string, '--')">
          <xsl:value-of select="substring-before($string, '--')"></xsl:value-of>
          <xsl:value-of select="'- -'"></xsl:value-of>
          <xsl:call-template name="comment-escape-string.recursive">
            <xsl:with-param name="string" select="substring-after($string, '--')"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$string"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

prepend-pad — Right-pad a string out to a certain length

## Description

This function takes string `padVar` and pads it out to the string-length `length`, using string `padChar` (a space character by default) as the padding string (note that `padChar` can be a string; it is not limited to just being a single character).

This function is a copy of Nate Austin's `prepend-pad` function in the Padding Content section of Dave Pawson's XSLT FAQ.

      <xsl:template name="prepend-pad">
      <!-- recursive template to right justify and prepend-->
      <!-- the value with whatever padChar is passed in   -->
        <xsl:param name="padChar" select="' '"></xsl:param>
        <xsl:param name="padVar"></xsl:param>
        <xsl:param name="length"></xsl:param>
        <xsl:choose>
          <xsl:when test="string-length($padVar) < $length">
            <xsl:call-template name="prepend-pad">
              <xsl:with-param name="padChar" select="$padChar"></xsl:with-param>
              <xsl:with-param name="padVar" select="concat($padChar,$padVar)"></xsl:with-param>
              <xsl:with-param name="length" select="$length"></xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring($padVar,string-length($padVar) - $length + 1)"></xsl:value-of>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

## Name

trim.text — Trim leading and trailing whitespace from a text node

## Description

Given a text node, this function trims leading and trailing whitespace from it and returns the trimmed contents.

      <xsl:template name="trim.text">
        <xsl:param name="contents" select="."></xsl:param>
        <xsl:variable name="contents-left-trimmed">
          <xsl:call-template name="trim-left">
            <xsl:with-param name="contents" select="$contents"></xsl:with-param>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="contents-trimmed">
          <xsl:call-template name="trim-right">
            <xsl:with-param name="contents" select="$contents-left-trimmed"></xsl:with-param>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$contents-trimmed"></xsl:value-of>
      </xsl:template>

      <xsl:template name="trim-left">
        <xsl:param name="contents"></xsl:param>
        <xsl:choose>
          <xsl:when test="starts-with($contents,'
    ') or                       starts-with($contents,'
    ') or                       starts-with($contents,' ') or                       starts-with($contents,'	')">
            <xsl:call-template name="trim-left">
              <xsl:with-param name="contents" select="substring($contents, 2)"></xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$contents"></xsl:value-of>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template name="trim-right">
        <xsl:param name="contents"></xsl:param>
        <xsl:variable name="last-char">
          <xsl:value-of select="substring($contents, string-length($contents), 1)"></xsl:value-of>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="($last-char = '
    ') or                       ($last-char = '') or                       ($last-char = ' ') or                       ($last-char = '	')">
            <xsl:call-template name="trim-right">
              <xsl:with-param name="contents" select="substring($contents, 1, string-length($contents) - 1)"></xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$contents"></xsl:value-of>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

## Name

str.tokenize.keep.delimiters — Tokenize a string while preserving any delimiters

## Description

Based on the occurrence of one or more delimiter characters, this function breaks a string into a list of tokens and delimiters, marking up each of the tokens with a `token` element and preserving the delimiters as text nodes between the tokens.

This function is a very slightly modified version of a function from the EXSLT site. The original is available at: http://www.exslt.org/str/functions/tokenize/str.tokenize.template.xsl The `str.tokenize.keep.delimiters` function differs only in that it preserves the delimiters instead of discarding them.

      <xsl:template name="str.tokenize.keep.delimiters">
        <xsl:param name="string" select="''"></xsl:param>
        <xsl:param name="delimiters" select="' '"></xsl:param>
        <xsl:choose>
          <xsl:when test="not($string)"></xsl:when>
          <xsl:when test="not($delimiters)">
    	<xsl:call-template name="str.tokenize.keep.delimiters-characters">
    	  <xsl:with-param name="string" select="$string"></xsl:with-param>
    	</xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
    	<xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
    	  <xsl:with-param name="string" select="$string"></xsl:with-param>
    	  <xsl:with-param name="delimiters" select="$delimiters"></xsl:with-param>
    	</xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template name="str.tokenize.keep.delimiters-characters">
        <xsl:param name="string"></xsl:param>
        <xsl:if test="$string">
          <token><xsl:value-of select="substring($string, 1, 1)"></xsl:value-of></token>
          <xsl:call-template name="str.tokenize.keep.delimiters-characters">
    	<xsl:with-param name="string" select="substring($string, 2)"></xsl:with-param>
          </xsl:call-template>
        </xsl:if>
      </xsl:template>

      <xsl:template name="str.tokenize.keep.delimiters-delimiters">
        <xsl:param name="string"></xsl:param>
        <xsl:param name="delimiters"></xsl:param>
        <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)"></xsl:variable>
        <xsl:choose>
          <xsl:when test="not($delimiter)">
    	<token><xsl:value-of select="$string"></xsl:value-of></token>
          </xsl:when>
          <xsl:when test="contains($string, $delimiter)">
    	<xsl:if test="not(starts-with($string, $delimiter))">
    	  <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
    	    <xsl:with-param name="string" select="substring-before($string, $delimiter)"></xsl:with-param>
    	    <xsl:with-param name="delimiters" select="substring($delimiters, 2)"></xsl:with-param>
    	  </xsl:call-template>
    	</xsl:if>
    	<!-- output each delimiter -->
    	<xsl:value-of select="$delimiter"></xsl:value-of>
    	<xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
    	  <xsl:with-param name="string" select="substring-after($string, $delimiter)"></xsl:with-param>
    	  <xsl:with-param name="delimiters" select="$delimiters"></xsl:with-param>
    	</xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
    	<xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
    	  <xsl:with-param name="string" select="$string"></xsl:with-param>
    	  <xsl:with-param name="delimiters" select="substring($delimiters, 2)"></xsl:with-param>
    	</xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

## Name

apply-string-subst-map — Apply a string-substitution map

## Description

This function applies a "string substitution" map. Use it when you want to do multiple string substitutions on the same target content. It reads in two things: `content`, the content on which to perform the substitution, and `map.contents`, a node set of elements (the names of the elements don't matter), with each element having the following attributes:

- `oldstring`, a string to be replaced
- `newstring`, a string with which to replace `oldstring`

The function uses `map.contents` to do substitution on `content`, and then returns the modified contents.

This function is a very slightly modified version of Jeni Tennison's `replace_strings` function in the multiple string replacements section of Dave Pawson's XSLT FAQ. The `apply-string-subst-map` function is essentially the same function as the `apply-character-map` function; the only difference is that in the map that `apply-string-subst-map` expects, `oldstring` and `newstring` attributes are used instead of `character` and `string` attributes.

        <xsl:template name="apply-string-subst-map">
          <xsl:param name="content"></xsl:param>
          <xsl:param name="map.contents"></xsl:param>
          <xsl:variable name="replaced_text">
            <xsl:call-template name="string.subst">
              <xsl:with-param name="string" select="$content"></xsl:with-param>
              <xsl:with-param name="target" select="$map.contents[1]/@oldstring"></xsl:with-param>
              <xsl:with-param name="replacement" select="$map.contents[1]/@newstring"></xsl:with-param>
            </xsl:call-template>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="$map.contents[2]">
              <xsl:call-template name="apply-string-subst-map">
                <xsl:with-param name="content" select="$replaced_text"></xsl:with-param>
                <xsl:with-param name="map.contents" select="$map.contents[position() > 1]"></xsl:with-param>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$replaced_text"></xsl:value-of>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>

## Name

apply-character-map — Apply an XSLT character map

## Description

This function applies an XSLT character map; that is, it cause certain individual characters to be substituted with strings of one or more characters. It is useful mainly for replacing multiple "special" chararacters or symbols in the same target content. It reads in two things: `content`, the content on which to perform the substitution, and `map.contents`, a node set of elements (the names of the elements don't matter), with each element having the following attributes:

- `character`, a character to be replaced
- `string`, a string with which to replace `character`

This function uses `map.contents` to do substitution on `content`, and then returns the modified contents.

This function is a very slightly modified version of Jeni Tennison's `replace_strings` function in the multiple string replacements section of Dave Pawson's XSLT FAQ. The `apply-string-subst-map` function is essentially the same function as the `apply-character-map` function; the only difference is that in the map that `apply-string-subst-map` expects, `oldstring` and `newstring` attributes are used instead of `character` and `string` attributes.

        <xsl:template name="apply-character-map">
          <xsl:param name="content"></xsl:param>
          <xsl:param name="map.contents"></xsl:param>
          <xsl:variable name="replaced_text">
            <xsl:call-template name="string.subst">
              <xsl:with-param name="string" select="$content"></xsl:with-param>
              <xsl:with-param name="target" select="$map.contents[1]/@character"></xsl:with-param>
              <xsl:with-param name="replacement" select="$map.contents[1]/@string"></xsl:with-param>
            </xsl:call-template>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="$map.contents[2]">
              <xsl:call-template name="apply-character-map">
                <xsl:with-param name="content" select="$replaced_text"></xsl:with-param>
                <xsl:with-param name="map.contents" select="$map.contents[position() > 1]"></xsl:with-param>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$replaced_text"></xsl:value-of>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>

## Name

read-character-map — Read in all or part of an XSLT character map

## Description

The XSLT 2.0 specification describes character maps and explains how they may be used to allow a specific character appearing in a text or attribute node in a final results tree to be substituted by a specified string of characters during serialization. The `read-character-map` function provides a means for reading and using character maps with XSLT 1.0-based tools.

It reads the character-map contents from `uri` (in full or in part, depending on the value of the `use.subset` parameter), then passes those contents to the `apply-character-map` function, along with `content`, the data on which to perform the character substition.

Using the character map "in part" means that it uses only those `output-character` elements that match the XPATH expression given in the value of the `subset.profile` parameter. The current implementation of that capability here relies on the `evaluate` extension XSLT function.

      <xsl:template name="read-character-map">
        <xsl:param name="use.subset"></xsl:param>
        <xsl:param name="subset.profile"></xsl:param>
        <xsl:param name="uri"></xsl:param>
        <xsl:choose>
          <xsl:when test="$use.subset != 0">
            <!-- use a subset of the character map instead of the full map -->
            <xsl:choose>
              <!-- xsltproc and Xalan both support dyn:evaluate() -->
              <xsl:when test="function-available('dyn:evaluate')">
                <xsl:copy-of select="document($uri)//*[local-name()='output-character']                                  [dyn:evaluate($subset.profile)]"></xsl:copy-of>
              </xsl:when>
              <!-- Saxon has its own evaluate() & doesn't support dyn:evaluate() -->
              <xsl:when test="function-available('saxon:evaluate')">
                <xsl:copy-of select="document($uri)//*[local-name()='output-character']                                  [saxon:evaluate($subset.profile)]"></xsl:copy-of>
              </xsl:when>
              <xsl:otherwise>
                <xsl:message terminate="yes">
    Error: To process character-map subsets, you must use an XSLT engine
    that supports the evaluate() XSLT extension function. Your XSLT engine
    does not support it.
    </xsl:message>
              </xsl:otherwise>
            </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <!-- value of $use.subset is non-zero, so use the full map -->
            <xsl:copy-of select="document($uri)//*[local-name()='output-character']"></xsl:copy-of>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

# Relative URI Functions

# Introduction

These functions manipulate relative URI references.

The following assumptions must hold true:

1. All URIs are relative.

2. No URI contains the “`../`” sequence which would effectively move “up” the hierarchy.

If these assumptions do not hold, the results are unpredictable.

## Name

count.uri.path.depth — Count the number of path components in a relative URI

## Description

This function counts the number of path components in a relative URI.

    <xsl:template name="count.uri.path.depth">
      <xsl:param name="filename" select="''"></xsl:param>
      <xsl:param name="count" select="0"></xsl:param>

      <xsl:choose>
        <xsl:when test="contains($filename, '/')">
          <xsl:call-template name="count.uri.path.depth">
            <xsl:with-param name="filename" select="substring-after($filename, '/')"></xsl:with-param>
            <xsl:with-param name="count" select="$count + 1"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$count"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Name

trim.common.uri.paths — Trim common leading path components from a relative URI

## Description

This function trims common leading path components from a relative URI.

    <xsl:template name="trim.common.uri.paths">
      <xsl:param name="uriA" select="''"></xsl:param>
      <xsl:param name="uriB" select="''"></xsl:param>
      <xsl:param name="return" select="'A'"></xsl:param>

      <xsl:choose>
        <xsl:when test="contains($uriA, '/') and contains($uriB, '/')                     and substring-before($uriA, '/') = substring-before($uriB, '/')">
          <xsl:call-template name="trim.common.uri.paths">
            <xsl:with-param name="uriA" select="substring-after($uriA, '/')"></xsl:with-param>
            <xsl:with-param name="uriB" select="substring-after($uriB, '/')"></xsl:with-param>
            <xsl:with-param name="return" select="$return"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="$return = 'A'">
              <xsl:value-of select="$uriA"></xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$uriB"></xsl:value-of>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

## Appendix A. The Stylesheet

The `lib.xsl` stylesheet is just a wrapper around these functions.

    <!-- ********************************************************************
         $Id: lib.xweb 6369 2006-10-19 08:40:35Z xmldoc $
         ********************************************************************

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

         This module implements DTD-independent functions

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

    <xsl:stylesheet exclude-result-prefixes="src" version="1.0">

    <src:fragref linkend="dot.count.frag"></src:fragref>
    <src:fragref linkend="copy-string.frag"></src:fragref>
    <src:fragref linkend="string.subst.frag"></src:fragref>
    <src:fragref linkend="xpointer.idref.frag"></src:fragref>
    <src:fragref linkend="length-magnitude.frag"></src:fragref>
    <src:fragref linkend="length-units.frag"></src:fragref>
    <src:fragref linkend="length-spec.frag"></src:fragref>
    <src:fragref linkend="length-in-points.frag"></src:fragref>
    <src:fragref linkend="pi-attribute.frag"></src:fragref>
    <src:fragref linkend="lookup.key.frag"></src:fragref>
    <src:fragref linkend="xpath.location.frag"></src:fragref>
    <src:fragref linkend="comment-escape-string.frag"></src:fragref>
    <src:fragref linkend="comment-escape-string.recursive.frag"></src:fragref>
    <src:fragref linkend="prepend-pad.frag"></src:fragref>
    <src:fragref linkend="str.tokenize.keep.delimiters.frag"></src:fragref>
    <src:fragref linkend="apply-string-subst-map.frag"></src:fragref>
    <src:fragref linkend="apply-character-map.frag"></src:fragref>
    <src:fragref linkend="read-character-map.frag"></src:fragref>
    <src:fragref linkend="count.uri.path.depth.frag"></src:fragref>
    <src:fragref linkend="trim.common.uri.paths.frag"></src:fragref>
    <src:fragref linkend="trim.text.frag"></src:fragref>

    </xsl:stylesheet>