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