toc.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. version='1.0'>
  4. <!-- ********************************************************************
  5. $Id: toc.xsl 9297 2012-04-22 03:56:16Z bobstayton $
  6. ********************************************************************
  7. This file is part of the XSL DocBook Stylesheet distribution.
  8. See ../README or http://docbook.sf.net/release/xsl/current/ for
  9. copyright and other information.
  10. ******************************************************************** -->
  11. <!-- ==================================================================== -->
  12. <xsl:template match="set/toc | book/toc | part/toc">
  13. <xsl:variable name="toc.params">
  14. <xsl:call-template name="find.path.params">
  15. <xsl:with-param name="node" select="parent::*"/>
  16. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  17. </xsl:call-template>
  18. </xsl:variable>
  19. <!-- Do not output the toc element if one is already generated
  20. by the use of $generate.toc parameter, or if
  21. generating a source toc is turned off -->
  22. <xsl:if test="not(contains($toc.params, 'toc')) and
  23. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  24. <xsl:variable name="content">
  25. <xsl:choose>
  26. <xsl:when test="* and $process.source.toc != 0">
  27. <xsl:apply-templates />
  28. </xsl:when>
  29. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  30. <!-- trick to switch context node to parent element -->
  31. <xsl:for-each select="parent::*">
  32. <xsl:choose>
  33. <xsl:when test="self::set">
  34. <xsl:call-template name="set.toc">
  35. <xsl:with-param name="toc.title.p"
  36. select="contains($toc.params, 'title')"/>
  37. </xsl:call-template>
  38. </xsl:when>
  39. <xsl:when test="self::book">
  40. <xsl:call-template name="division.toc">
  41. <xsl:with-param name="toc.title.p"
  42. select="contains($toc.params, 'title')"/>
  43. </xsl:call-template>
  44. </xsl:when>
  45. <xsl:when test="self::part">
  46. <xsl:call-template name="division.toc">
  47. <xsl:with-param name="toc.title.p"
  48. select="contains($toc.params, 'title')"/>
  49. </xsl:call-template>
  50. </xsl:when>
  51. </xsl:choose>
  52. </xsl:for-each>
  53. </xsl:when>
  54. </xsl:choose>
  55. </xsl:variable>
  56. <xsl:if test="string-length(normalize-space($content)) != 0">
  57. <xsl:copy-of select="$content"/>
  58. </xsl:if>
  59. </xsl:if>
  60. </xsl:template>
  61. <xsl:template match="chapter/toc | appendix/toc | preface/toc | article/toc">
  62. <xsl:variable name="toc.params">
  63. <xsl:call-template name="find.path.params">
  64. <xsl:with-param name="node" select="parent::*"/>
  65. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  66. </xsl:call-template>
  67. </xsl:variable>
  68. <!-- Do not output the toc element if one is already generated
  69. by the use of $generate.toc parameter, or if
  70. generating a source toc is turned off -->
  71. <xsl:if test="not(contains($toc.params, 'toc')) and
  72. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  73. <xsl:choose>
  74. <xsl:when test="* and $process.source.toc != 0">
  75. <div>
  76. <xsl:apply-templates select="." mode="common.html.attributes"/>
  77. <xsl:call-template name="id.attribute"/>
  78. <xsl:apply-templates select="title"/>
  79. <dl>
  80. <xsl:apply-templates select="." mode="common.html.attributes"/>
  81. <xsl:apply-templates select="*[not(self::title)]"/>
  82. </dl>
  83. </div>
  84. <xsl:call-template name="component.toc.separator"/>
  85. </xsl:when>
  86. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  87. <!-- trick to switch context node to section element -->
  88. <xsl:for-each select="parent::*">
  89. <xsl:call-template name="component.toc">
  90. <xsl:with-param name="toc.title.p"
  91. select="contains($toc.params, 'title')"/>
  92. </xsl:call-template>
  93. </xsl:for-each>
  94. <xsl:call-template name="component.toc.separator"/>
  95. </xsl:when>
  96. </xsl:choose>
  97. </xsl:if>
  98. </xsl:template>
  99. <xsl:template match="section/toc
  100. |sect1/toc
  101. |sect2/toc
  102. |sect3/toc
  103. |sect4/toc
  104. |sect5/toc">
  105. <xsl:variable name="toc.params">
  106. <xsl:call-template name="find.path.params">
  107. <xsl:with-param name="node" select="parent::*"/>
  108. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  109. </xsl:call-template>
  110. </xsl:variable>
  111. <!-- Do not output the toc element if one is already generated
  112. by the use of $generate.toc parameter, or if
  113. generating a source toc is turned off -->
  114. <xsl:if test="not(contains($toc.params, 'toc')) and
  115. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  116. <xsl:choose>
  117. <xsl:when test="* and $process.source.toc != 0">
  118. <div>
  119. <xsl:apply-templates select="." mode="common.html.attributes"/>
  120. <xsl:call-template name="id.attribute"/>
  121. <xsl:apply-templates select="title"/>
  122. <dl>
  123. <xsl:apply-templates select="." mode="common.html.attributes"/>
  124. <xsl:apply-templates select="*[not(self::title)]"/>
  125. </dl>
  126. </div>
  127. <xsl:call-template name="section.toc.separator"/>
  128. </xsl:when>
  129. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  130. <!-- trick to switch context node to section element -->
  131. <xsl:for-each select="parent::*">
  132. <xsl:call-template name="section.toc">
  133. <xsl:with-param name="toc.title.p"
  134. select="contains($toc.params, 'title')"/>
  135. </xsl:call-template>
  136. </xsl:for-each>
  137. <xsl:call-template name="section.toc.separator"/>
  138. </xsl:when>
  139. </xsl:choose>
  140. </xsl:if>
  141. </xsl:template>
  142. <!-- ==================================================================== -->
  143. <xsl:template match="tocpart|tocchap
  144. |toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
  145. <xsl:variable name="sub-toc">
  146. <xsl:if test="tocchap|toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
  147. <xsl:choose>
  148. <xsl:when test="$toc.list.type = 'dl'">
  149. <dd>
  150. <xsl:apply-templates select="." mode="common.html.attributes"/>
  151. <xsl:element name="{$toc.list.type}">
  152. <xsl:apply-templates select="." mode="common.html.attributes"/>
  153. <xsl:apply-templates select="tocchap|toclevel1|toclevel2|
  154. toclevel3|toclevel4|toclevel5"/>
  155. </xsl:element>
  156. </dd>
  157. </xsl:when>
  158. <xsl:otherwise>
  159. <xsl:element name="{$toc.list.type}">
  160. <xsl:apply-templates select="." mode="common.html.attributes"/>
  161. <xsl:apply-templates select="tocchap|toclevel1|toclevel2|
  162. toclevel3|toclevel4|toclevel5"/>
  163. </xsl:element>
  164. </xsl:otherwise>
  165. </xsl:choose>
  166. </xsl:if>
  167. </xsl:variable>
  168. <xsl:apply-templates select="tocentry[position() != last()]"/>
  169. <xsl:choose>
  170. <xsl:when test="$toc.list.type = 'dl'">
  171. <dt>
  172. <xsl:apply-templates select="." mode="common.html.attributes"/>
  173. <xsl:apply-templates select="tocentry[position() = last()]"/>
  174. </dt>
  175. <xsl:copy-of select="$sub-toc"/>
  176. </xsl:when>
  177. <xsl:otherwise>
  178. <li>
  179. <xsl:apply-templates select="." mode="common.html.attributes"/>
  180. <xsl:apply-templates select="tocentry[position() = last()]"/>
  181. <xsl:copy-of select="$sub-toc"/>
  182. </li>
  183. </xsl:otherwise>
  184. </xsl:choose>
  185. </xsl:template>
  186. <xsl:template match="tocentry|tocdiv|lotentry|tocfront|tocback">
  187. <xsl:choose>
  188. <xsl:when test="$toc.list.type = 'dl'">
  189. <dt>
  190. <xsl:apply-templates select="." mode="common.html.attributes"/>
  191. <xsl:call-template name="tocentry-content"/>
  192. </dt>
  193. </xsl:when>
  194. <xsl:otherwise>
  195. <li>
  196. <xsl:apply-templates select="." mode="common.html.attributes"/>
  197. <xsl:call-template name="tocentry-content"/>
  198. </li>
  199. </xsl:otherwise>
  200. </xsl:choose>
  201. </xsl:template>
  202. <xsl:template match="tocentry[position() = last()]" priority="2">
  203. <xsl:call-template name="tocentry-content"/>
  204. </xsl:template>
  205. <xsl:template name="tocentry-content">
  206. <xsl:variable name="targets" select="key('id',@linkend)"/>
  207. <xsl:variable name="target" select="$targets[1]"/>
  208. <xsl:choose>
  209. <xsl:when test="@linkend">
  210. <xsl:call-template name="check.id.unique">
  211. <xsl:with-param name="linkend" select="@linkend"/>
  212. </xsl:call-template>
  213. <a>
  214. <xsl:attribute name="href">
  215. <xsl:call-template name="href.target">
  216. <xsl:with-param name="object" select="$target"/>
  217. </xsl:call-template>
  218. </xsl:attribute>
  219. <xsl:apply-templates/>
  220. </a>
  221. </xsl:when>
  222. <xsl:otherwise>
  223. <xsl:apply-templates/>
  224. </xsl:otherwise>
  225. </xsl:choose>
  226. </xsl:template>
  227. <xsl:template match="toc/title">
  228. <div>
  229. <xsl:apply-templates select="." mode="common.html.attributes"/>
  230. <xsl:apply-templates/>
  231. </div>
  232. </xsl:template>
  233. <xsl:template match="toc/subtitle">
  234. <div>
  235. <xsl:apply-templates select="." mode="common.html.attributes"/>
  236. <xsl:apply-templates/>
  237. </div>
  238. </xsl:template>
  239. <xsl:template match="toc/titleabbrev">
  240. </xsl:template>
  241. <!-- ==================================================================== -->
  242. <!-- A lot element must have content, because there is no attribute
  243. to select what kind of list should be generated -->
  244. <xsl:template match="book/lot | part/lot">
  245. <!-- Don't generate a page sequence unless there is content -->
  246. <xsl:variable name="content">
  247. <xsl:choose>
  248. <xsl:when test="* and $process.source.toc != 0">
  249. <div>
  250. <xsl:apply-templates select="." mode="common.html.attributes"/>
  251. <xsl:apply-templates />
  252. </div>
  253. </xsl:when>
  254. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  255. <xsl:call-template name="process.empty.lot"/>
  256. </xsl:when>
  257. </xsl:choose>
  258. </xsl:variable>
  259. <xsl:if test="string-length(normalize-space($content)) != 0">
  260. <xsl:copy-of select="$content"/>
  261. </xsl:if>
  262. </xsl:template>
  263. <xsl:template match="chapter/lot | appendix/lot | preface/lot | article/lot">
  264. <xsl:choose>
  265. <xsl:when test="* and $process.source.toc != 0">
  266. <div>
  267. <xsl:apply-templates select="." mode="common.html.attributes"/>
  268. <xsl:apply-templates />
  269. </div>
  270. <xsl:call-template name="component.toc.separator"/>
  271. </xsl:when>
  272. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  273. <xsl:call-template name="process.empty.lot"/>
  274. </xsl:when>
  275. </xsl:choose>
  276. </xsl:template>
  277. <xsl:template match="section/lot
  278. |sect1/lot
  279. |sect2/lot
  280. |sect3/lot
  281. |sect4/lot
  282. |sect5/lot">
  283. <xsl:choose>
  284. <xsl:when test="* and $process.source.toc != 0">
  285. <div>
  286. <xsl:apply-templates select="." mode="common.html.attributes"/>
  287. <xsl:apply-templates/>
  288. </div>
  289. <xsl:call-template name="section.toc.separator"/>
  290. </xsl:when>
  291. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  292. <xsl:call-template name="process.empty.lot"/>
  293. </xsl:when>
  294. </xsl:choose>
  295. </xsl:template>
  296. <xsl:template name="process.empty.lot">
  297. <!-- An empty lot element does not provide any information to indicate
  298. what should be included in it. You can customize this
  299. template to generate a lot based on @role or something -->
  300. <xsl:message>
  301. <xsl:text>Warning: don't know what to generate for </xsl:text>
  302. <xsl:text>lot that has no children.</xsl:text>
  303. </xsl:message>
  304. </xsl:template>
  305. <xsl:template match="lot/title">
  306. <div>
  307. <xsl:apply-templates select="." mode="common.html.attributes"/>
  308. <xsl:apply-templates/>
  309. </div>
  310. </xsl:template>
  311. <xsl:template match="lot/subtitle">
  312. <div>
  313. <xsl:apply-templates select="." mode="common.html.attributes"/>
  314. <xsl:apply-templates/>
  315. </div>
  316. </xsl:template>
  317. <xsl:template match="lot/titleabbrev">
  318. </xsl:template>
  319. </xsl:stylesheet>