table.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. exclude-result-prefixes="doc"
  5. version="1.0">
  6. <!-- ********************************************************************
  7. $Id: table.xsl 8392 2009-04-01 08:47:55Z bobstayton $
  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. <!-- ==================================================================== -->
  14. <xsl:template name="blank.spans">
  15. <xsl:param name="cols" select="1"/>
  16. <xsl:if test="$cols &gt; 0">
  17. <xsl:text>0:</xsl:text>
  18. <xsl:call-template name="blank.spans">
  19. <xsl:with-param name="cols" select="$cols - 1"/>
  20. </xsl:call-template>
  21. </xsl:if>
  22. </xsl:template>
  23. <xsl:template name="calculate.following.spans">
  24. <xsl:param name="colspan" select="1"/>
  25. <xsl:param name="spans" select="''"/>
  26. <xsl:choose>
  27. <xsl:when test="$colspan &gt; 0">
  28. <xsl:call-template name="calculate.following.spans">
  29. <xsl:with-param name="colspan" select="$colspan - 1"/>
  30. <xsl:with-param name="spans" select="substring-after($spans,':')"/>
  31. </xsl:call-template>
  32. </xsl:when>
  33. <xsl:otherwise>
  34. <xsl:value-of select="$spans"/>
  35. </xsl:otherwise>
  36. </xsl:choose>
  37. </xsl:template>
  38. <xsl:template name="finaltd">
  39. <xsl:param name="spans"/>
  40. <xsl:param name="col" select="0"/>
  41. <xsl:if test="$spans != ''">
  42. <xsl:choose>
  43. <xsl:when test="starts-with($spans,'0:')">
  44. <xsl:call-template name="empty.table.cell">
  45. <xsl:with-param name="colnum" select="$col"/>
  46. </xsl:call-template>
  47. </xsl:when>
  48. <xsl:otherwise></xsl:otherwise>
  49. </xsl:choose>
  50. <xsl:call-template name="finaltd">
  51. <xsl:with-param name="spans" select="substring-after($spans,':')"/>
  52. <xsl:with-param name="col" select="$col+1"/>
  53. </xsl:call-template>
  54. </xsl:if>
  55. </xsl:template>
  56. <xsl:template name="sfinaltd">
  57. <xsl:param name="spans"/>
  58. <xsl:if test="$spans != ''">
  59. <xsl:choose>
  60. <xsl:when test="starts-with($spans,'0:')">0:</xsl:when>
  61. <xsl:otherwise>
  62. <xsl:value-of select="substring-before($spans,':')-1"/>
  63. <xsl:text>:</xsl:text>
  64. </xsl:otherwise>
  65. </xsl:choose>
  66. <xsl:call-template name="sfinaltd">
  67. <xsl:with-param name="spans" select="substring-after($spans,':')"/>
  68. </xsl:call-template>
  69. </xsl:if>
  70. </xsl:template>
  71. <xsl:template name="entry.colnum">
  72. <xsl:param name="entry" select="."/>
  73. <xsl:choose>
  74. <xsl:when test="$entry/@spanname">
  75. <xsl:variable name="spanname" select="$entry/@spanname"/>
  76. <xsl:variable name="spanspec"
  77. select="($entry/ancestor::tgroup/spanspec[@spanname=$spanname]
  78. |$entry/ancestor::entrytbl/spanspec[@spanname=$spanname])[last()]"/>
  79. <xsl:variable name="colspec"
  80. select="($entry/ancestor::tgroup/colspec[@colname=$spanspec/@namest]
  81. |$entry/ancestor::entrytbl/colspec[@colname=$spanspec/@namest])[last()]"/>
  82. <xsl:call-template name="colspec.colnum">
  83. <xsl:with-param name="colspec" select="$colspec"/>
  84. </xsl:call-template>
  85. </xsl:when>
  86. <xsl:when test="$entry/@colname">
  87. <xsl:variable name="colname" select="$entry/@colname"/>
  88. <xsl:variable name="colspec"
  89. select="($entry/ancestor::tgroup/colspec[@colname=$colname]
  90. |$entry/ancestor::entrytbl/colspec[@colname=$colname])[last()]"/>
  91. <xsl:call-template name="colspec.colnum">
  92. <xsl:with-param name="colspec" select="$colspec"/>
  93. </xsl:call-template>
  94. </xsl:when>
  95. <xsl:when test="$entry/@namest">
  96. <xsl:variable name="namest" select="$entry/@namest"/>
  97. <xsl:variable name="colspec"
  98. select="($entry/ancestor::tgroup/colspec[@colname=$namest]
  99. |$entry/ancestor::entrytbl/colspec[@colname=$namest])[last()]"/>
  100. <xsl:call-template name="colspec.colnum">
  101. <xsl:with-param name="colspec" select="$colspec"/>
  102. </xsl:call-template>
  103. </xsl:when>
  104. <!-- no idea, return 0 -->
  105. <xsl:otherwise>0</xsl:otherwise>
  106. </xsl:choose>
  107. </xsl:template>
  108. <doc:template name="entry.colnum" xmlns="">
  109. <refpurpose>Determine the column number in which a given entry occurs</refpurpose>
  110. <refdescription id="entry.colnum-desc">
  111. <para>If an <tag>entry</tag> has a
  112. <tag class="attribute">colname</tag> or
  113. <tag class="attribute">namest</tag> attribute, this template
  114. will determine the number of the column in which the entry should occur.
  115. For other <tag>entry</tag>s, nothing is returned.</para>
  116. </refdescription>
  117. <refparameter id="entry.colnum-params">
  118. <variablelist>
  119. <varlistentry><term>entry</term>
  120. <listitem>
  121. <para>The <tag>entry</tag>-element which is to be tested.</para>
  122. </listitem>
  123. </varlistentry>
  124. </variablelist>
  125. </refparameter>
  126. <refreturn id="entry.colnum-returns">
  127. <para>This template returns the column number if it can be determined,
  128. or 0 (the empty string)</para>
  129. </refreturn>
  130. </doc:template>
  131. <xsl:template name="colspec.colnum">
  132. <xsl:param name="colspec" select="."/>
  133. <xsl:choose>
  134. <xsl:when test="$colspec/@colnum">
  135. <xsl:value-of select="$colspec/@colnum"/>
  136. </xsl:when>
  137. <xsl:when test="$colspec/preceding-sibling::colspec">
  138. <xsl:variable name="prec.colspec.colnum">
  139. <xsl:call-template name="colspec.colnum">
  140. <xsl:with-param name="colspec"
  141. select="$colspec/preceding-sibling::colspec[1]"/>
  142. </xsl:call-template>
  143. </xsl:variable>
  144. <xsl:value-of select="$prec.colspec.colnum + 1"/>
  145. </xsl:when>
  146. <xsl:otherwise>1</xsl:otherwise>
  147. </xsl:choose>
  148. </xsl:template>
  149. <xsl:template name="calculate.colspan">
  150. <xsl:param name="entry" select="."/>
  151. <xsl:variable name="spanname" select="$entry/@spanname"/>
  152. <xsl:variable name="spanspec"
  153. select="($entry/ancestor::tgroup/spanspec[@spanname=$spanname]
  154. |$entry/ancestor::entrytbl/spanspec[@spanname=$spanname])[last()]"/>
  155. <xsl:variable name="namest">
  156. <xsl:choose>
  157. <xsl:when test="@spanname">
  158. <xsl:value-of select="$spanspec/@namest"/>
  159. </xsl:when>
  160. <xsl:otherwise>
  161. <xsl:value-of select="$entry/@namest"/>
  162. </xsl:otherwise>
  163. </xsl:choose>
  164. </xsl:variable>
  165. <xsl:variable name="nameend">
  166. <xsl:choose>
  167. <xsl:when test="@spanname">
  168. <xsl:value-of select="$spanspec/@nameend"/>
  169. </xsl:when>
  170. <xsl:otherwise>
  171. <xsl:value-of select="$entry/@nameend"/>
  172. </xsl:otherwise>
  173. </xsl:choose>
  174. </xsl:variable>
  175. <xsl:variable name="scol">
  176. <xsl:call-template name="colspec.colnum">
  177. <xsl:with-param name="colspec"
  178. select="($entry/ancestor::tgroup/colspec[@colname=$namest]
  179. |$entry/ancestor::entrytbl/colspec[@colname=$namest])[last()]"/>
  180. </xsl:call-template>
  181. </xsl:variable>
  182. <xsl:variable name="ecol">
  183. <xsl:call-template name="colspec.colnum">
  184. <xsl:with-param name="colspec"
  185. select="($entry/ancestor::tgroup/colspec[@colname=$nameend]
  186. |$entry/ancestor::entrytbl/colspec[@colname=$nameend])[last()]"/>
  187. </xsl:call-template>
  188. </xsl:variable>
  189. <xsl:choose>
  190. <xsl:when test="$namest != '' and $nameend != ''">
  191. <xsl:choose>
  192. <xsl:when test="number($ecol) &gt;= number($scol)">
  193. <xsl:value-of select="number($ecol) - number($scol) + 1"/>
  194. </xsl:when>
  195. <xsl:otherwise>
  196. <xsl:value-of select="number($scol) - number($ecol) + 1"/>
  197. </xsl:otherwise>
  198. </xsl:choose>
  199. </xsl:when>
  200. <xsl:otherwise>1</xsl:otherwise>
  201. </xsl:choose>
  202. </xsl:template>
  203. <xsl:template name="calculate.rowsep">
  204. <xsl:param name="entry" select="."/>
  205. <xsl:param name="colnum" select="0"/>
  206. <xsl:call-template name="inherited.table.attribute">
  207. <xsl:with-param name="entry" select="$entry"/>
  208. <xsl:with-param name="colnum" select="$colnum"/>
  209. <xsl:with-param name="attribute" select="'rowsep'"/>
  210. </xsl:call-template>
  211. </xsl:template>
  212. <xsl:template name="calculate.colsep">
  213. <xsl:param name="entry" select="."/>
  214. <xsl:param name="colnum" select="0"/>
  215. <xsl:call-template name="inherited.table.attribute">
  216. <xsl:with-param name="entry" select="$entry"/>
  217. <xsl:with-param name="colnum" select="$colnum"/>
  218. <xsl:with-param name="attribute" select="'colsep'"/>
  219. </xsl:call-template>
  220. </xsl:template>
  221. <xsl:template name="inherited.table.attribute">
  222. <xsl:param name="entry" select="."/>
  223. <xsl:param name="row" select="$entry/ancestor-or-self::row[1]"/>
  224. <xsl:param name="colnum" select="0"/>
  225. <xsl:param name="attribute" select="'colsep'"/>
  226. <xsl:variable name="tgroup" select="$row/parent::*/parent::tgroup[1]"/>
  227. <xsl:variable name="tbody" select="$row/parent::*[1]"/>
  228. <xsl:variable name="table" select="($tgroup/ancestor::table
  229. |$tgroup/ancestor::informaltable
  230. |$entry/ancestor::entrytbl)[last()]"/>
  231. <xsl:variable name="entry.value">
  232. <xsl:call-template name="get-attribute">
  233. <xsl:with-param name="element" select="$entry"/>
  234. <xsl:with-param name="attribute" select="$attribute"/>
  235. </xsl:call-template>
  236. </xsl:variable>
  237. <xsl:variable name="row.value">
  238. <xsl:call-template name="get-attribute">
  239. <xsl:with-param name="element" select="$row"/>
  240. <xsl:with-param name="attribute" select="$attribute"/>
  241. </xsl:call-template>
  242. </xsl:variable>
  243. <xsl:variable name="span.value">
  244. <xsl:if test="$entry/@spanname">
  245. <xsl:variable name="spanname" select="$entry/@spanname"/>
  246. <xsl:variable name="spanspec"
  247. select="$tgroup/spanspec[@spanname=$spanname]"/>
  248. <xsl:variable name="span.colspec"
  249. select="$tgroup/colspec[@colname=$spanspec/@namest]"/>
  250. <xsl:variable name="spanspec.value">
  251. <xsl:call-template name="get-attribute">
  252. <xsl:with-param name="element" select="$spanspec"/>
  253. <xsl:with-param name="attribute" select="$attribute"/>
  254. </xsl:call-template>
  255. </xsl:variable>
  256. <xsl:variable name="scolspec.value">
  257. <xsl:call-template name="get-attribute">
  258. <xsl:with-param name="element" select="$span.colspec"/>
  259. <xsl:with-param name="attribute" select="$attribute"/>
  260. </xsl:call-template>
  261. </xsl:variable>
  262. <xsl:choose>
  263. <xsl:when test="$spanspec.value != ''">
  264. <xsl:value-of select="$spanspec.value"/>
  265. </xsl:when>
  266. <xsl:when test="$scolspec.value != ''">
  267. <xsl:value-of select="$scolspec.value"/>
  268. </xsl:when>
  269. <xsl:otherwise></xsl:otherwise>
  270. </xsl:choose>
  271. </xsl:if>
  272. </xsl:variable>
  273. <xsl:variable name="namest.value">
  274. <xsl:if test="$entry/@namest">
  275. <xsl:variable name="namest" select="$entry/@namest"/>
  276. <xsl:variable name="colspec"
  277. select="$tgroup/colspec[@colname=$namest]"/>
  278. <xsl:variable name="inner.namest.value">
  279. <xsl:call-template name="get-attribute">
  280. <xsl:with-param name="element" select="$colspec"/>
  281. <xsl:with-param name="attribute" select="$attribute"/>
  282. </xsl:call-template>
  283. </xsl:variable>
  284. <xsl:choose>
  285. <xsl:when test="$inner.namest.value">
  286. <xsl:value-of select="$inner.namest.value"/>
  287. </xsl:when>
  288. <xsl:otherwise></xsl:otherwise>
  289. </xsl:choose>
  290. </xsl:if>
  291. </xsl:variable>
  292. <xsl:variable name="tgroup.value">
  293. <xsl:call-template name="get-attribute">
  294. <xsl:with-param name="element" select="$tgroup"/>
  295. <xsl:with-param name="attribute" select="$attribute"/>
  296. </xsl:call-template>
  297. </xsl:variable>
  298. <xsl:variable name="tbody.value">
  299. <xsl:call-template name="get-attribute">
  300. <xsl:with-param name="element" select="$tbody"/>
  301. <xsl:with-param name="attribute" select="$attribute"/>
  302. </xsl:call-template>
  303. </xsl:variable>
  304. <xsl:variable name="table.value">
  305. <xsl:call-template name="get-attribute">
  306. <xsl:with-param name="element" select="$table"/>
  307. <xsl:with-param name="attribute" select="$attribute"/>
  308. </xsl:call-template>
  309. </xsl:variable>
  310. <xsl:variable name="default.value">
  311. <!-- This section used to say that rowsep and colsep have defaults based -->
  312. <!-- on the frame setting. Further reflection and closer examination of the -->
  313. <!-- CALS spec reveals I was mistaken. The default is "1" for rowsep and colsep. -->
  314. <!-- For everything else, the default is the tgroup value -->
  315. <xsl:choose>
  316. <xsl:when test="$tgroup.value != ''">
  317. <xsl:value-of select="$tgroup.value"/>
  318. </xsl:when>
  319. <xsl:when test="$attribute = 'rowsep'">1</xsl:when>
  320. <xsl:when test="$attribute = 'colsep'">1</xsl:when>
  321. <xsl:otherwise><!-- empty --></xsl:otherwise>
  322. </xsl:choose>
  323. </xsl:variable>
  324. <xsl:variable name="calc.colvalue">
  325. <xsl:if test="$colnum &gt; 0">
  326. <xsl:call-template name="colnum.colspec">
  327. <xsl:with-param name="colnum" select="$colnum"/>
  328. <xsl:with-param name="attribute" select="$attribute"/>
  329. </xsl:call-template>
  330. </xsl:if>
  331. </xsl:variable>
  332. <xsl:choose>
  333. <xsl:when test="$entry.value != ''">
  334. <xsl:value-of select="$entry.value"/>
  335. </xsl:when>
  336. <xsl:when test="$row.value != ''">
  337. <xsl:value-of select="$row.value"/>
  338. </xsl:when>
  339. <xsl:when test="$span.value != ''">
  340. <xsl:value-of select="$span.value"/>
  341. </xsl:when>
  342. <xsl:when test="$namest.value != ''">
  343. <xsl:value-of select="$namest.value"/>
  344. </xsl:when>
  345. <xsl:when test="$calc.colvalue != ''">
  346. <xsl:value-of select="$calc.colvalue"/>
  347. </xsl:when>
  348. <xsl:when test="$tbody.value != ''">
  349. <xsl:value-of select="$tbody.value"/>
  350. </xsl:when>
  351. <xsl:when test="$tgroup.value != ''">
  352. <xsl:value-of select="$tgroup.value"/>
  353. </xsl:when>
  354. <xsl:when test="$table.value != ''">
  355. <xsl:value-of select="$table.value"/>
  356. </xsl:when>
  357. <xsl:otherwise>
  358. <xsl:value-of select="$default.value"/>
  359. </xsl:otherwise>
  360. </xsl:choose>
  361. </xsl:template>
  362. <xsl:template name="colnum.colspec">
  363. <xsl:param name="colnum" select="0"/>
  364. <xsl:param name="attribute" select="'colname'"/>
  365. <xsl:param name="colspec.ancestor"
  366. select="(ancestor::tgroup|ancestor::entrytbl)
  367. [position() = last()]"/>
  368. <xsl:param name="colspecs" select="$colspec.ancestor/colspec"/>
  369. <xsl:param name="count" select="1"/>
  370. <xsl:choose>
  371. <xsl:when test="not($colspecs) or $count &gt; $colnum">
  372. <!-- nop -->
  373. </xsl:when>
  374. <xsl:when test="$colspecs[1]/@colnum">
  375. <xsl:choose>
  376. <xsl:when test="$colspecs[1]/@colnum = $colnum">
  377. <xsl:call-template name="get-attribute">
  378. <xsl:with-param name="element" select="$colspecs[1]"/>
  379. <xsl:with-param name="attribute" select="$attribute"/>
  380. </xsl:call-template>
  381. </xsl:when>
  382. <xsl:otherwise>
  383. <xsl:call-template name="colnum.colspec">
  384. <xsl:with-param name="colnum" select="$colnum"/>
  385. <xsl:with-param name="attribute" select="$attribute"/>
  386. <xsl:with-param name="colspecs"
  387. select="$colspecs[position()&gt;1]"/>
  388. <xsl:with-param name="count"
  389. select="$colspecs[1]/@colnum+1"/>
  390. </xsl:call-template>
  391. </xsl:otherwise>
  392. </xsl:choose>
  393. </xsl:when>
  394. <xsl:otherwise>
  395. <xsl:choose>
  396. <xsl:when test="$count = $colnum">
  397. <xsl:call-template name="get-attribute">
  398. <xsl:with-param name="element" select="$colspecs[1]"/>
  399. <xsl:with-param name="attribute" select="$attribute"/>
  400. </xsl:call-template>
  401. </xsl:when>
  402. <xsl:otherwise>
  403. <xsl:call-template name="colnum.colspec">
  404. <xsl:with-param name="colnum" select="$colnum"/>
  405. <xsl:with-param name="attribute" select="$attribute"/>
  406. <xsl:with-param name="colspecs"
  407. select="$colspecs[position()&gt;1]"/>
  408. <xsl:with-param name="count" select="$count+1"/>
  409. </xsl:call-template>
  410. </xsl:otherwise>
  411. </xsl:choose>
  412. </xsl:otherwise>
  413. </xsl:choose>
  414. </xsl:template>
  415. <xsl:template name="get-attribute">
  416. <xsl:param name="element" select="."/>
  417. <xsl:param name="attribute" select="''"/>
  418. <xsl:for-each select="$element/@*">
  419. <xsl:if test="local-name(.) = $attribute">
  420. <xsl:value-of select="."/>
  421. </xsl:if>
  422. </xsl:for-each>
  423. </xsl:template>
  424. <xsl:template name="consume-row">
  425. <xsl:param name="spans"/>
  426. <xsl:if test="contains($spans,':')">
  427. <xsl:value-of select="substring-before($spans,':') - 1"/>
  428. <xsl:text>:</xsl:text>
  429. <xsl:call-template name="consume-row">
  430. <xsl:with-param name="spans" select="substring-after($spans,':')"/>
  431. </xsl:call-template>
  432. </xsl:if>
  433. </xsl:template>
  434. <!-- Returns the table style for the context element -->
  435. <xsl:template name="tabstyle">
  436. <xsl:param name="node" select="."/>
  437. <xsl:variable name="tgroup" select="$node/tgroup[1] |
  438. $node/ancestor-or-self::tgroup[1]"/>
  439. <xsl:variable name="table"
  440. select="($node/ancestor-or-self::table |
  441. $node/ancestor-or-self::informaltable)[last()]"/>
  442. <xsl:variable name="tabstyle">
  443. <xsl:choose>
  444. <xsl:when test="$table/@tabstyle != ''">
  445. <xsl:value-of select="normalize-space($table/@tabstyle)"/>
  446. </xsl:when>
  447. <xsl:when test="$tgroup/@tgroupstyle != ''">
  448. <xsl:value-of select="normalize-space($tgroup/@tgroupstyle)"/>
  449. </xsl:when>
  450. <xsl:otherwise>
  451. </xsl:otherwise>
  452. </xsl:choose>
  453. </xsl:variable>
  454. <xsl:value-of select="$tabstyle"/>
  455. </xsl:template>
  456. </xsl:stylesheet>