footnote.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:exsl="http://exslt.org/common"
  4. exclude-result-prefixes="exsl"
  5. version='1.0'>
  6. <!-- ********************************************************************
  7. $Id: footnote.xsl 9665 2012-11-08 14:26:20Z kosek $
  8. ********************************************************************
  9. This file is part of the XSL DocBook Stylesheet distribution.
  10. See ../README or http://docbook.sf.net/release/xsl/current/ for
  11. copyright and other information.
  12. ******************************************************************** -->
  13. <!-- This template generates just the footnote marker inline.
  14. The footnote text is handled in name="process.footnote".
  15. The footnote marker gets an id of @id, while the
  16. footnote text gets an id of #ftn.@id. They cross link to each other. -->
  17. <xsl:template match="footnote">
  18. <xsl:variable name="name">
  19. <xsl:call-template name="object.id">
  20. <xsl:with-param name="conditional" select="0"/>
  21. </xsl:call-template>
  22. </xsl:variable>
  23. <xsl:variable name="href">
  24. <xsl:text>#ftn.</xsl:text>
  25. <xsl:value-of select="$name"/>
  26. </xsl:variable>
  27. <a href="{$href}">
  28. <xsl:apply-templates select="." mode="class.attribute"/>
  29. <xsl:if test="$generate.id.attributes = 0">
  30. <xsl:attribute name="name">
  31. <xsl:value-of select="$name"/>
  32. </xsl:attribute>
  33. </xsl:if>
  34. <sup>
  35. <xsl:apply-templates select="." mode="class.attribute"/>
  36. <xsl:call-template name="id.attribute">
  37. <xsl:with-param name="conditional" select="0"/>
  38. </xsl:call-template>
  39. <xsl:text>[</xsl:text>
  40. <xsl:apply-templates select="." mode="footnote.number"/>
  41. <xsl:text>]</xsl:text>
  42. </sup>
  43. </a>
  44. </xsl:template>
  45. <xsl:template match="footnoteref">
  46. <xsl:variable name="targets" select="key('id',@linkend)"/>
  47. <xsl:variable name="footnote" select="$targets[1]"/>
  48. <xsl:if test="not(local-name($footnote) = 'footnote')">
  49. <xsl:message terminate="yes">
  50. ERROR: A footnoteref element has a linkend that points to an element that is not a footnote.
  51. Typically this happens when an id attribute is accidentally applied to the child of a footnote element.
  52. target element: <xsl:value-of select="local-name($footnote)"/>
  53. linkend/id: <xsl:value-of select="@linkend"/>
  54. </xsl:message>
  55. </xsl:if>
  56. <xsl:variable name="target.href">
  57. <xsl:call-template name="href.target">
  58. <xsl:with-param name="object" select="$footnote"/>
  59. </xsl:call-template>
  60. </xsl:variable>
  61. <xsl:variable name="href">
  62. <xsl:value-of select="substring-before($target.href, '#')"/>
  63. <xsl:text>#ftn.</xsl:text>
  64. <xsl:value-of select="substring-after($target.href, '#')"/>
  65. </xsl:variable>
  66. <a href="{$href}">
  67. <xsl:apply-templates select="." mode="class.attribute"/>
  68. <xsl:call-template name="id.attribute"/>
  69. <sup>
  70. <xsl:apply-templates select="." mode="class.attribute"/>
  71. <xsl:text>[</xsl:text>
  72. <xsl:apply-templates select="$footnote" mode="footnote.number"/>
  73. <xsl:text>]</xsl:text>
  74. </sup>
  75. </a>
  76. </xsl:template>
  77. <xsl:template match="footnote" mode="footnote.number">
  78. <xsl:choose>
  79. <xsl:when test="string-length(@label) != 0">
  80. <xsl:value-of select="@label"/>
  81. </xsl:when>
  82. <xsl:when test="ancestor::table or ancestor::informaltable">
  83. <xsl:variable name="tfnum">
  84. <xsl:number level="any" from="table|informaltable" format="1"/>
  85. </xsl:variable>
  86. <xsl:choose>
  87. <xsl:when test="string-length($table.footnote.number.symbols) &gt;= $tfnum">
  88. <xsl:value-of select="substring($table.footnote.number.symbols, $tfnum, 1)"/>
  89. </xsl:when>
  90. <xsl:otherwise>
  91. <xsl:number level="any" from="table | informaltable"
  92. format="{$table.footnote.number.format}"/>
  93. </xsl:otherwise>
  94. </xsl:choose>
  95. </xsl:when>
  96. <xsl:otherwise>
  97. <xsl:variable name="pfoot" select="preceding::footnote[not(@label)]"/>
  98. <xsl:variable name="ptfoot" select="preceding::table//footnote |
  99. preceding::informaltable//footnote"/>
  100. <xsl:variable name="fnum" select="count($pfoot) - count($ptfoot) + 1"/>
  101. <xsl:choose>
  102. <xsl:when test="string-length($footnote.number.symbols) &gt;= $fnum">
  103. <xsl:value-of select="substring($footnote.number.symbols, $fnum, 1)"/>
  104. </xsl:when>
  105. <xsl:otherwise>
  106. <xsl:number value="$fnum" format="{$footnote.number.format}"/>
  107. </xsl:otherwise>
  108. </xsl:choose>
  109. </xsl:otherwise>
  110. </xsl:choose>
  111. </xsl:template>
  112. <!-- ==================================================================== -->
  113. <xsl:template match="footnote/para[1]|footnote/simpara[1]" priority="2">
  114. <!-- this only works if the first thing in a footnote is a para, -->
  115. <!-- which is ok, because it usually is. -->
  116. <xsl:variable name="href">
  117. <xsl:text>#</xsl:text>
  118. <xsl:call-template name="object.id">
  119. <xsl:with-param name="object" select="ancestor::footnote"/>
  120. </xsl:call-template>
  121. </xsl:variable>
  122. <xsl:call-template name="paragraph">
  123. <xsl:with-param name="class">
  124. <xsl:if test="@role and $para.propagates.style != 0">
  125. <xsl:value-of select="@role"/>
  126. </xsl:if>
  127. </xsl:with-param>
  128. <xsl:with-param name="content">
  129. <a href="{$href}">
  130. <xsl:apply-templates select="." mode="class.attribute"/>
  131. <sup>
  132. <xsl:apply-templates select="." mode="class.attribute"/>
  133. <xsl:text>[</xsl:text>
  134. <xsl:apply-templates select="ancestor::footnote" mode="footnote.number"/>
  135. <xsl:text>] </xsl:text>
  136. </sup>
  137. </a>
  138. <xsl:apply-templates/>
  139. </xsl:with-param>
  140. </xsl:call-template>
  141. </xsl:template>
  142. <!-- ==================================================================== -->
  143. <xsl:template match="*" mode="footnote.body.number">
  144. <xsl:variable name="name">
  145. <xsl:text>ftn.</xsl:text>
  146. <xsl:call-template name="object.id">
  147. <xsl:with-param name="object" select="ancestor::footnote"/>
  148. </xsl:call-template>
  149. </xsl:variable>
  150. <xsl:variable name="href">
  151. <xsl:text>#</xsl:text>
  152. <xsl:call-template name="object.id">
  153. <xsl:with-param name="object" select="ancestor::footnote"/>
  154. </xsl:call-template>
  155. </xsl:variable>
  156. <xsl:variable name="footnote.mark">
  157. <a href="{$href}">
  158. <xsl:apply-templates select="." mode="class.attribute"/>
  159. <xsl:choose>
  160. <xsl:when test="$generate.id.attributes = 0">
  161. <xsl:if test="@id or @xml:id">
  162. <xsl:attribute name="name">
  163. <xsl:value-of select="@id|@xml:id"/>
  164. </xsl:attribute>
  165. </xsl:if>
  166. </xsl:when>
  167. <xsl:otherwise>
  168. <xsl:call-template name="id.attribute"/>
  169. </xsl:otherwise>
  170. </xsl:choose>
  171. <sup>
  172. <xsl:text>[</xsl:text>
  173. <xsl:apply-templates select="ancestor::footnote"
  174. mode="footnote.number"/>
  175. <xsl:text>] </xsl:text>
  176. </sup>
  177. </a>
  178. </xsl:variable>
  179. <xsl:variable name="html">
  180. <xsl:apply-templates select="."/>
  181. </xsl:variable>
  182. <xsl:choose>
  183. <xsl:when test="$exsl.node.set.available != 0">
  184. <xsl:variable name="html-nodes" select="exsl:node-set($html)"/>
  185. <xsl:choose>
  186. <xsl:when test="$html-nodes//p">
  187. <xsl:apply-templates select="$html-nodes" mode="insert.html.p">
  188. <xsl:with-param name="mark" select="$footnote.mark"/>
  189. </xsl:apply-templates>
  190. </xsl:when>
  191. <xsl:otherwise>
  192. <xsl:apply-templates select="$html-nodes" mode="insert.html.text">
  193. <xsl:with-param name="mark" select="$footnote.mark"/>
  194. </xsl:apply-templates>
  195. </xsl:otherwise>
  196. </xsl:choose>
  197. </xsl:when>
  198. <xsl:otherwise>
  199. <xsl:copy-of select="$html"/>
  200. </xsl:otherwise>
  201. </xsl:choose>
  202. </xsl:template>
  203. <!-- ==================================================================== -->
  204. <!--
  205. <xsl:template name="count-element-from">
  206. <xsl:param name="from" select=".."/>
  207. <xsl:param name="to" select="."/>
  208. <xsl:param name="count" select="0"/>
  209. <xsl:param name="list" select="$from/following::*[local-name(.)=local-name($to)]
  210. |$from/descendant-or-self::*[local-name(.)=local-name($to)]"/>
  211. <xsl:choose>
  212. <xsl:when test="not($list)">
  213. <xsl:text>-1</xsl:text>
  214. </xsl:when>
  215. <xsl:when test="$list[1] = $to">
  216. <xsl:value-of select="$count + 1"/>
  217. </xsl:when>
  218. <xsl:otherwise>
  219. </xsl:otherwise>
  220. </xsl:choose>
  221. </xsl:template>
  222. -->
  223. <!-- ==================================================================== -->
  224. <xsl:template name="process.footnotes">
  225. <xsl:variable name="footnotes" select=".//footnote"/>
  226. <xsl:variable name="table.footnotes"
  227. select=".//table//footnote | .//informaltable//footnote"/>
  228. <!-- Only bother to do this if there's at least one non-table footnote -->
  229. <xsl:if test="count($footnotes)>count($table.footnotes)">
  230. <div class="footnotes">
  231. <xsl:call-template name="footnotes.attributes"/>
  232. <br/>
  233. <hr>
  234. <xsl:choose>
  235. <xsl:when test="$make.clean.html != 0">
  236. <xsl:attribute name="class">footnote-hr</xsl:attribute>
  237. </xsl:when>
  238. <xsl:when test="$css.decoration != 0">
  239. <xsl:attribute name="style">
  240. <xsl:value-of select="concat('width:100; text-align:',
  241. $direction.align.start,
  242. ';',
  243. 'margin-', $direction.align.start, ': 0')"/>
  244. </xsl:attribute>
  245. </xsl:when>
  246. <xsl:otherwise>
  247. <xsl:attribute name="width">100</xsl:attribute>
  248. <xsl:attribute name="align"><xsl:value-of
  249. select="$direction.align.start"/></xsl:attribute>
  250. </xsl:otherwise>
  251. </xsl:choose>
  252. </hr>
  253. <xsl:apply-templates select="$footnotes" mode="process.footnote.mode"/>
  254. </div>
  255. </xsl:if>
  256. <xsl:if test="$annotation.support != 0 and //annotation">
  257. <div class="annotation-list">
  258. <div class="annotation-nocss">
  259. <p>The following annotations are from this essay. You are seeing
  260. them here because your browser doesn’t support the user-interface
  261. techniques used to make them appear as ‘popups’ on modern browsers.</p>
  262. </div>
  263. <xsl:apply-templates select="//annotation"
  264. mode="annotation-popup"/>
  265. </div>
  266. </xsl:if>
  267. </xsl:template>
  268. <xsl:template name="footnotes.attributes">
  269. <!-- customizable for footnotes attributes -->
  270. </xsl:template>
  271. <xsl:template name="process.chunk.footnotes">
  272. <!-- nop -->
  273. </xsl:template>
  274. <xsl:template match="footnote" name="process.footnote" mode="process.footnote.mode">
  275. <xsl:variable name="id">
  276. <xsl:text>ftn.</xsl:text>
  277. <xsl:call-template name="object.id">
  278. <xsl:with-param name="conditional" select="0"/>
  279. </xsl:call-template>
  280. </xsl:variable>
  281. <xsl:choose>
  282. <xsl:when test="local-name(*[1]) = 'para' or local-name(*[1]) = 'simpara'">
  283. <div id="{$id}">
  284. <xsl:call-template name="common.html.attributes"/>
  285. <xsl:apply-templates/>
  286. </div>
  287. </xsl:when>
  288. <xsl:when test="$html.cleanup != 0 and
  289. $exsl.node.set.available != 0">
  290. <div id="{$id}">
  291. <xsl:call-template name="common.html.attributes"/>
  292. <xsl:call-template name="id.attribute"/>
  293. <xsl:apply-templates select="*[1]" mode="footnote.body.number"/>
  294. <xsl:apply-templates select="*[position() &gt; 1]"/>
  295. </div>
  296. </xsl:when>
  297. <xsl:otherwise>
  298. <xsl:message>
  299. <xsl:text>Warning: footnote number may not be generated </xsl:text>
  300. <xsl:text>correctly; </xsl:text>
  301. <xsl:value-of select="local-name(*[1])"/>
  302. <xsl:text> unexpected as first child of footnote.</xsl:text>
  303. </xsl:message>
  304. <div id="{$id}">
  305. <xsl:call-template name="common.html.attributes"/>
  306. <xsl:call-template name="id.attribute"/>
  307. <xsl:apply-templates/>
  308. </div>
  309. </xsl:otherwise>
  310. </xsl:choose>
  311. </xsl:template>
  312. <xsl:template match="table//footnote | informaltable//footnote"
  313. mode="process.footnote.mode">
  314. </xsl:template>
  315. <xsl:template match="footnote" mode="table.footnote.mode">
  316. <xsl:call-template name="process.footnote"/>
  317. </xsl:template>
  318. </xsl:stylesheet>