charmap.xsl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
  4. xmlns:dyn="http://exslt.org/dynamic"
  5. xmlns:saxon="http://icl.com/saxon"
  6. xmlns:xlink="http://www.w3.org/1999/xlink"
  7. exclude-result-prefixes="doc dyn saxon"
  8. version='1.0'>
  9. <!-- ********************************************************************
  10. $Id: charmap.xsl 7266 2007-08-22 11:58:42Z xmldoc $
  11. ********************************************************************
  12. This file is part of the XSL DocBook Stylesheet distribution.
  13. See ../README or http://docbook.sf.net/release/xsl/current/ for
  14. copyright and other information.
  15. ******************************************************************** -->
  16. <doc:reference xmlns="" xml:id="charmap">
  17. <info>
  18. <title>Common » Character-Map Template Reference</title>
  19. <releaseinfo role="meta">
  20. $Id: charmap.xsl 7266 2007-08-22 11:58:42Z xmldoc $
  21. </releaseinfo>
  22. </info>
  23. <!-- * yes, partintro is a valid child of a reference... -->
  24. <partintro xml:id="partintro">
  25. <title>Introduction</title>
  26. <para>This is technical reference documentation for the
  27. character-map templates in the DocBook XSL Stylesheets.</para>
  28. <note>
  29. <para>These templates are defined in a separate file from the set
  30. of “common” templates because some of the common templates
  31. reference DocBook XSL stylesheet parameters, requiring the
  32. entire set of parameters to be imported/included in any
  33. stylesheet that imports/includes the common templates.</para>
  34. <para>The character-map templates don’t import or include
  35. any DocBook XSL stylesheet parameters, so the
  36. character-map templates can be used without importing the
  37. whole set of parameters.</para>
  38. </note>
  39. <para>This is not intended to be user documentation. It is
  40. provided for developers writing customization layers for the
  41. stylesheets.</para>
  42. </partintro>
  43. </doc:reference>
  44. <!-- ===================================== -->
  45. <doc:template name="apply-character-map" xmlns="">
  46. <refpurpose>Applies an XSLT character map</refpurpose>
  47. <refdescription id="apply-character-map-desc">
  48. <para>This template applies an <link
  49. xlink:href="http://www.w3.org/TR/xslt20/#character-maps"
  50. >XSLT character map</link>; that is, it causes certain
  51. individual characters to be substituted with strings of one
  52. or more characters. It is useful mainly for replacing
  53. multiple “special” characters or symbols in the same target
  54. content. It uses the value of
  55. <parameter>map.contents</parameter> to do substitution on
  56. <parameter>content</parameter>, and then returns the
  57. modified contents.</para>
  58. <note>
  59. <para>This template is a very slightly modified version of
  60. Jeni Tennison’s <function>replace_strings</function>
  61. template in the <link
  62. xlink:href="http://www.dpawson.co.uk/xsl/sect2/StringReplace.html#d9351e13"
  63. >multiple string replacements</link> section of Dave Pawson’s
  64. <link xlink:href="http://www.dpawson.co.uk/xsl/index.html"
  65. >XSLT FAQ</link>.</para>
  66. <para>The <function>apply-string-subst-map</function>
  67. template is essentially the same template as the
  68. <function>apply-character-map</function> template; the
  69. only difference is that in the map that
  70. <function>apply-string-subst-map</function> expects, <tag
  71. class="attribute">oldstring</tag> and <tag
  72. class="attribute">newstring</tag> attributes are used
  73. instead of <tag class="attribute">character</tag> and <tag
  74. class="attribute">string</tag> attributes.</para>
  75. </note>
  76. </refdescription>
  77. <refparameter id="apply-character-map-params">
  78. <variablelist>
  79. <varlistentry><term>content</term>
  80. <listitem>
  81. <para>The content on which to perform the character-map
  82. substitution.</para>
  83. </listitem>
  84. </varlistentry>
  85. <varlistentry><term>map.contents</term>
  86. <listitem>
  87. <para>A node set of elements, with each element having
  88. the following attributes:
  89. <itemizedlist>
  90. <listitem>
  91. <simpara><tag class="attribute">character</tag>, a
  92. character to be replaced</simpara>
  93. </listitem>
  94. <listitem>
  95. <simpara><tag class="attribute">string</tag>, a
  96. string with which to replace <tag
  97. class="attribute">character</tag></simpara>
  98. </listitem>
  99. </itemizedlist>
  100. </para>
  101. </listitem>
  102. </varlistentry>
  103. </variablelist>
  104. </refparameter>
  105. </doc:template>
  106. <xsl:template name="apply-character-map">
  107. <xsl:param name="content"/>
  108. <xsl:param name="map.contents"/>
  109. <xsl:variable name="replaced_text">
  110. <xsl:call-template name="string.subst">
  111. <xsl:with-param name="string" select="$content" />
  112. <xsl:with-param name="target"
  113. select="$map.contents[1]/@character" />
  114. <xsl:with-param name="replacement"
  115. select="$map.contents[1]/@string" />
  116. </xsl:call-template>
  117. </xsl:variable>
  118. <xsl:choose>
  119. <xsl:when test="$map.contents[2]">
  120. <xsl:call-template name="apply-character-map">
  121. <xsl:with-param name="content" select="$replaced_text" />
  122. <xsl:with-param name="map.contents"
  123. select="$map.contents[position() > 1]" />
  124. </xsl:call-template>
  125. </xsl:when>
  126. <xsl:otherwise>
  127. <xsl:value-of select="$replaced_text" />
  128. </xsl:otherwise>
  129. </xsl:choose>
  130. </xsl:template>
  131. <!-- ===================================== -->
  132. <doc:template name="read-character-map" xmlns="">
  133. <refpurpose>Reads in all or part of an XSLT character map</refpurpose>
  134. <refdescription id="read-character-map-desc">
  135. <para>The XSLT 2.0 specification describes <link
  136. xlink:href="http://www.w3.org/TR/xslt20/#character-maps"
  137. >character maps</link> and explains how they may be used
  138. to allow a specific character appearing in a text or
  139. attribute node in a final result tree to be substituted by
  140. a specified string of characters during serialization. The
  141. <function>read-character-map</function> template provides a
  142. means for reading and using character maps with XSLT
  143. 1.0-based tools.</para>
  144. <para>This template reads the character-map contents from
  145. <parameter>uri</parameter> (in full or in part, depending on
  146. the value of the <parameter>use.subset</parameter>
  147. parameter), then passes those contents to the
  148. <function>apply-character-map</function> template, along with
  149. <parameter>content</parameter>, the data on which to perform
  150. the character substitution.</para>
  151. <para>Using the character map “in part” means that it uses only
  152. those <tag>output-character</tag> elements that match the
  153. XPath expression given in the value of the
  154. <parameter>subset.profile</parameter> parameter. The current
  155. implementation of that capability here relies on the
  156. <function>evaluate</function> extension XSLT function.</para>
  157. </refdescription>
  158. <refparameter id="read-character-map-params">
  159. <variablelist>
  160. <varlistentry><term>use.subset</term>
  161. <listitem>
  162. <para>Specifies whether to use a subset of the character
  163. map instead of the whole map; boolean
  164. <literal>0</literal> or <literal>1</literal></para>
  165. </listitem>
  166. </varlistentry>
  167. <varlistentry><term>subset.profile</term>
  168. <listitem>
  169. <para>XPath expression that specifies what subset of the
  170. character map to use</para>
  171. </listitem>
  172. </varlistentry>
  173. <varlistentry><term>uri</term>
  174. <listitem>
  175. <para>URI for a character map</para>
  176. </listitem>
  177. </varlistentry>
  178. </variablelist>
  179. </refparameter>
  180. </doc:template>
  181. <xsl:template name="read-character-map">
  182. <xsl:param name="use.subset"/>
  183. <xsl:param name="subset.profile"/>
  184. <xsl:param name="uri"/>
  185. <xsl:choose>
  186. <xsl:when test="$use.subset != 0">
  187. <!-- *use a subset of the character map instead of the full map -->
  188. <xsl:choose>
  189. <!-- * xsltproc and Xalan both support dyn:evaluate() -->
  190. <xsl:when test="function-available('dyn:evaluate')">
  191. <xsl:copy-of select="document($uri)//*[local-name()='output-character']
  192. [dyn:evaluate($subset.profile)]"/>
  193. </xsl:when>
  194. <!-- * Saxon has its own evaluate() and doesn't support dyn:evaluate() -->
  195. <xsl:when test="function-available('saxon:evaluate')">
  196. <xsl:copy-of select="document($uri)//*[local-name()='output-character']
  197. [saxon:evaluate($subset.profile)]"/>
  198. </xsl:when>
  199. <xsl:otherwise>
  200. <xsl:message terminate="yes"
  201. >
  202. Error: To process character-map subsets, you must use an XSLT engine
  203. that supports the evaluate() XSLT extension function. Your XSLT engine
  204. does not support it.
  205. </xsl:message>
  206. </xsl:otherwise>
  207. </xsl:choose>
  208. </xsl:when>
  209. <xsl:otherwise>
  210. <!-- *value of $use.subset is non-zero, so use the full map -->
  211. <xsl:copy-of select="document($uri)//*[local-name()='output-character']"/>
  212. </xsl:otherwise>
  213. </xsl:choose>
  214. </xsl:template>
  215. </xsl:stylesheet>