htmltbl.xsl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  4. version="1.0">
  5. <!-- ********************************************************************
  6. $Id: htmltbl.xsl 9647 2012-10-26 17:42:03Z bobstayton $
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://docbook.sf.net/release/xsl/current/ for
  10. copyright and other information.
  11. ******************************************************************** -->
  12. <!-- ==================================================================== -->
  13. <!-- Outputs an fo:table only, not the caption -->
  14. <xsl:template match="table|informaltable" mode="htmlTable">
  15. <xsl:variable name="numcols">
  16. <xsl:call-template name="widest-html-row">
  17. <xsl:with-param name="rows" select=".//tr"/>
  18. </xsl:call-template>
  19. </xsl:variable>
  20. <xsl:variable name="prop-columns"
  21. select=".//col[contains(@width, '%')] |
  22. .//colgroup[contains(@width, '%')]"/>
  23. <xsl:variable name="table.width">
  24. <xsl:call-template name="table.width"/>
  25. </xsl:variable>
  26. <fo:table xsl:use-attribute-sets="table.table.properties">
  27. <xsl:choose>
  28. <xsl:when test="$fop.extensions != 0 or
  29. $fop1.extensions != 0">
  30. <xsl:attribute name="table-layout">fixed</xsl:attribute>
  31. </xsl:when>
  32. </xsl:choose>
  33. <xsl:attribute name="width">
  34. <xsl:choose>
  35. <xsl:when test="@width">
  36. <xsl:value-of select="@width"/>
  37. </xsl:when>
  38. <xsl:when test="$table.width">
  39. <xsl:value-of select="$table.width"/>
  40. </xsl:when>
  41. <xsl:otherwise>100%</xsl:otherwise>
  42. </xsl:choose>
  43. </xsl:attribute>
  44. <xsl:call-template name="table.frame">
  45. <xsl:with-param name="frame">
  46. <xsl:choose>
  47. <xsl:when test="@frame = 'box'">all</xsl:when>
  48. <xsl:when test="@frame = 'border'">all</xsl:when>
  49. <xsl:when test="@frame = 'below'">bottom</xsl:when>
  50. <xsl:when test="@frame = 'above'">top</xsl:when>
  51. <xsl:when test="@frame = 'hsides'">topbot</xsl:when>
  52. <xsl:when test="@frame = 'vsides'">sides</xsl:when>
  53. <xsl:when test="@frame = 'lhs'">lhs</xsl:when>
  54. <xsl:when test="@frame = 'rhs'">rhs</xsl:when>
  55. <xsl:when test="@frame = 'void'">none</xsl:when>
  56. <xsl:when test="@border != '' and @border != 0">all</xsl:when>
  57. <xsl:when test="@border != '' and @border = 0">none</xsl:when>
  58. <xsl:when test="@frame != ''">
  59. <xsl:value-of select="@frame"/>
  60. </xsl:when>
  61. <xsl:when test="$default.table.frame != ''">
  62. <xsl:value-of select="$default.table.frame"/>
  63. </xsl:when>
  64. <xsl:otherwise>all</xsl:otherwise>
  65. </xsl:choose>
  66. </xsl:with-param>
  67. </xsl:call-template>
  68. <xsl:call-template name="make-html-table-columns">
  69. <xsl:with-param name="count" select="$numcols"/>
  70. </xsl:call-template>
  71. <xsl:apply-templates select="thead" mode="htmlTable"/>
  72. <xsl:apply-templates select="tfoot" mode="htmlTable"/>
  73. <xsl:choose>
  74. <xsl:when test="tbody">
  75. <xsl:apply-templates select="tbody" mode="htmlTable"/>
  76. </xsl:when>
  77. <xsl:otherwise>
  78. <fo:table-body start-indent="0pt" end-indent="0pt">
  79. <xsl:apply-templates select="tr" mode="htmlTable"/>
  80. </fo:table-body>
  81. </xsl:otherwise>
  82. </xsl:choose>
  83. </fo:table>
  84. </xsl:template>
  85. <!-- This template writes rowsep or colsep equivalant for html tables -->
  86. <xsl:template name="html.table.cell.rules">
  87. <xsl:variable name="border"
  88. select="(ancestor::table |
  89. ancestor::informaltable)[last()]/@border"/>
  90. <xsl:variable name="table.rules"
  91. select="(ancestor::table |
  92. ancestor::informaltable)[last()]/@rules"/>
  93. <xsl:variable name="rules">
  94. <xsl:choose>
  95. <xsl:when test="$table.rules != ''">
  96. <xsl:value-of select="$table.rules"/>
  97. </xsl:when>
  98. <xsl:when test="$default.table.rules != ''">
  99. <xsl:value-of select="$default.table.rules"/>
  100. </xsl:when>
  101. </xsl:choose>
  102. </xsl:variable>
  103. <xsl:choose>
  104. <xsl:when test="$border != '' and $border != 0">
  105. <xsl:attribute name="border">
  106. <xsl:value-of select="$table.cell.border.thickness"/>
  107. <xsl:text> </xsl:text>
  108. <xsl:value-of select="$table.cell.border.style"/>
  109. <xsl:text> </xsl:text>
  110. <xsl:value-of select="$table.cell.border.color"/>
  111. </xsl:attribute>
  112. </xsl:when>
  113. <xsl:when test="$rules = 'none'">
  114. <xsl:attribute name="border-start-style">none</xsl:attribute>
  115. <xsl:attribute name="border-end-style">none</xsl:attribute>
  116. <xsl:attribute name="border-top-style">none</xsl:attribute>
  117. <xsl:attribute name="border-bottom-style">none</xsl:attribute>
  118. </xsl:when>
  119. <xsl:when test="$rules = 'cols' and following-sibling::*">
  120. <!-- If not the last column, add border after -->
  121. <xsl:attribute name="border-start-style">none</xsl:attribute>
  122. <xsl:attribute name="border-top-style">none</xsl:attribute>
  123. <xsl:attribute name="border-bottom-style">none</xsl:attribute>
  124. <xsl:attribute name="border-end-style">
  125. <xsl:value-of select="$table.frame.border.style"/>
  126. </xsl:attribute>
  127. <xsl:attribute name="border-end-width">
  128. <xsl:value-of select="$table.frame.border.thickness"/>
  129. </xsl:attribute>
  130. <xsl:attribute name="border-end-color">
  131. <xsl:value-of select="$table.frame.border.color"/>
  132. </xsl:attribute>
  133. </xsl:when>
  134. <!-- If not the last row, add border below -->
  135. <xsl:when test="$rules = 'rows'">
  136. <xsl:variable name="rowborder">
  137. <xsl:choose>
  138. <!-- If in thead and tbody has rows, add border -->
  139. <xsl:when test="parent::tr/parent::thead/
  140. following-sibling::tbody/tr">1</xsl:when>
  141. <!-- If in tbody and tfoot has rows, add border -->
  142. <xsl:when test="parent::tr/parent::tbody/
  143. following-sibling::tfoot/tr">1</xsl:when>
  144. <xsl:when test="parent::tr/parent::tbody/
  145. preceding-sibling::tfoot/tr">1</xsl:when>
  146. <!-- If following rows, but not rowspan reaches last row -->
  147. <xsl:when test="parent::tr/following-sibling::tr and
  148. not(@rowspan = count(parent::tr/following-sibling::tr) + 1)">1</xsl:when>
  149. <xsl:otherwise>0</xsl:otherwise>
  150. </xsl:choose>
  151. </xsl:variable>
  152. <xsl:if test="$rowborder = 1">
  153. <xsl:attribute name="border-start-style">none</xsl:attribute>
  154. <xsl:attribute name="border-end-style">none</xsl:attribute>
  155. <xsl:attribute name="border-top-style">none</xsl:attribute>
  156. <xsl:attribute name="border-bottom-style">
  157. <xsl:value-of select="$table.frame.border.style"/>
  158. </xsl:attribute>
  159. <xsl:attribute name="border-bottom-width">
  160. <xsl:value-of select="$table.frame.border.thickness"/>
  161. </xsl:attribute>
  162. <xsl:attribute name="border-bottom-color">
  163. <xsl:value-of select="$table.frame.border.color"/>
  164. </xsl:attribute>
  165. </xsl:if>
  166. </xsl:when>
  167. <xsl:when test="$rules = 'all'">
  168. <xsl:attribute name="border-start-style">none</xsl:attribute>
  169. <xsl:attribute name="border-top-style">none</xsl:attribute>
  170. <xsl:variable name="rowborder">
  171. <xsl:choose>
  172. <!-- If in thead and tbody has rows, add border -->
  173. <xsl:when test="parent::tr/parent::thead/
  174. following-sibling::tbody/tr">1</xsl:when>
  175. <!-- If in tbody and tfoot has rows, add border -->
  176. <xsl:when test="parent::tr/parent::tbody/
  177. following-sibling::tfoot/tr">1</xsl:when>
  178. <xsl:when test="parent::tr/parent::tbody/
  179. preceding-sibling::tfoot/tr">1</xsl:when>
  180. <!-- If following rows, but not rowspan reaches last row -->
  181. <xsl:when test="parent::tr/following-sibling::tr and
  182. not(@rowspan = count(parent::tr/following-sibling::tr) + 1)">1</xsl:when>
  183. <xsl:otherwise>0</xsl:otherwise>
  184. </xsl:choose>
  185. </xsl:variable>
  186. <xsl:if test="$rowborder = 1">
  187. <xsl:attribute name="border-bottom-style">
  188. <xsl:value-of select="$table.frame.border.style"/>
  189. </xsl:attribute>
  190. <xsl:attribute name="border-bottom-width">
  191. <xsl:value-of select="$table.frame.border.thickness"/>
  192. </xsl:attribute>
  193. <xsl:attribute name="border-bottom-color">
  194. <xsl:value-of select="$table.frame.border.color"/>
  195. </xsl:attribute>
  196. </xsl:if>
  197. <xsl:if test="following-sibling::*">
  198. <!-- If not the last column, add border after -->
  199. <xsl:attribute name="border-end-style">
  200. <xsl:value-of select="$table.frame.border.style"/>
  201. </xsl:attribute>
  202. <xsl:attribute name="border-end-width">
  203. <xsl:value-of select="$table.frame.border.thickness"/>
  204. </xsl:attribute>
  205. <xsl:attribute name="border-end-color">
  206. <xsl:value-of select="$table.frame.border.color"/>
  207. </xsl:attribute>
  208. </xsl:if>
  209. </xsl:when>
  210. <xsl:when test="$rules = 'groups' and ancestor::thead
  211. and not(parent::tr/following-sibling::tr)">
  212. <xsl:attribute name="border-start-style">none</xsl:attribute>
  213. <xsl:attribute name="border-end-style">none</xsl:attribute>
  214. <xsl:attribute name="border-top-style">none</xsl:attribute>
  215. <xsl:attribute name="border-bottom-style">
  216. <xsl:value-of select="$table.frame.border.style"/>
  217. </xsl:attribute>
  218. <xsl:attribute name="border-bottom-width">
  219. <xsl:value-of select="$table.frame.border.thickness"/>
  220. </xsl:attribute>
  221. <xsl:attribute name="border-bottom-color">
  222. <xsl:value-of select="$table.frame.border.color"/>
  223. </xsl:attribute>
  224. </xsl:when>
  225. <xsl:when test="$rules = 'groups' and ancestor::tfoot
  226. and not(parent::tr/preceding-sibling::tr)">
  227. <xsl:attribute name="border-start-style">none</xsl:attribute>
  228. <xsl:attribute name="border-end-style">none</xsl:attribute>
  229. <xsl:attribute name="border-top-style">none</xsl:attribute>
  230. <xsl:attribute name="border-top-style">
  231. <xsl:value-of select="$table.frame.border.style"/>
  232. </xsl:attribute>
  233. <xsl:attribute name="border-top-width">
  234. <xsl:value-of select="$table.frame.border.thickness"/>
  235. </xsl:attribute>
  236. <xsl:attribute name="border-top-color">
  237. <xsl:value-of select="$table.frame.border.color"/>
  238. </xsl:attribute>
  239. </xsl:when>
  240. </xsl:choose>
  241. </xsl:template>
  242. <xsl:template match="caption" mode="htmlTable">
  243. <!-- Handled by formal.object.heading -->
  244. </xsl:template>
  245. <xsl:template name="widest-html-row">
  246. <xsl:param name="rows" select="''"/>
  247. <xsl:param name="count" select="0"/>
  248. <xsl:choose>
  249. <xsl:when test="count($rows) = 0">
  250. <xsl:value-of select="$count"/>
  251. </xsl:when>
  252. <xsl:otherwise>
  253. <xsl:choose>
  254. <xsl:when test="$count &gt; count($rows[1]/*)">
  255. <xsl:call-template name="widest-html-row">
  256. <xsl:with-param name="rows" select="$rows[position() &gt; 1]"/>
  257. <xsl:with-param name="count" select="$count"/>
  258. </xsl:call-template>
  259. </xsl:when>
  260. <xsl:otherwise>
  261. <xsl:call-template name="widest-html-row">
  262. <xsl:with-param name="rows" select="$rows[position() &gt; 1]"/>
  263. <xsl:with-param name="count" select="count($rows[1]/*)"/>
  264. </xsl:call-template>
  265. </xsl:otherwise>
  266. </xsl:choose>
  267. </xsl:otherwise>
  268. </xsl:choose>
  269. </xsl:template>
  270. <xsl:template name="make-html-table-columns">
  271. <xsl:param name="count" select="0"/>
  272. <xsl:param name="number" select="1"/>
  273. <xsl:choose>
  274. <xsl:when test="col|colgroup/col">
  275. <xsl:for-each select="col|colgroup/col">
  276. <fo:table-column>
  277. <xsl:attribute name="column-number">
  278. <xsl:number from="table|informaltable" level="any" format="1"/>
  279. </xsl:attribute>
  280. <xsl:if test="@width">
  281. <xsl:attribute name="column-width">
  282. <xsl:choose>
  283. <xsl:when test="$fop.extensions != 0 and
  284. contains(@width, '%')">
  285. <xsl:value-of select="concat('proportional-column-width(',
  286. substring-before(@width, '%'),
  287. ')')"/>
  288. </xsl:when>
  289. <xsl:otherwise>
  290. <xsl:value-of select="@width"/>
  291. </xsl:otherwise>
  292. </xsl:choose>
  293. </xsl:attribute>
  294. </xsl:if>
  295. </fo:table-column>
  296. </xsl:for-each>
  297. </xsl:when>
  298. <xsl:when test="$fop.extensions != 0">
  299. <xsl:if test="$number &lt;= $count">
  300. <fo:table-column column-number="{$number}"
  301. column-width="{6.5 div $count}in"/>
  302. <xsl:call-template name="make-html-table-columns">
  303. <xsl:with-param name="count" select="$count"/>
  304. <xsl:with-param name="number" select="$number + 1"/>
  305. </xsl:call-template>
  306. </xsl:if>
  307. </xsl:when>
  308. </xsl:choose>
  309. </xsl:template>
  310. <xsl:template match="tbody" mode="htmlTable">
  311. <fo:table-body start-indent="0pt"
  312. end-indent="0pt">
  313. <xsl:apply-templates mode="htmlTable"/>
  314. </fo:table-body>
  315. </xsl:template>
  316. <xsl:template match="tfoot" mode="htmlTable">
  317. <fo:table-footer start-indent="0pt"
  318. end-indent="0pt">
  319. <xsl:apply-templates mode="htmlTable"/>
  320. </fo:table-footer>
  321. </xsl:template>
  322. <xsl:template match="th|td" mode="htmlTable">
  323. <xsl:variable name="bgcolor.pi">
  324. <xsl:call-template name="pi.dbfo_bgcolor"/>
  325. </xsl:variable>
  326. <xsl:variable name="bgcolor">
  327. <xsl:choose>
  328. <xsl:when test="$bgcolor.pi != ''">
  329. <xsl:value-of select="$bgcolor.pi"/>
  330. </xsl:when>
  331. <xsl:when test="string-length(@bgcolor) != 0">
  332. <xsl:value-of select="@bgcolor"/>
  333. </xsl:when>
  334. </xsl:choose>
  335. </xsl:variable>
  336. <xsl:variable name="align">
  337. <xsl:call-template name="inherited.table.attribute">
  338. <xsl:with-param name="entry" select="."/>
  339. <xsl:with-param name="row" select="parent::tr"/>
  340. <xsl:with-param name="attribute" select="'align'"/>
  341. </xsl:call-template>
  342. </xsl:variable>
  343. <xsl:variable name="valign">
  344. <xsl:call-template name="inherited.table.attribute">
  345. <xsl:with-param name="entry" select="."/>
  346. <xsl:with-param name="row" select="parent::tr"/>
  347. <xsl:with-param name="attribute" select="'valign'"/>
  348. </xsl:call-template>
  349. </xsl:variable>
  350. <fo:table-cell xsl:use-attribute-sets="table.cell.padding">
  351. <xsl:call-template name="table.cell.properties">
  352. <xsl:with-param name="bgcolor.pi" select="$bgcolor"/>
  353. <xsl:with-param name="rowsep.inherit" select="0"/>
  354. <xsl:with-param name="align.inherit" select="$align"/>
  355. <xsl:with-param name="valign.inherit" select="$valign"/>
  356. <xsl:with-param name="colsep.inherit" select="0"/>
  357. </xsl:call-template>
  358. <xsl:if test="@colspan &gt; 1">
  359. <xsl:attribute name="number-columns-spanned">
  360. <xsl:value-of select="@colspan"/>
  361. </xsl:attribute>
  362. </xsl:if>
  363. <xsl:if test="@rowspan &gt; 1">
  364. <xsl:attribute name="number-rows-spanned">
  365. <xsl:value-of select="@rowspan"/>
  366. </xsl:attribute>
  367. </xsl:if>
  368. <fo:block>
  369. <xsl:call-template name="table.cell.block.properties"/>
  370. <xsl:apply-templates/>
  371. </fo:block>
  372. </fo:table-cell>
  373. </xsl:template>
  374. <xsl:template match="thead" mode="htmlTable">
  375. <fo:table-header start-indent="0pt"
  376. end-indent="0pt">
  377. <xsl:apply-templates mode="htmlTable"/>
  378. </fo:table-header>
  379. </xsl:template>
  380. <xsl:template match="tr" mode="htmlTable">
  381. <fo:table-row>
  382. <xsl:call-template name="table.row.properties"/>
  383. <xsl:apply-templates mode="htmlTable"/>
  384. </fo:table-row>
  385. </xsl:template>
  386. </xsl:stylesheet>