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