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: table.xsl,v 1.18 2005/12/15 18:18:59 bobstayton Exp $
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
130will determine the number of the column in which the entry should occur.
131For 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,
145or 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/parent::*/parent::tgroup[1]"/>
257
258  <xsl:variable name="table" select="($tgroup/ancestor::table
259                                     |$tgroup/ancestor::informaltable
260				     |$entry/ancestor::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/spanspec[@spanname=$spanname]"/>
281      <xsl:variable name="span.colspec"
282                    select="$tgroup/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/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="table.value">
340    <xsl:call-template name="get-attribute">
341      <xsl:with-param name="element" select="$table"/>
342      <xsl:with-param name="attribute" select="$attribute"/>
343    </xsl:call-template>
344  </xsl:variable>
345
346  <xsl:variable name="default.value">
347    <!-- This section used to say that rowsep and colsep have defaults based -->
348    <!-- on the frame setting. Further reflection and closer examination of the -->
349    <!-- CALS spec reveals I was mistaken. The default is "1" for rowsep and colsep. -->
350    <!-- For everything else, the default is the tgroup value -->
351    <xsl:choose>
352      <xsl:when test="$tgroup.value != ''">
353        <xsl:value-of select="$tgroup.value"/>
354      </xsl:when>
355      <xsl:when test="$attribute = 'rowsep'">1</xsl:when>
356      <xsl:when test="$attribute = 'colsep'">1</xsl:when>
357      <xsl:otherwise><!-- empty --></xsl:otherwise>
358    </xsl:choose>
359  </xsl:variable>
360
361  <xsl:variable name="calc.colvalue">
362    <xsl:if test="$colnum &gt; 0">
363      <xsl:call-template name="colnum.colspec">
364        <xsl:with-param name="colnum" select="$colnum"/>
365        <xsl:with-param name="attribute" select="$attribute"/>
366      </xsl:call-template>
367    </xsl:if>
368  </xsl:variable>
369
370  <xsl:choose>
371    <xsl:when test="$entry.value != ''">
372      <xsl:value-of select="$entry.value"/>
373    </xsl:when>
374    <xsl:when test="$row.value != ''">
375      <xsl:value-of select="$row.value"/>
376    </xsl:when>
377    <xsl:when test="$span.value != ''">
378      <xsl:value-of select="$span.value"/>
379    </xsl:when>
380    <xsl:when test="$namest.value != ''">
381      <xsl:value-of select="$namest.value"/>
382    </xsl:when>
383    <xsl:when test="$calc.colvalue != ''">
384      <xsl:value-of select="$calc.colvalue"/>
385    </xsl:when>
386    <xsl:when test="$tgroup.value != ''">
387      <xsl:value-of select="$tgroup.value"/>
388    </xsl:when>
389    <xsl:when test="$table.value != ''">
390      <xsl:value-of select="$table.value"/>
391    </xsl:when>
392    <xsl:otherwise>
393      <xsl:value-of select="$default.value"/>
394    </xsl:otherwise>
395  </xsl:choose>
396</xsl:template>
397
398<xsl:template name="colnum.colspec">
399  <xsl:param name="colnum" select="0"/>
400  <xsl:param name="attribute" select="'colname'"/>
401  <xsl:param name="colspec.ancestor" 
402             select="(ancestor::tgroup|ancestor::entrytbl)
403	             [position() = last()]"/>
404  <xsl:param name="colspecs" select="$colspec.ancestor/colspec"/>
405  <xsl:param name="count" select="1"/>
406
407  <xsl:choose>
408    <xsl:when test="not($colspecs) or $count &gt; $colnum">
409      <!-- nop -->
410    </xsl:when>
411    <xsl:when test="$colspecs[1]/@colnum">
412      <xsl:choose>
413        <xsl:when test="$colspecs[1]/@colnum = $colnum">
414          <xsl:call-template name="get-attribute">
415            <xsl:with-param name="element" select="$colspecs[1]"/>
416            <xsl:with-param name="attribute" select="$attribute"/>
417          </xsl:call-template>
418        </xsl:when>
419        <xsl:otherwise>
420          <xsl:call-template name="colnum.colspec">
421            <xsl:with-param name="colnum" select="$colnum"/>
422            <xsl:with-param name="attribute" select="$attribute"/>
423            <xsl:with-param name="colspecs"
424                            select="$colspecs[position()&gt;1]"/>
425            <xsl:with-param name="count"
426                            select="$colspecs[1]/@colnum+1"/>
427          </xsl:call-template>
428        </xsl:otherwise>
429      </xsl:choose>
430    </xsl:when>
431    <xsl:otherwise>
432      <xsl:choose>
433        <xsl:when test="$count = $colnum">
434          <xsl:call-template name="get-attribute">
435            <xsl:with-param name="element" select="$colspecs[1]"/>
436            <xsl:with-param name="attribute" select="$attribute"/>
437          </xsl:call-template>
438        </xsl:when>
439        <xsl:otherwise>
440          <xsl:call-template name="colnum.colspec">
441            <xsl:with-param name="colnum" select="$colnum"/>
442            <xsl:with-param name="attribute" select="$attribute"/>
443            <xsl:with-param name="colspecs"
444                            select="$colspecs[position()&gt;1]"/>
445            <xsl:with-param name="count" select="$count+1"/>
446          </xsl:call-template>
447        </xsl:otherwise>
448      </xsl:choose>
449    </xsl:otherwise>
450  </xsl:choose>
451</xsl:template>
452
453<xsl:template name="get-attribute">
454  <xsl:param name="element" select="."/>
455  <xsl:param name="attribute" select="''"/>
456
457  <xsl:for-each select="$element/@*">
458    <xsl:if test="local-name(.) = $attribute">
459      <xsl:value-of select="."/>
460    </xsl:if>
461  </xsl:for-each>
462</xsl:template>
463
464</xsl:stylesheet>
465