]> git.stg.codes - stg.git/blob - doc/xslt/roundtrip/blocks2dbk.xsl
Added SERVICES::Find for resetable value.
[stg.git] / doc / xslt / roundtrip / blocks2dbk.xsl
1 <!DOCTYPE xsl:stylesheet [
2 <!-- External DTD defines entities:
3      components :- QNames of component-level elements
4      blocks :- QNames of block-level elements
5      metadata-content :- XPath expression matching metadata styles
6      author-content :- XPath expression matching author styles
7      admonition :- XPath expression matching admonition styles
8      admonition-title :- XPath expression matching admonition title styles
9 -->
10 <!ENTITY % ext SYSTEM "blocks2dbk.dtd">
11 %ext;
12 ]>
13 <xsl:stylesheet version="1.0"
14   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
15   xmlns:dbk='http://docbook.org/ns/docbook'
16   xmlns:rnd='http://docbook.org/ns/docbook/roundtrip'
17   xmlns:xlink='http://www.w3.org/1999/xlink'>
18
19   <!-- $Id: blocks2dbk.xsl 8137 2008-10-09 03:37:56Z balls $ -->
20   <!-- Stylesheet to convert word processing docs to DocBook -->
21   <!-- This stylesheet processes the output of sections2blocks.xsl -->
22
23   <xsl:output indent="yes" method="xml"
24     cdata-section-elements='dbk:programlisting dbk:literallayout'/>
25
26   <!-- ================================================== -->
27   <!--    Parameters                                      -->
28   <!-- ================================================== -->
29
30   <xsl:param name='docbook5'>0</xsl:param>
31   <xsl:param name="nest.sections">1</xsl:param>
32
33   <xsl:strip-space elements='*'/>
34   <xsl:preserve-space elements='dbk:para dbk:emphasis'/>
35
36   <xsl:template match="&components; |
37                        &blocks;">
38     <xsl:choose>
39       <xsl:when test='self::dbk:article and
40                       count(dbk:book) = 1 and
41                       count(dbk:info|dbk:book) &lt;= 2'>
42         <xsl:apply-templates select='dbk:book'/>
43       </xsl:when>
44       <xsl:when test='self::dbk:article and
45                       *[1][self::dbk:para] and
46                       *[1]/@rnd:style != "article-title" and
47                       contains(*[1]/@rnd:style, "-title")'>
48         <xsl:variable name='element-name'
49           select='substring-before(*[1]/@rnd:style, "-title")'/>
50         <xsl:element name='{$element-name}'
51           namespace='http://docbook.org/ns/docbook'>
52           <dbk:info>
53             <xsl:apply-templates select='*[1]'
54               mode='rnd:metadata'/>
55           </dbk:info>
56           <xsl:apply-templates/>
57         </xsl:element>
58       </xsl:when>
59       <xsl:otherwise>
60         <xsl:copy>
61           <xsl:call-template name='rnd:attributes'/>
62
63           <xsl:variable name='metadata'>
64             <xsl:apply-templates select='*[1]'
65               mode='rnd:metadata'/>
66           </xsl:variable>
67           <xsl:if test='$metadata'>
68             <dbk:info>
69               <xsl:copy-of select='$metadata'/>
70             </dbk:info>
71           </xsl:if>
72
73           <xsl:apply-templates/>
74         </xsl:copy>
75       </xsl:otherwise>
76     </xsl:choose>
77   </xsl:template>
78
79   <xsl:template match='dbk:book-component'>
80     <xsl:variable name='element-name'
81       select='substring-before(*[1]/@rnd:style, "-title")'/>
82
83     <xsl:element name='{$element-name}'
84       namespace='http://docbook.org/ns/docbook'>
85       <dbk:info>
86         <xsl:apply-templates select='*[1]'
87           mode='rnd:metadata'/>
88       </dbk:info>
89       <xsl:apply-templates/>
90     </xsl:element>
91   </xsl:template>
92
93   <xsl:template match="dbk:para" name='rnd:para'>
94     <!-- Some elements are normally suppressed,
95          since they are processed in a different context.
96          If this parameter is false then the element will be processed normally.
97       -->
98     <xsl:param name='suppress' select='true()'/>
99
100     <!-- This paragraph may be in a sidebar -->
101     <xsl:variable name='sidebar'
102                   select='preceding-sibling::*[self::dbk:para and @rnd:style = "sidebar-title"][1]'/>
103
104     <!-- This paragraph may be in the textobject of a table or figure -->
105     <xsl:variable name='table'
106                   select='preceding-sibling::dbk:informaltable[1]'/>
107     <xsl:variable name='figure'
108                   select='preceding-sibling::dbk:para[@rnd:style = "informalfigure-imagedata" or (dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = "")][1]'/>
109     <xsl:variable name='caption'
110                   select='following-sibling::dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"]'/>
111
112     <xsl:choose>
113       <!-- continue style paragraphs are handled in context -->
114       <xsl:when test='$suppress and
115                       @rnd:style = "para-continue"'/>
116
117       <!-- Certain elements gather the following paragraph -->
118       <xsl:when test='$suppress and
119                       preceding-sibling::*[1][self::dbk:para and
120                       @rnd:style = "example-title"]'/>
121
122       <xsl:when test='$suppress and
123                       $sidebar and
124                       not(preceding-sibling::dbk:para[(not(@rnd:style) or @rnd:style = "") and
125                         preceding-sibling::*[preceding-sibling::*[generate-id() = generate-id($sidebar)]]])'/>
126
127       <!-- Separate processing is performed for table/figure titles and captions -->
128       <xsl:when test='$suppress and
129                       @rnd:style = "table-title" and
130                       following-sibling::*[1][self::dbk:informaltable|self::dbk:para[@rnd:style = "informalfigure-imagedata"]|self::dbk:para[dbk:inlinemediaobject and count(*) = 1 and normalize-space() = ""]]'/>
131       <xsl:when test='$suppress and
132                       @rnd:style = "figure-title" and
133                       following-sibling::*[1][self::dbk:para][@rnd:style = "informalfigure-imagedata" or (dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = "")]'/>
134       <xsl:when test='$suppress and
135                       (@rnd:style = "caption" or @rnd:style = "Caption") and
136                       (preceding-sibling::*[self::dbk:informaltable] or
137                       preceding-sibling::*[self::dbk:para][@rnd:style = "informalfigure-imagedata" or (dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = "")])'/>
138
139       <xsl:when test='$suppress and
140                       $table and
141                       $caption and
142                       generate-id($caption/preceding-sibling::dbk:informaltable[1]) = generate-id($table)'/>
143       <xsl:when test='$suppress and
144                       $figure and
145                       $caption and
146                       generate-id($caption/preceding-sibling::dbk:para[@rnd:style = "informalfigure-imagedata" or (dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = "")][1]) = generate-id($figure)'/>
147
148       <xsl:when test='@rnd:style = "imagedata-metadata" or
149                       @rnd:style = "table-metadata"'/>
150
151       <!-- Ignore empty paragraphs -->
152       <xsl:when test='(not(@rnd:style) or
153                       @rnd:style = "") and
154                       normalize-space(.) = "" and
155                       not(*)'/>
156
157       <!-- Image inline or block? -->
158       <xsl:when test='(not(@rnd:style) or
159                       @rnd:style = "") and
160                       normalize-space(.) = "" and
161                       count(*) = 1 and
162                       dbk:inlinemediaobject'>
163         <xsl:apply-templates/>
164       </xsl:when>
165       <xsl:when test='not(@rnd:style) or
166                       @rnd:style = "" or
167                       @rnd:style = "para-continue"'>
168         <dbk:para>
169           <xsl:call-template name='rnd:attributes'/>
170           <xsl:apply-templates/>
171         </dbk:para>
172       </xsl:when>
173
174       <xsl:when test='@rnd:style = "xinclude"'
175         xmlns:xi='http://www.w3.org/2001/XInclude'>
176         <xi:include>
177           <xsl:attribute name='href'>
178             <xsl:apply-templates mode='rnd:xinclude'/>
179           </xsl:attribute>
180         </xi:include>
181       </xsl:when>
182
183       <xsl:when test='$suppress and
184                       preceding-sibling::*[1]/self::dbk:para[&admonition-title;]'/>
185       <xsl:when test='&admonition-title;'>
186         <xsl:element name='{substring-before(@rnd:style, "-title")}'
187           namespace='http://docbook.org/ns/docbook'>
188           <xsl:call-template name='rnd:attributes'/>
189           <dbk:title>
190             <xsl:apply-templates/>
191           </dbk:title>
192           <xsl:apply-templates select='following-sibling::*[1]'>
193             <xsl:with-param name='suppress' select='false()'/>
194           </xsl:apply-templates>
195           <xsl:apply-templates select='following-sibling::*[2]'
196             mode='rnd:continue'/>
197         </xsl:element>
198       </xsl:when>
199
200       <xsl:when test='starts-with(@rnd:style, "itemizedlist") or
201                       starts-with(@rnd:style, "orderedlist")'>
202
203         <xsl:variable name='stop.node'
204           select='following-sibling::dbk:para[not(@rnd:style) or
205                   (not(starts-with(@rnd:style, "itemizedlist") or starts-with(@rnd:style, "orderedlist")) and @rnd:style != "para-continue")][1]'/>
206
207         <xsl:choose>
208           <xsl:when test='translate(substring-after(@rnd:style, "list"), "0123456789", "") != "" or
209                           substring-after(@rnd:style, "list") = ""'>
210             <xsl:call-template name='rnd:error'>
211               <xsl:with-param name='code' select='"list-bad-level"'/>
212               <xsl:with-param name='message'>style "<xsl:value-of select='@rnd:style'/>" is not a valid list style</xsl:with-param>
213             </xsl:call-template>
214           </xsl:when>
215
216           <!-- TODO: the previous para-continue may not be associated with a list -->
217
218           <!-- NB. Look back at the previous paragraph. There may be intervening tables or images. -->
219           <xsl:when test='preceding-sibling::dbk:para[1][starts-with(@rnd:style, "itemizedlist") or starts-with(@rnd:style, "orderedlist") or @rnd:style = "para-continue"]'/>
220           <xsl:when test='substring-after(@rnd:style, "list") != 1'>
221             <xsl:call-template name='rnd:error'>
222               <xsl:with-param name='code'>list-wrong-level</xsl:with-param>
223               <xsl:with-param name='message'>list started at the wrong level</xsl:with-param>
224             </xsl:call-template>
225           </xsl:when>
226           <xsl:when test='$stop.node'>
227             <xsl:element name='{substring-before(@rnd:style, "1")}'
228               namespace='http://docbook.org/ns/docbook'>
229               <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = current()/@rnd:style][following-sibling::*[generate-id() = generate-id($stop.node)]]'
230                 mode='rnd:listitem'/>
231             </xsl:element>
232           </xsl:when>
233           <xsl:otherwise>
234             <xsl:element name='{substring-before(@rnd:style, "1")}'
235               namespace='http://docbook.org/ns/docbook'>
236               <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = current()/@rnd:style]'
237                 mode='rnd:listitem'/>
238             </xsl:element>
239           </xsl:otherwise>
240         </xsl:choose>
241       </xsl:when>
242
243       <xsl:when test='@rnd:style = "programlisting" and
244                       preceding-sibling::*[1][self::dbk:para and @rnd:style = "programlisting"]'/>
245       <xsl:when test='@rnd:style = "literallayout" and
246                       preceding-sibling::*[1][self::dbk:para and @rnd:style = "literallayout"]'/>
247       <xsl:when test='@rnd:style = "programlisting" or
248                       @rnd:style = "literallayout"'>
249
250         <xsl:variable name='stop.node'
251           select='following-sibling::dbk:para[@rnd:style != current()/@rnd:style][1]'/>
252
253         <xsl:element name='{@rnd:style}'
254           namespace='http://docbook.org/ns/docbook'>
255           <xsl:apply-templates/>
256
257           <xsl:choose>
258             <xsl:when test='$stop.node'>
259               <xsl:apply-templates select='following-sibling::dbk:para[following-sibling::*[generate-id() = generate-id($stop.node)]]'
260                 mode='rnd:programlisting'/>
261             </xsl:when>
262             <xsl:otherwise>
263               <xsl:apply-templates select='following-sibling::dbk:para'
264                 mode='rnd:programlisting'/>
265             </xsl:otherwise>
266           </xsl:choose>
267         </xsl:element>
268       </xsl:when>
269
270       <xsl:when test='@rnd:style = "example-title"'>
271         <xsl:element name='{substring-before(@rnd:style, "-title")}'
272           namespace='http://docbook.org/ns/docbook'>
273           <xsl:call-template name='rnd:attributes'/>
274           <dbk:title>
275             <xsl:apply-templates/>
276           </dbk:title>
277
278           <xsl:apply-templates select='following-sibling::*[1]'>
279             <xsl:with-param name='suppress' select='false()'/>
280           </xsl:apply-templates>
281         </xsl:element>
282       </xsl:when>
283
284       <xsl:when test='@rnd:style = "sidebar-title"'>
285         <!-- TODO: next sidebar should stop this sidebar -->
286         <xsl:variable name='stop.node'
287                       select='following-sibling::dbk:para[(not(@rnd:style) or @rnd:style = "") and
288                               normalize-space(.) = ""][1]'/>
289
290         <dbk:sidebar>
291           <xsl:call-template name='rnd:attributes'/>
292           <dbk:info>
293             <dbk:title>
294               <xsl:apply-templates/>
295             </dbk:title>
296           </dbk:info>
297
298           <xsl:choose>
299             <xsl:when test='$stop.node'>
300               <xsl:apply-templates select='following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]]'
301                                    mode='rnd:sidebar'/>
302             </xsl:when>
303             <xsl:otherwise>
304               <xsl:apply-templates select='following-sibling::*'
305                                    mode='rnd:sidebar'/>
306             </xsl:otherwise>
307           </xsl:choose>
308         </dbk:sidebar>
309       </xsl:when>
310
311       <xsl:when test='&admonition;'>
312         <xsl:element name='{@rnd:style}'
313           namespace='http://docbook.org/ns/docbook'>
314           <xsl:call-template name='rnd:attributes'/>
315           <dbk:para>
316             <xsl:apply-templates/>
317           </dbk:para>
318           <xsl:apply-templates select='following-sibling::*[1]'
319             mode='rnd:continue'/>
320         </xsl:element>
321       </xsl:when>
322
323       <!-- TODO: make sure this is in a bibliography.
324            If not, create a bibliolist.
325         -->
326       <xsl:when test='@rnd:style = "bibliomixed"'>
327         <dbk:bibliomixed>
328           <xsl:call-template name='rnd:attributes'/>
329           <xsl:apply-templates/>
330         </dbk:bibliomixed>
331       </xsl:when>
332       <xsl:when test='@rnd:style = "biblioentry-title"'>
333         <dbk:biblioentry>
334           <xsl:call-template name='rnd:attributes'/>
335           <dbk:title>
336             <xsl:apply-templates/>
337           </dbk:title>
338           <xsl:if test='following-sibling::*[1][&metadata-content;]'>
339             <xsl:apply-templates select='following-sibling::*[1]'
340               mode='rnd:metadata'/>
341           </xsl:if>
342         </dbk:biblioentry>
343       </xsl:when>
344
345       <xsl:when test='@rnd:style = "blockquote-attribution" and
346                       preceding-sibling::*[1][self::dbk:para][@rnd:style = "blockquote-title" or @rnd:style = "blockquote"]'/>
347       <xsl:when test='@rnd:style = "blockquote-attribution"'>
348         <xsl:call-template name='rnd:error'>
349           <xsl:with-param name='code'>improper-blockquote-attribution</xsl:with-param>
350           <xsl:with-param name='message'>blockquote attribution must follow a blockquote title</xsl:with-param>
351         </xsl:call-template>
352       </xsl:when>
353       <xsl:when test='@rnd:style = "blockquote" or
354                       @rnd:style = "blockquote-title"'>
355         <xsl:choose>
356           <xsl:when test='@rnd:style = "blockquote" and
357                           preceding-sibling::*[1][self::dbk:para][starts-with(@rnd:style, "blockquote")]'/>
358           <xsl:otherwise>
359
360             <xsl:variable name='stop.node'
361               select='following-sibling::*[not(@rnd:style = "blockquote" or
362                       @rnd:style = "blockquote-attribution")][1]'/>
363
364             <dbk:blockquote>
365               <xsl:call-template name='rnd:attributes'/>
366               <xsl:if test='@rnd:style = "blockquote-title"'>
367                 <dbk:info>
368                   <dbk:title>
369                     <xsl:apply-templates/>
370                   </dbk:title>
371                 </dbk:info>
372               </xsl:if>
373               <xsl:choose>
374                 <xsl:when test='$stop.node'>
375                   <xsl:apply-templates select='following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]][@rnd:style = "blockquote-attribution"]' mode='rnd:blockquote-attribution'/>
376                   <xsl:apply-templates select='self::*[@rnd:style = "blockquote"] |
377                                                following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]]'
378                     mode='rnd:blockquote'/>
379                 </xsl:when>
380                 <xsl:otherwise>
381                   <xsl:apply-templates select='following-sibling::*[@rnd:style = "blockquote-attribution"]' mode='rnd:blockquote-attribution'/>
382                   <xsl:apply-templates select='self::*[@rnd:style = "blockquote"] |
383                                                following-sibling::*'
384                     mode='rnd:blockquote'/>
385                 </xsl:otherwise>
386               </xsl:choose>
387             </dbk:blockquote>
388           </xsl:otherwise>
389         </xsl:choose>
390       </xsl:when>
391
392       <xsl:when test='@rnd:style = "bridgehead"'>
393         <xsl:element name='{@rnd:style}'
394           namespace='http://docbook.org/ns/docbook'>
395           <xsl:call-template name='rnd:attributes'/>
396           <xsl:apply-templates/>
397         </xsl:element>
398       </xsl:when>
399
400       <xsl:when test='@rnd:style = "formalpara-title"'>
401         <dbk:formalpara>
402           <dbk:title>
403             <xsl:call-template name='rnd:attributes'/>
404             <xsl:apply-templates/>
405           </dbk:title>
406           <xsl:choose>
407             <xsl:when test='following-sibling::*[1][self::dbk:para][@rnd:style = "formalpara"]'>
408               <dbk:para>
409                 <xsl:call-template name='rnd:attributes'>
410                   <xsl:with-param name='node'
411                     select='following-sibling::*[1]'/>
412                 </xsl:call-template>
413                 <xsl:apply-templates select='following-sibling::*[1]/node()'/>
414               </dbk:para>
415             </xsl:when>
416           </xsl:choose>
417         </dbk:formalpara>
418       </xsl:when>
419       <xsl:when test='@rnd:style = "formalpara" and
420                       preceding-sibling::*[1][self::dbk:para][@rnd:style = "formalpara-title"]'/>
421       <xsl:when test='@rnd:style = "formalpara"'>
422         <xsl:call-template name='rnd:error'>
423           <xsl:with-param name='code'>formalpara-notitle</xsl:with-param>
424           <xsl:with-param name='message'>formalpara used without a title</xsl:with-param>
425         </xsl:call-template>
426       </xsl:when>
427
428       <xsl:when test='@rnd:style = "informalfigure-imagedata"'>
429         <xsl:variable name='caption.next'
430           select='following-sibling::dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"][1]'/>
431
432         <xsl:variable name='metadata'
433           select='preceding-sibling::*[1][self::dbk:para][@rnd:style = "imagedata-metadata"]'/>
434
435         <xsl:choose>
436           <xsl:when test='preceding-sibling::*[1][self::dbk:para][@rnd:style = "figure-title"] or
437                           ($metadata and preceding-sibling::*[2][self::dbk:para][@rnd:style = "figure-title"])'>
438             <dbk:figure>
439               <xsl:call-template name='rnd:attributes'/>
440               <dbk:info>
441                 <dbk:title>
442                   <xsl:choose>
443                     <xsl:when test='$metadata'>
444                       <xsl:apply-templates
445                         select='preceding-sibling::*[2]/node()'/>
446                     </xsl:when>
447                     <xsl:otherwise>
448                       <xsl:apply-templates
449                         select='preceding-sibling::*[1]/node()'/>
450                     </xsl:otherwise>
451                   </xsl:choose>
452                 </dbk:title>
453               </dbk:info>
454               <dbk:mediaobject>
455                 <dbk:imageobject>
456                   <dbk:imagedata>
457                     <xsl:attribute name='fileref'>
458                       <xsl:choose>
459                         <xsl:when test='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref != ""'>
460                           <xsl:value-of select='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref'/>
461                         </xsl:when>
462                         <xsl:otherwise>
463                           <xsl:value-of select='.'/>
464                         </xsl:otherwise>
465                       </xsl:choose>
466                     </xsl:attribute>
467                     <xsl:call-template name='rnd:imagedata-attributes'>
468                       <xsl:with-param name='metadata' select='$metadata'/>
469                     </xsl:call-template>
470                   </dbk:imagedata>
471                 </dbk:imageobject>
472               </dbk:mediaobject>
473               <xsl:call-template name='rnd:figure-text-caption'>
474                 <xsl:with-param name='caption' select='$caption.next'/>
475               </xsl:call-template>
476             </dbk:figure>
477           </xsl:when>
478           <xsl:when test='preceding-sibling::*[1][self::dbk:para][@rnd:style = "table-title"] or
479                           ($metadata and preceding-sibling::*[2][self::dbk:para][@rnd:style = "table-title"])'>
480             <dbk:table>
481               <xsl:call-template name='rnd:attributes'/>
482               <dbk:info>
483                 <dbk:title>
484                   <xsl:choose>
485                     <xsl:when test='$metadata'>
486                       <xsl:apply-templates
487                         select='preceding-sibling::*[2]/node()'/>
488                     </xsl:when>
489                     <xsl:otherwise>
490                       <xsl:apply-templates
491                         select='preceding-sibling::*[1]/node()'/>
492                     </xsl:otherwise>
493                   </xsl:choose>
494                 </dbk:title>
495               </dbk:info>
496
497               <dbk:mediaobject>
498                 <dbk:imageobject>
499                   <dbk:imagedata>
500                     <xsl:attribute name='fileref'>
501                       <xsl:choose>
502                         <xsl:when test='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref != ""'>
503                           <xsl:value-of select='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref'/>
504                         </xsl:when>
505                         <xsl:otherwise>
506                           <xsl:value-of select='.'/>
507                         </xsl:otherwise>
508                       </xsl:choose>
509                     </xsl:attribute>
510                     <xsl:call-template name='rnd:imagedata-attributes'>
511                       <xsl:with-param name='metadata' select='$metadata'/>
512                     </xsl:call-template>
513                   </dbk:imagedata>
514                 </dbk:imageobject>
515               </dbk:mediaobject>
516               <xsl:call-template name='rnd:figure-text-caption'>
517                 <xsl:with-param name='caption' select='$caption.next'/>
518               </xsl:call-template>
519             </dbk:table>
520           </xsl:when>
521           <xsl:otherwise>
522             <dbk:informalfigure>
523               <xsl:call-template name='rnd:attributes'/>
524               <dbk:mediaobject>
525                 <dbk:imageobject>
526                   <dbk:imagedata>
527                     <xsl:attribute name='fileref'>
528                       <xsl:choose>
529                         <xsl:when test='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref != ""'>
530                           <xsl:value-of select='dbk:inlinemediaobject/dbk:imageobject/dbk:imagedata/@fileref'/>
531                         </xsl:when>
532                         <xsl:otherwise>
533                           <xsl:value-of select='.'/>
534                         </xsl:otherwise>
535                       </xsl:choose>
536                     </xsl:attribute>
537                     <xsl:call-template name='rnd:imagedata-attributes'>
538                       <xsl:with-param name='metadata' select='$metadata'/>
539                     </xsl:call-template>
540                   </dbk:imagedata>
541                 </dbk:imageobject>
542               </dbk:mediaobject>
543               <xsl:call-template name='rnd:figure-text-caption'>
544                 <xsl:with-param name='caption' select='$caption.next'/>
545               </xsl:call-template>
546             </dbk:informalfigure>
547           </xsl:otherwise>
548         </xsl:choose>
549       </xsl:when>
550
551       <xsl:when test='(@rnd:style = "caption" or @rnd:style = "Caption") and
552                       preceding-sibling::*[(self::dbk:para and contains(@rnd:style, "imagedata")) or self::dbk:informaltable]'/>
553       <xsl:when test='@rnd:style = "caption" or @rnd:style = "Caption"'>
554         <xsl:call-template name='rnd:error'>
555           <xsl:with-param name='code'>bad-caption</xsl:with-param>
556           <xsl:with-param name='message'>caption does not follow table or figure</xsl:with-param>
557         </xsl:call-template>
558       </xsl:when>
559
560       <xsl:when test='(contains(@rnd:style, "-title") or
561                       contains(@rnd:style, "-titleabbrev") or
562                       contains(@rnd:style, "-subtitle")) and
563                       not(starts-with(@rnd:style, "blockquote") or starts-with(@rnd:style, "formal"))'>
564         <!-- TODO: check that no non-metadata elements occur before this paragraph -->
565       </xsl:when>
566
567       <!-- Metadata elements are handled in rnd:metadata mode -->
568       <!-- TODO: check that no non-metadata elements occur before this paragraph -->
569       <xsl:when test='&metadata-content;'/>
570
571       <xsl:otherwise>
572         <xsl:call-template name='rnd:error'>
573           <xsl:with-param name='code'>unknown-style</xsl:with-param>
574           <xsl:with-param name='message'>unknown paragraph style "<xsl:value-of select='@rnd:style'/>" encountered</xsl:with-param>
575         </xsl:call-template>
576       </xsl:otherwise>
577     </xsl:choose>
578   </xsl:template>
579
580   <!-- Determine if the caption belongs to the current figure.
581        If so, then process the textobject content and the caption.
582     -->
583   <xsl:template name='rnd:figure-text-caption'>
584     <xsl:param name='caption' select='/..'/>
585
586     <xsl:variable name='textobjs'
587       select='following-sibling::*[following-sibling::*[generate-id() = generate-id($caption)]]'/>
588
589     <xsl:choose>
590       <xsl:when test='not($caption)'/> <!-- nothing to do -->
591       <xsl:when test='$textobjs[self::dbk:informaltable |
592                       self::dbk:inlinemediaobject |
593                       self::dbk:para[@rnd:style = "informalfigure-imagedata" or
594                       @rnd:style = "mediaobject-imagedata"]]'/> <!-- caption belongs to something else -->
595       <xsl:otherwise>
596         <xsl:apply-templates select='$textobjs' mode='rnd:textobject'/>
597
598         <xsl:apply-templates select='$caption' mode='rnd:caption'/>
599       </xsl:otherwise>
600     </xsl:choose>
601   </xsl:template>
602
603   <xsl:template match='dbk:para' mode='rnd:caption'>
604     <dbk:caption>
605       <xsl:call-template name='rnd:attributes'/>
606       <xsl:apply-templates/>
607     </dbk:caption>
608   </xsl:template>
609
610   <xsl:template name='rnd:imagedata-attributes'>
611     <xsl:param name='metadata' select='""'/>
612
613     <xsl:choose>
614       <xsl:when test='not($metadata)'/>
615       <xsl:when test='contains($metadata, " ")'>
616         <xsl:call-template name='rnd:imagedata-attributes'>
617           <xsl:with-param name='metadata'
618             select='substring-before($metadata, " ")'/>
619         </xsl:call-template>
620         <xsl:call-template name='rnd:imagedata-attributes'>
621           <xsl:with-param name='metadata'
622             select='substring-after($metadata, " ")'/>
623         </xsl:call-template>
624       </xsl:when>
625       <xsl:when test='not(contains($metadata, "="))'>
626         <xsl:call-template name='rnd:warning'>
627           <xsl:with-param name='code' select='"imagedata-missing-value"'/>
628           <xsl:with-param name='message'>
629             <xsl:text>imagedata-metadata missing value for attribute "</xsl:text>
630             <xsl:value-of select='$metadata'/>
631             <xsl:text>"</xsl:text>
632           </xsl:with-param>
633         </xsl:call-template>
634       </xsl:when>
635       <xsl:otherwise>
636         <xsl:variable name='name'
637           select='translate(normalize-space(substring-before($metadata, "=")), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'/>
638         <xsl:variable name='value'
639           select='translate(normalize-space(substring-after($metadata, "=")), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'/>
640
641         <xsl:choose>
642           <xsl:when test='$name = "scalefit"'>
643             <xsl:attribute name='scalefit'>
644               <xsl:choose>
645                 <xsl:when test='$value = "1" or
646                                 $value = "yes" or
647                                 $value = "true"'>1</xsl:when>
648                 <xsl:otherwise>0</xsl:otherwise>
649               </xsl:choose>
650             </xsl:attribute>
651           </xsl:when>
652           <xsl:when test='$name = "align" or
653                           $name = "contentdepth" or
654                           $name = "contentwidth" or
655                           $name = "depth" or
656                           $name = "scale" or
657                           $name = "valign" or
658                           $name = "width"'>
659             <!-- TODO: check enumerate values-->
660             <xsl:attribute name='{$name}'>
661               <xsl:value-of select='$value'/>
662             </xsl:attribute>
663           </xsl:when>
664           <xsl:otherwise>
665             <xsl:call-template name='rnd:error'>
666               <xsl:with-param name='code' select='"imagedata-unknown-attribute"'/>
667               <xsl:with-param name='message'>
668                 <xsl:text>imagedata-metadata unknown attribute "</xsl:text>
669                 <xsl:value-of select='$name'/>
670                 <xsl:text>"</xsl:text>
671               </xsl:with-param>
672             </xsl:call-template>
673           </xsl:otherwise>
674         </xsl:choose>
675       </xsl:otherwise>
676     </xsl:choose>
677   </xsl:template>
678
679   <xsl:template match='dbk:emphasis'>
680     <xsl:choose>
681       <xsl:when test='not(@rnd:style) and @role = "italic"'>
682         <xsl:copy>
683           <xsl:apply-templates mode='rnd:copy'/>
684         </xsl:copy>
685       </xsl:when>
686       <xsl:when test='not(@rnd:style) or @role'>
687         <xsl:copy>
688           <xsl:call-template name='rnd:attributes'/>
689           <xsl:apply-templates mode='rnd:copy'/>
690         </xsl:copy>
691       </xsl:when>
692
693       <xsl:when test='@rnd:style = preceding-sibling::node()[1][self::dbk:emphasis]/@rnd:style'/>
694
695       <xsl:when test='@rnd:style = "emphasis"'>
696         <xsl:copy>
697           <xsl:call-template name='rnd:attributes'/>
698           <xsl:apply-templates mode='rnd:copy'/>
699           <xsl:apply-templates select='following-sibling::node()[1]'
700             mode='rnd:emphasis'/>
701         </xsl:copy>
702       </xsl:when>
703       <xsl:when test='@rnd:style = "emphasis-bold" or
704                       @rnd:style = "emphasis-strong"'>
705         <xsl:copy>
706           <xsl:attribute name='role'>bold</xsl:attribute>
707           <xsl:call-template name='rnd:attributes'/>
708           <xsl:apply-templates mode='rnd:copy'/>
709           <xsl:apply-templates select='following-sibling::node()[1]'
710             mode='rnd:emphasis'/>
711         </xsl:copy>
712       </xsl:when>
713       <xsl:when test='@rnd:style = "emphasis-underline"'>
714         <xsl:copy>
715           <xsl:attribute name='role'>underline</xsl:attribute>
716           <xsl:call-template name='rnd:attributes'/>
717           <xsl:apply-templates mode='rnd:copy'/>
718           <xsl:apply-templates select='following-sibling::node()[1]'
719             mode='rnd:emphasis'/>
720         </xsl:copy>
721       </xsl:when>
722
723       <xsl:when test='@rnd:style = "citetitle" or
724                       @rnd:style = "literal" or
725                       @rnd:style = "sgmltag"'>
726         <xsl:element name='{@rnd:style}'
727           namespace='http://docbook.org/ns/docbook'>
728           <xsl:call-template name='rnd:attributes'/>
729           <xsl:apply-templates/>
730           <xsl:apply-templates select='following-sibling::node()[1]'
731             mode='rnd:emphasis'/>
732         </xsl:element>
733       </xsl:when>
734
735       <xsl:when test='@rnd:style = "Hyperlink" and
736                       parent::dbk:link'>
737         <!-- This occurs in a hyperlink; parent should be dbk:link -->
738         <xsl:apply-templates/>
739       </xsl:when>
740       <xsl:when test='@rnd:style = "Hyperlink"'>
741         <!-- dbk:link is missing -->
742         <dbk:link xlink:href='{.}'>
743           <xsl:apply-templates/>
744         </dbk:link>
745       </xsl:when>
746
747       <!-- In rare circumstances, Word inserts an empty element that appears to be something like a space in the editor -->
748       <xsl:when test='(@rnd:style = "EndnoteReference" or
749                       @rnd:style = "FootnoteReference") and
750                       . = ""'>
751         <xsl:text> </xsl:text>
752       </xsl:when>
753
754       <xsl:otherwise>
755         <xsl:call-template name='rnd:error'>
756           <xsl:with-param name='code'>unknown-style</xsl:with-param>
757           <xsl:with-param name='message'>unknown character span style "<xsl:value-of select='@rnd:style'/>" encountered</xsl:with-param>
758         </xsl:call-template>
759       </xsl:otherwise>
760     </xsl:choose>
761   </xsl:template>
762
763   <!-- Coalesce emphasis elements into a single element -->
764   <xsl:template match='dbk:emphasis' mode='rnd:emphasis'>
765     <xsl:choose>
766       <xsl:when test='@rnd:style = preceding-sibling::node()[self::dbk:emphasis]/@rnd:style'>
767         <xsl:apply-templates/>
768         <xsl:apply-templates select='following-sibling::node()[1]'
769           mode='rnd:emphasis'/>
770       </xsl:when>
771     </xsl:choose>
772   </xsl:template>
773   <xsl:template match='*|text()' mode='rnd:emphasis'/>
774
775   <xsl:template match='dbk:emphasis' mode='rnd:personname-emphasis'>
776     <xsl:param name='style'/>
777
778     <xsl:choose>
779       <xsl:when test='@rnd:style = $style'>
780         <xsl:apply-templates/>
781         <xsl:apply-templates select='following-sibling::dbk:emphasis[1]'
782           mode='rnd:personname-emphasis'>
783           <xsl:with-param name='style' select='$style'/>
784         </xsl:apply-templates>
785       </xsl:when>
786     </xsl:choose>
787   </xsl:template>
788   <xsl:template match='*|text()' mode='rnd:emphasis'/>
789
790   <xsl:template match='dbk:subscript|dbk:superscript'>
791     <xsl:copy>
792       <xsl:apply-templates select='@*' mode='rnd:copy'/>
793       <xsl:apply-templates/>
794     </xsl:copy>
795   </xsl:template>
796
797   <!-- Images -->
798
799   <xsl:template match='dbk:inlinemediaobject'>
800     <xsl:choose>
801       <xsl:when test='not(preceding-sibling::*|following-sibling::*) and
802                       normalize-space(..) = ""'>
803
804         <xsl:variable name='next.captioned'
805                       select='ancestor::dbk:para/following-sibling::*[self::dbk:informaltable or self::dbk:para[dbk:inlinemediaobject and count(*) = 1 and normalize-space() = ""]][1]'/>
806
807         <xsl:variable name='caption'
808                       select='ancestor::dbk:para/following-sibling::dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"]'/>
809
810         <xsl:variable name='metadata'>
811           <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[1]'
812                                mode='rnd:metadata'/>
813         </xsl:variable>
814
815         <dbk:figure>
816           <xsl:if test='ancestor::dbk:para/preceding-sibling::*[1][self::dbk:para][@rnd:style = "figure-title"] or
817                         $metadata'>
818             <dbk:info>
819               <xsl:if test='ancestor::dbk:para/preceding-sibling::*[1][self::dbk:para][@rnd:style = "figure-title"]'>
820                 <dbk:title>
821                   <xsl:apply-templates select='ancestor::dbk:para/preceding-sibling::*[1]/node()'/>
822                 </dbk:title>
823               </xsl:if>
824               <xsl:copy-of select='$metadata'/>
825             </dbk:info>
826           </xsl:if>
827
828           <dbk:mediaobject>
829             <xsl:apply-templates mode='rnd:copy'/>
830           </dbk:mediaobject>
831
832           <xsl:choose>
833             <xsl:when test='not($caption)'/>
834             <xsl:when test='not($next.captioned)'>
835               <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[following-sibling::*[generate-id() = generate-id($caption)]][not(&metadata-content;)]'
836                                    mode='rnd:figure'/>
837               <xsl:apply-templates select='$caption'
838                                    mode='rnd:caption'/>
839             </xsl:when>
840             <!-- Does caption belong to this image or next.captioned?
841                - Only if it belongs to this image do we process it here.
842               -->
843             <xsl:when test='$next.captioned[preceding-sibling::*[generate-id() = generate-id($caption)]]'>
844               <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[following-sibling::*[generate-id() = generate-id($caption)]][not(&metadata-content;)]'
845                                    mode='rnd:figure'/>
846               <xsl:apply-templates select='$caption'
847                                    mode='rnd:caption'/>
848             </xsl:when>
849             <!-- otherwise caption does not belong to this figure -->
850           </xsl:choose>
851         </dbk:figure>
852       </xsl:when>
853       <xsl:otherwise>
854         <xsl:call-template name='rnd:copy'/>
855       </xsl:otherwise>
856     </xsl:choose>
857   </xsl:template>
858
859   <xsl:template match='dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"]'
860     mode='rnd:caption'>
861     <dbk:caption>
862       <dbk:para>
863         <xsl:apply-templates/>
864       </dbk:para>
865     </dbk:caption>
866   </xsl:template>
867   <xsl:template match='*' mode='rnd:caption'/>
868
869   <xsl:template match='*' mode='rnd:figure'>
870     <xsl:call-template name='rnd:para'>
871       <xsl:with-param name='suppress' select='false()'/>
872     </xsl:call-template>
873   </xsl:template>
874
875   <!-- Sidebars -->
876
877   <xsl:template match='*' mode='rnd:sidebar'>
878     <xsl:call-template name='rnd:para'>
879       <xsl:with-param name='suppress' select='false()'/>
880     </xsl:call-template>
881   </xsl:template>
882
883   <!-- Lists -->
884
885   <xsl:template match='dbk:para' mode='rnd:listitem'>
886     <dbk:listitem>
887       <dbk:para>
888         <xsl:call-template name='rnd:attributes'/>
889         <xsl:apply-templates/>
890       </dbk:para>
891       <xsl:apply-templates select='following-sibling::*[1]'
892         mode='rnd:continue'/>
893
894       <!-- Handle nested lists -->
895       <xsl:variable name='list-type'
896         select='concat(substring-before(@rnd:style, "list"), "list")'/>
897       <xsl:variable name='list-level'
898         select='substring-after(@rnd:style, $list-type)'/>
899
900       <!-- Assuming only five levels of list nesting.
901          - This is probably better done in a previous stage using grouping.
902         -->
903       <xsl:variable name='stop.node'
904         select='following-sibling::dbk:para[@rnd:style != concat("itemizedlist", $list-level + 1) and
905                 @rnd:style != concat("orderedlist", $list-level + 1) and
906                 @rnd:style != concat("itemizedlist", $list-level + 2) and
907                 @rnd:style != concat("orderedlist", $list-level + 2) and
908                 @rnd:style != concat("itemizedlist", $list-level + 3) and
909                 @rnd:style != concat("orderedlist", $list-level + 3) and
910                 @rnd:style != "para-continue"][1]'/>
911
912       <xsl:variable name='nested'
913         select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)][1]'/>
914
915       <xsl:choose>
916         <!-- Is there a nested list at all? -->
917         <xsl:when test='following-sibling::*[self::dbk:para and @rnd:style != "para-continue"][1][@rnd:style != concat("itemizedlist", $list-level + 1) and @rnd:style != concat("orderedlist", $list-level + 1)]'/>
918
919         <xsl:when test='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)] and
920                         $stop.node'>
921           <xsl:element name='{concat(substring-before($nested/@rnd:style, "list"), "list")}'
922             namespace='http://docbook.org/ns/docbook'>
923             <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)][following-sibling::*[generate-id() = generate-id($stop.node)]]'
924               mode='rnd:listitem'/>
925           </xsl:element>
926         </xsl:when>
927         <xsl:when test='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)]'>
928
929           <xsl:element name='{concat(substring-before($nested/@rnd:style, "list"), "list")}'
930             namespace='http://docbook.org/ns/docbook'>
931             <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)]'
932               mode='rnd:listitem'/>
933           </xsl:element>
934         </xsl:when>
935       </xsl:choose>
936     </dbk:listitem>
937   </xsl:template>
938
939   <!-- Blockquotes -->
940
941   <xsl:template match='dbk:para' mode='rnd:blockquote'>
942     <xsl:choose>
943       <xsl:when test='@rnd:style ="blockquote-attribution"'/>
944       <xsl:when test='@rnd:style ="blockquote-title"'/>
945       <xsl:otherwise>
946         <dbk:para>
947           <xsl:apply-templates/>
948         </dbk:para>
949       </xsl:otherwise>
950     </xsl:choose>
951   </xsl:template>
952   <xsl:template match='dbk:para' mode='rnd:blockquote-attribution'>
953     <xsl:if test='@rnd:style ="blockquote-attribution"'>
954       <dbk:attribution>
955         <xsl:apply-templates/>
956       </dbk:attribution>
957     </xsl:if>
958   </xsl:template>
959
960   <!-- Metadata -->
961
962   <xsl:template match='dbk:para' mode='rnd:metadata'>
963     <xsl:choose>
964       <xsl:when test='@rnd:style = "biblioentry-title" and
965                       parent::dbk:bibliography|parent::dbk:bibliodiv'/>
966       <xsl:when test='@rnd:style = "biblioentry-title"'>
967         <xsl:call-template name='rnd:error'>
968           <xsl:with-param name='code'>bad-metadata</xsl:with-param>
969           <xsl:with-param name='message'>style "<xsl:value-of select='@rnd:style'/>" must not be metadata for parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
970         </xsl:call-template>
971       </xsl:when>
972
973       <xsl:when test='@rnd:style = "abstract-title" or
974                       @rnd:style = "abstract"'>
975         <xsl:variable name='stop.node'
976           select='following-sibling::dbk:para[@rnd:style != "abstract"][1]'/>
977         <xsl:choose>
978           <xsl:when test='$stop.node'>
979             <dbk:abstract>
980               <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = "abstract"][following-sibling::*[generate-id() = generate-id($stop.node)]]'
981                 mode='rnd:abstract'/>
982             </dbk:abstract>
983             <xsl:apply-templates select='$stop.node'
984               mode='rnd:metadata'/>
985           </xsl:when>
986           <xsl:otherwise>
987             <dbk:abstract>
988               <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = "abstract"]' mode='rnd:abstract'/>
989             </dbk:abstract>
990           </xsl:otherwise>
991         </xsl:choose>
992       </xsl:when>
993
994       <xsl:when test='@rnd:style = "legalnotice"'>
995         <xsl:variable name='stop.node'
996           select='following-sibling::dbk:para[@rnd:style != "legalnotice"][1]'/>
997
998         <xsl:choose>
999           <xsl:when test='$stop.node'>
1000             <dbk:legalnotice>
1001               <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = "legalnotice"][following-sibling::*[generate-id() = generate-id($stop.node)]]'
1002                 mode='rnd:legalnotice'/>
1003             </dbk:legalnotice>
1004             <xsl:apply-templates select='$stop.node'
1005               mode='rnd:metadata'/>
1006           </xsl:when>
1007           <xsl:otherwise>
1008             <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = "legalnotice"]'
1009               mode='rnd:legalnotice'/>
1010           </xsl:otherwise>
1011         </xsl:choose>
1012       </xsl:when>
1013
1014       <xsl:when test='@rnd:style = "keyword"'>
1015         <xsl:variable name='stop.node'
1016           select='following-sibling::*[not(self::dbk:para) or
1017                   (self::dbk:para and @rnd:style != "keyword")][1]'/>
1018
1019         <xsl:choose>
1020           <xsl:when test='$stop.node'>
1021             <dbk:keywordset>
1022               <xsl:call-template name='rnd:keyword'>
1023                 <xsl:with-param name='nodes'
1024                   select='.|following-sibling::dbk:para[@rnd:style = "keyword"][following-sibling::*[generate-id() = generate-id($stop.node)]]'/>
1025               </xsl:call-template>
1026             </dbk:keywordset>
1027             <xsl:apply-templates select='$stop.node'
1028               mode='rnd:metadata'/>
1029           </xsl:when>
1030           <xsl:otherwise>
1031             <xsl:call-template name='rnd:keyword'>
1032               <xsl:with-param name='nodes'
1033                 select='.|following-sibling::dbk:para[@rnd:style = "keyword"]'/>
1034             </xsl:call-template>
1035           </xsl:otherwise>
1036         </xsl:choose>
1037       </xsl:when>
1038
1039       <xsl:when test='@rnd:style = "author"'>
1040         <dbk:author>
1041           <xsl:choose>
1042             <xsl:when test='dbk:emphasis[@rnd:style = "orgname"]'>
1043               <dbk:orgname>
1044                 <xsl:apply-templates
1045                   select='dbk:emphasis[@rnd:style = "orgname"]'
1046                   mode='rnd:orgname'/>
1047               </dbk:orgname>
1048               <xsl:if test='*[not(@rnd:style = "orgname")]'>
1049                 <xsl:call-template name='rnd:error'>
1050                   <xsl:with-param name='code'>bad-author-orgname-combo</xsl:with-param>
1051                   <xsl:with-param name='message'>character span "<xsl:value-of select='dbk:emphasis[@rnd:style != "orgname"][1]/@rnd:style'/>" not allowed in an author paragraph combined with orgname</xsl:with-param>
1052                 </xsl:call-template>
1053               </xsl:if>
1054             </xsl:when>
1055             <xsl:otherwise>
1056               <dbk:personname>
1057                 <!-- TODO: check style of author; mixed content or structured -->
1058                 <xsl:apply-templates mode='rnd:personname'/>
1059               </dbk:personname>
1060             </xsl:otherwise>
1061           </xsl:choose>
1062           <xsl:apply-templates mode='rnd:author-personblurb'/>
1063           <xsl:apply-templates select='following-sibling::*[1]'
1064             mode='rnd:author'/>
1065         </dbk:author>
1066         <xsl:call-template name='rnd:resume-metadata'>
1067           <xsl:with-param name='node' select='following-sibling::*[1]'/>
1068         </xsl:call-template>
1069       </xsl:when>
1070       <xsl:when test='@rnd:style = "personblurb" or
1071                       @rnd:style = "address" or
1072                       @rnd:style = "affiliation" or
1073                       @rnd:style = "contrib" or
1074                       @rnd:style = "email"'/>
1075
1076       <xsl:when test='@rnd:style = "releaseinfo" or
1077                       @rnd:style = "date" or
1078                       @rnd:style = "pubdate" or
1079                       @rnd:style = "pagenums" or
1080                       @rnd:style = "issuenum" or
1081                       @rnd:style = "volumenum" or
1082                       @rnd:style = "edition" or
1083                       @rnd:style = "editor" or
1084                       @rnd:style = "othercredit" or
1085                       @rnd:style = "biblioid" or
1086                       @rnd:style = "bibliosource" or
1087                       @rnd:style = "bibliomisc" or
1088                       @rnd:style = "revhistory" or
1089                       @rnd:style = "revision"'>
1090         <xsl:element name='{@rnd:style}'
1091           namespace='http://docbook.org/ns/docbook'>
1092           <xsl:apply-templates mode='rnd:metadata'/>
1093         </xsl:element>
1094         <xsl:apply-templates select='following-sibling::*[1]'
1095           mode='rnd:metadata'/>
1096       </xsl:when>
1097       <xsl:when test='contains(@rnd:style, "-titleabbrev")'>
1098         <xsl:variable name='parent'
1099           select='substring-before(@rnd:style, "-titleabbrev")'/>
1100
1101         <xsl:choose>
1102           <xsl:when test='$parent = local-name(..)'>
1103             <dbk:titleabbrev>
1104               <xsl:apply-templates mode='rnd:metadata'/>
1105             </dbk:titleabbrev>
1106           </xsl:when>
1107           <xsl:when test='(parent::dbk:article or parent::dbk:book-component) and
1108                           preceding-sibling::dbk:para[@rnd:style = concat($parent, "-title")]'>
1109             <dbk:titleabbrev>
1110               <xsl:apply-templates mode='rnd:metadata'/>
1111             </dbk:titleabbrev>
1112           </xsl:when>
1113           <xsl:otherwise>
1114             <xsl:call-template name='rnd:error'>
1115               <xsl:with-param name='code'>bad-titleabbrev</xsl:with-param>
1116               <xsl:with-param name='message'>titleabbrev style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
1117             </xsl:call-template>
1118           </xsl:otherwise>
1119         </xsl:choose>
1120
1121         <xsl:apply-templates select='following-sibling::*[1]'
1122           mode='rnd:metadata'/>
1123       </xsl:when>
1124       <xsl:when test='contains(@rnd:style, "-title")'>
1125         <xsl:variable name='parent'
1126           select='substring-before(@rnd:style, "-title")'/>
1127
1128         <xsl:choose>
1129           <xsl:when test='$parent = "table" or
1130                           $parent = "figure"'>
1131             <dbk:title>
1132               <xsl:apply-templates mode='rnd:metadata'/>
1133             </dbk:title>
1134           </xsl:when>
1135           <xsl:when test='$parent = local-name(..)'>
1136             <dbk:title>
1137               <xsl:apply-templates mode='rnd:metadata'/>
1138             </dbk:title>
1139           </xsl:when>
1140           <xsl:when test='parent::dbk:book-component'>
1141             <dbk:title>
1142               <xsl:apply-templates mode='rnd:metadata'/>
1143             </dbk:title>
1144           </xsl:when>
1145           <xsl:when test='parent::dbk:article and
1146                           not(../../..)'>
1147             <dbk:title>
1148               <xsl:apply-templates mode='rnd:metadata'/>
1149             </dbk:title>
1150           </xsl:when>
1151           <xsl:otherwise>
1152             <xsl:call-template name='rnd:error'>
1153               <xsl:with-param name='code'>bad-title</xsl:with-param>
1154               <xsl:with-param name='message'>title style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
1155             </xsl:call-template>
1156           </xsl:otherwise>
1157         </xsl:choose>
1158
1159         <xsl:apply-templates select='following-sibling::*[1]'
1160           mode='rnd:metadata'/>
1161       </xsl:when>
1162
1163       <!-- Exception to normal subtitle handling is biblioentry-subtitle -->
1164       <xsl:when test='@rnd:style = "biblioentry-subtitle"'>
1165         <!-- TODO: check that this is in a biblioentry -->
1166         <dbk:subtitle>
1167           <xsl:apply-templates mode='rnd:metadata'/>
1168         </dbk:subtitle>
1169
1170         <xsl:apply-templates select='following-sibling::*[1]'
1171           mode='rnd:metadata'/>
1172       </xsl:when>
1173       <xsl:when test='contains(@rnd:style, "-subtitle")'>
1174         <xsl:variable name='parent'
1175           select='substring-before(@rnd:style, "-subtitle")'/>
1176
1177         <xsl:choose>
1178           <xsl:when test='$parent = local-name(..)'>
1179             <dbk:subtitle>
1180               <xsl:apply-templates mode='rnd:metadata'/>
1181             </dbk:subtitle>
1182           </xsl:when>
1183           <xsl:when test='(parent::dbk:article or parent::dbk:book-component) and
1184                           preceding-sibling::dbk:para[@rnd:style = concat($parent, "-title")]'>
1185             <dbk:subtitle>
1186               <xsl:apply-templates mode='rnd:metadata'/>
1187             </dbk:subtitle>
1188           </xsl:when>
1189           <xsl:otherwise>
1190             <xsl:call-template name='rnd:error'>
1191               <xsl:with-param name='code'>bad-subtitle</xsl:with-param>
1192               <xsl:with-param name='message'>subtitle style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
1193             </xsl:call-template>
1194           </xsl:otherwise>
1195         </xsl:choose>
1196
1197         <xsl:apply-templates select='following-sibling::*[1]'
1198           mode='rnd:metadata'/>
1199       </xsl:when>
1200
1201       <xsl:when test='@rnd:style = "publisher-address" and
1202                       preceding-sibling::*[1][not(self::dbk:para) or not(@rnd:style = "publisher")]'>
1203         <xsl:call-template name='rnd:error'>
1204           <xsl:with-param name='code'>bad-publisher-address</xsl:with-param>
1205           <xsl:with-param name='message'>publisher-address must follow publisher</xsl:with-param>
1206         </xsl:call-template>
1207         <xsl:apply-templates select='following-sibling::*[1]'
1208           mode='rnd:metadata'/>
1209       </xsl:when>
1210       <xsl:when test='@rnd:style = "publisher-address"'>
1211         <xsl:apply-templates select='following-sibling::*[1]'
1212           mode='rnd:metadata'/>
1213       </xsl:when>
1214       <xsl:when test='@rnd:style = "publisher"'>
1215         <dbk:publisher>
1216           <dbk:publishername>
1217             <xsl:apply-templates/>
1218           </dbk:publishername>
1219           <xsl:if test='following-sibling::*[1][@rnd:style = "publisher-address"]'>
1220             <xsl:apply-templates select='following-sibling::*[1]'
1221               mode='rnd:publisher'/>
1222           </xsl:if>
1223         </dbk:publisher>
1224
1225         <xsl:apply-templates select='following-sibling::*[1]'
1226           mode='rnd:metadata'/>
1227       </xsl:when>
1228     </xsl:choose>
1229   </xsl:template>
1230
1231   <xsl:template name='rnd:keyword'>
1232     <xsl:param name='nodes' select='/..'/>
1233
1234     <xsl:choose>
1235       <xsl:when test='not($nodes)'/>
1236       <xsl:otherwise>
1237         <xsl:call-template name='rnd:keyword-phrases'>
1238           <xsl:with-param name='text' select='$nodes[1]'/>
1239         </xsl:call-template>
1240         <xsl:call-template name='rnd:keyword'>
1241           <xsl:with-param name='nodes' select='$nodes[position() != 1]'/>
1242         </xsl:call-template>
1243       </xsl:otherwise>
1244     </xsl:choose>
1245   </xsl:template>
1246   <xsl:template name='rnd:keyword-phrases'>
1247     <xsl:param name='text'/>
1248
1249     <xsl:choose>
1250       <xsl:when test='not($text)'/>
1251       <xsl:when test='contains($text, ",")'>
1252         <dbk:keyword>
1253           <xsl:value-of select='normalize-space(substring-before($text, ","))'/>
1254         </dbk:keyword>
1255         <xsl:call-template name='rnd:keyword-phrases'>
1256           <xsl:with-param name='text' select='substring-after($text, ",")'/>
1257         </xsl:call-template>
1258       </xsl:when>
1259       <xsl:otherwise>
1260         <dbk:keyword>
1261           <xsl:value-of select='normalize-space($text)'/>
1262         </dbk:keyword>
1263       </xsl:otherwise>
1264     </xsl:choose>
1265   </xsl:template>
1266
1267   <xsl:template match='dbk:emphasis' mode='rnd:metadata'>
1268     <xsl:choose>
1269       <xsl:when test='not(@rnd:style)'>
1270         <xsl:copy>
1271           <xsl:apply-templates mode='rnd:metadata'/>
1272         </xsl:copy>
1273       </xsl:when>
1274       <xsl:when test='@rnd:style = "Hyperlink" and
1275                       parent::dbk:link'>
1276         <xsl:apply-templates mode='rnd:metadata'/>
1277       </xsl:when>
1278       <xsl:when test='@rnd:style = "Hyperlink"'>
1279         <dbk:link xlink:href='{.}'>
1280           <xsl:apply-templates mode='rnd:metadata'/>
1281         </dbk:link>
1282       </xsl:when>
1283       <xsl:otherwise>
1284         <xsl:element name='{@rnd:style}'
1285           namespace='http://docbook.org/ns/docbook'>
1286           <xsl:apply-templates mode='rnd:metadata'/>
1287         </xsl:element>
1288       </xsl:otherwise>
1289     </xsl:choose>
1290   </xsl:template>
1291   <xsl:template match='dbk:link' mode='rnd:metadata'>
1292     <xsl:copy>
1293       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1294       <xsl:apply-templates mode='rnd:metadata'/>
1295     </xsl:copy>
1296   </xsl:template>
1297   <xsl:template match='dbk:inlinemediaobject' mode='rnd:metadata'>
1298     <xsl:call-template name='rnd:copy'/>
1299   </xsl:template>
1300   <xsl:template match='*' mode='rnd:metadata'/>
1301
1302   <xsl:template name='rnd:resume-metadata'>
1303     <xsl:param name='node' select='/..'/>
1304
1305     <xsl:choose>
1306       <xsl:when test='$node[self::dbk:para][&author-content;]'>
1307         <xsl:call-template name='rnd:resume-metadata'>
1308           <xsl:with-param name='node' select='$node/following-sibling::*[1]'/>
1309         </xsl:call-template>
1310       </xsl:when>
1311       <xsl:when test='$node[self::dbk:para][&metadata-content;]'>
1312         <xsl:apply-templates select='$node' mode='rnd:metadata'/>
1313       </xsl:when>
1314     </xsl:choose>
1315   </xsl:template>
1316
1317   <xsl:template match='dbk:para' mode='rnd:abstract'>
1318     <xsl:choose>
1319       <xsl:when test='@rnd:style = "abstract-title"'>
1320         <dbk:title>
1321           <xsl:call-template name='rnd:attributes'/>
1322           <xsl:apply-templates/>
1323         </dbk:title>
1324       </xsl:when>
1325       <xsl:otherwise>
1326         <dbk:para>
1327           <xsl:call-template name='rnd:attributes'/>
1328           <xsl:apply-templates/>
1329         </dbk:para>
1330       </xsl:otherwise>
1331     </xsl:choose>
1332   </xsl:template>
1333
1334   <xsl:template match='dbk:para' mode='rnd:legalnotice'>
1335     <dbk:para>
1336       <xsl:call-template name='rnd:attributes'/>
1337       <xsl:apply-templates/>
1338     </dbk:para>
1339   </xsl:template>
1340
1341   <xsl:template match='dbk:footnote' mode='rnd:personname'/>
1342   <xsl:template match='dbk:emphasis' mode='rnd:personname'>
1343     <!-- Need to check preceding emphasis for same style,
1344          but blocks pretty-prints and all text nodes
1345          are preserved in paragraph content.
1346       -->
1347     <xsl:variable name='previous'
1348       select='preceding-sibling::node()[not(self::text()) or (self::text() and normalize-space() != "")]'/>
1349     <!--
1350     <xsl:comment> this style is <xsl:value-of select='@rnd:style'/>, previous style is <xsl:value-of select='$previous[last()]/@rnd:style'/> # previous <xsl:value-of select='count($previous)'/></xsl:comment>
1351 -->
1352     <xsl:choose>
1353       <!-- inlines are coalesced -->
1354       <xsl:when test='@rnd:style = $previous[last()][self::dbk:emphasis]/@rnd:style'/>
1355       <xsl:when test='@rnd:style = "honorific" or
1356                       @rnd:style = "firstname" or
1357                       @rnd:style = "lineage" or
1358                       @rnd:style = "othername" or
1359                       @rnd:style = "surname"'>
1360         <xsl:element name='{@rnd:style}'
1361           namespace='http://docbook.org/ns/docbook'>
1362           <xsl:apply-templates/>
1363           <xsl:apply-templates select='following-sibling::dbk:emphasis[1]'
1364             mode='rnd:personname-emphasis'>
1365             <xsl:with-param name='style' select='@rnd:style'/>
1366           </xsl:apply-templates>
1367         </xsl:element>
1368       </xsl:when>
1369       <xsl:otherwise>
1370         <xsl:call-template name='rnd:error'>
1371           <xsl:with-param name='code'>bad-author-inline</xsl:with-param>
1372           <xsl:with-param name='message'>character span "<xsl:value-of select='@rnd:style'/>" not allowed in an author paragraph</xsl:with-param>
1373         </xsl:call-template>
1374       </xsl:otherwise>
1375     </xsl:choose>
1376   </xsl:template>
1377
1378   <xsl:template match='text()' mode='rnd:author-personblurb'/>
1379   <xsl:template match='dbk:emphasis' mode='rnd:author-personblurb'/>
1380   <xsl:template match='dbk:footnote' mode='rnd:author-personblurb'>
1381     <dbk:personblurb>
1382       <dbk:para>
1383         <xsl:copy>
1384           <xsl:apply-templates select='@*' mode='rnd:copy'/>
1385           <xsl:apply-templates/>
1386         </xsl:copy>
1387       </dbk:para>
1388     </dbk:personblurb>
1389   </xsl:template>
1390
1391   <xsl:template match='dbk:para' mode='rnd:author'>
1392     <xsl:choose>
1393       <xsl:when test='@rnd:style = "personblurb" and
1394                       preceding-sibling::*[1][self::dbk:para and @rnd:style != "personblurb"]'>
1395         <dbk:personblurb>
1396           <xsl:apply-templates select='.'
1397             mode='rnd:personblurb'/>
1398         </dbk:personblurb>
1399       </xsl:when>
1400       <xsl:when test='@rnd:style = "personblurb"'>
1401         <xsl:apply-templates select='following-sibling::*[1]'
1402           mode='rnd:author'/>
1403       </xsl:when>
1404
1405       <!-- Web and mail addresses may appear in a simplified form -->
1406       <xsl:when test='@rnd:style = "address"'>
1407         <xsl:choose>
1408           <xsl:when test='dbk:link and
1409                           count(dbk:link) = count(*)'>
1410             <!-- simplified form -->
1411             <dbk:otheraddr>
1412               <xsl:apply-templates select='dbk:link'
1413                 mode='rnd:otheraddr'/>
1414             </dbk:otheraddr>
1415           </xsl:when>
1416           <xsl:otherwise>
1417             <dbk:address>
1418               <xsl:apply-templates mode='rnd:author'/>
1419             </dbk:address>
1420             <xsl:apply-templates select='following-sibling::*[1]'
1421               mode='rnd:author'/>
1422           </xsl:otherwise>
1423         </xsl:choose>
1424       </xsl:when>
1425
1426       <xsl:when test='@rnd:style = "affiliation"'>
1427         <dbk:affiliation>
1428           <xsl:choose>
1429             <xsl:when test='not(*)'>
1430               <dbk:jobtitle>
1431                 <xsl:apply-templates mode='rnd:author'/>
1432               </dbk:jobtitle>
1433             </xsl:when>
1434             <xsl:otherwise>
1435               <xsl:apply-templates mode='rnd:author'/>
1436             </xsl:otherwise>
1437           </xsl:choose>
1438         </dbk:affiliation>
1439         <xsl:apply-templates select='following-sibling::*[1]'
1440                              mode='rnd:author'/>
1441       </xsl:when>
1442       <xsl:when test='@rnd:style = "contrib" or
1443                       @rnd:style = "email"'>
1444         <xsl:element name='{@rnd:style}'
1445           namespace='http://docbook.org/ns/docbook'>
1446           <xsl:apply-templates mode='rnd:author'/>
1447         </xsl:element>
1448         <xsl:apply-templates select='following-sibling::*[1]'
1449           mode='rnd:author'/>
1450       </xsl:when>
1451     </xsl:choose>
1452   </xsl:template>
1453
1454   <xsl:template match='dbk:link' mode='rnd:otheraddr'>
1455     <xsl:copy>
1456       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1457       <xsl:apply-templates mode='rnd:otheraddr'/>
1458     </xsl:copy>
1459   </xsl:template>
1460
1461   <!-- TODO: not all of these inlines are allowed in all elements that are children of author.
1462        Need to further refine validation of inlines.
1463     -->
1464   <xsl:template match='dbk:emphasis' mode='rnd:author'>
1465     <xsl:choose>
1466       <xsl:when test='@rnd:style = "city" or
1467                       @rnd:style = "country" or
1468                       @rnd:style = "email" or
1469                       @rnd:style = "fax" or
1470                       @rnd:style = "jobtitle" or
1471                       @rnd:style = "orgdiv" or
1472                       @rnd:style = "orgname" or
1473                       @rnd:style = "otheraddr" or
1474                       @rnd:style = "phone" or
1475                       @rnd:style = "pob" or
1476                       @rnd:style = "postcode" or
1477                       @rnd:style = "shortaffil" or
1478                       @rnd:style = "state" or
1479                       @rnd:style = "street"'>
1480         <xsl:element name='{@rnd:style}'
1481           namespace='http://docbook.org/ns/docbook'>
1482           <xsl:apply-templates/>
1483         </xsl:element>
1484       </xsl:when>
1485       <xsl:otherwise>
1486         <xsl:call-template name='rnd:error'>
1487           <xsl:with-param name='code'>metadata-bad-inline</xsl:with-param>
1488           <xsl:with-param name='message'>character span "<xsl:value-of select='@rnd:style'/>" not allowed in author metadata</xsl:with-param>
1489         </xsl:call-template>
1490       </xsl:otherwise>
1491     </xsl:choose>
1492   </xsl:template>
1493
1494   <xsl:template match='dbk:para' mode='rnd:personblurb'>
1495     <xsl:if test='@rnd:style = "personblurb"'>
1496       <dbk:para>
1497         <xsl:apply-templates/>
1498       </dbk:para>
1499       <xsl:apply-templates select='following-sibling::*[1]'
1500         mode='rnd:personblurb'/>
1501     </xsl:if>
1502   </xsl:template>
1503
1504   <xsl:template match='dbk:para' mode='rnd:publisher'>
1505     <xsl:if test='@rnd:style = "publisher-address"'>
1506       <dbk:address>
1507         <xsl:apply-templates/>
1508       </dbk:address>
1509     </xsl:if>
1510   </xsl:template>
1511
1512   <xsl:template match='dbk:para' mode='rnd:programlisting'>
1513     <xsl:text>&#xa;</xsl:text>
1514     <xsl:apply-templates/>
1515   </xsl:template>
1516
1517   <!-- Continuing paragraphs -->
1518
1519   <xsl:template match='*' mode='rnd:continue'/>
1520   <xsl:template match='dbk:para' mode='rnd:continue'>
1521     <xsl:if test='@rnd:style = "para-continue"'>
1522       <dbk:para>
1523         <xsl:call-template name='rnd:attributes'/>
1524         <xsl:apply-templates/>
1525       </dbk:para>
1526       <xsl:apply-templates select='following-sibling::*[1]'
1527         mode='rnd:continue'/>
1528     </xsl:if>
1529   </xsl:template>
1530   <xsl:template match='dbk:informaltable' mode='rnd:continue'>
1531     <xsl:apply-templates select='.'>
1532       <xsl:with-param name='in-list' select='true()'/>
1533     </xsl:apply-templates>
1534     <xsl:apply-templates select='following-sibling::*[1]'
1535       mode='rnd:continue'/>
1536   </xsl:template>
1537
1538   <!-- Tables -->
1539
1540   <xsl:template match='dbk:informaltable'>
1541     <xsl:param name='in-list' select='false()'/>
1542
1543     <xsl:choose>
1544       <xsl:when test='not($in-list) and
1545                       preceding-sibling::dbk:para[1][starts-with(@rnd:style, "itemizedlist") or starts-with(@rnd:style, "orderedlist") or @rnd:style = "para-continue"]'/>
1546       <xsl:when test='preceding-sibling::*[1][self::dbk:para][@rnd:style ="table-title"]'>
1547         <dbk:table>
1548           <xsl:apply-templates select='@*' mode='rnd:copy'/>
1549
1550           <dbk:info>
1551             <xsl:apply-templates select='preceding-sibling::dbk:para[1]'
1552                                  mode='rnd:table-title'/>
1553           </dbk:info>
1554
1555           <xsl:call-template name='rnd:table-textobject'/>
1556
1557           <xsl:apply-templates/>
1558
1559           <xsl:call-template name='rnd:table-caption'/>
1560         </dbk:table>
1561       </xsl:when>
1562       <xsl:otherwise>
1563         <xsl:copy>
1564           <xsl:apply-templates select='@*' mode='rnd:copy'/>
1565
1566           <xsl:call-template name='rnd:table-textobject'/>
1567
1568           <xsl:apply-templates/>
1569
1570           <xsl:call-template name='rnd:table-caption'/>
1571         </xsl:copy>
1572       </xsl:otherwise>
1573     </xsl:choose>
1574   </xsl:template>
1575   <xsl:template match='dbk:tgroup'>
1576     <xsl:copy>
1577       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1578       <xsl:if test='not(@cols)'>
1579         <xsl:attribute name='cols'>
1580           <xsl:call-template name='dbk:max-columns'>
1581             <xsl:with-param name='rows' select='*/dbk:row'/>
1582           </xsl:call-template>
1583         </xsl:attribute>
1584       </xsl:if>
1585       <xsl:apply-templates/>
1586     </xsl:copy>
1587   </xsl:template>
1588   <xsl:template name='dbk:max-columns'>
1589     <xsl:param name='rows' select='/..'/>
1590     <xsl:param name='max' select='0'/>
1591
1592     <!-- This is a tail-recursive algorithm.
1593          Could improve this with other algorithm(s),
1594          eg. divide-and-conquer.
1595       -->
1596
1597     <xsl:choose>
1598       <xsl:when test='not($rows)'>
1599         <xsl:value-of select='$max'/>
1600       </xsl:when>
1601       <xsl:when test='count($rows[1]/dbk:entry) > $max'>
1602         <xsl:call-template name='dbk:max-columns'>
1603           <xsl:with-param name='rows' select='$rows[position() != 1]'/>
1604           <xsl:with-param name='max' select='count($rows[1]/dbk:entry)'/>
1605         </xsl:call-template>
1606       </xsl:when>
1607       <xsl:otherwise>
1608         <xsl:call-template name='dbk:max-columns'>
1609           <xsl:with-param name='rows' select='$rows[position() != 1]'/>
1610           <xsl:with-param name='max' select='$max'/>
1611         </xsl:call-template>
1612       </xsl:otherwise>
1613     </xsl:choose>
1614   </xsl:template>
1615   <xsl:template match='dbk:tbody|dbk:thead|dbk:tfoot|dbk:row|dbk:colspec'>
1616     <xsl:copy>
1617       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1618       <xsl:apply-templates/>
1619     </xsl:copy>
1620   </xsl:template>
1621   <xsl:template match='dbk:entry'>
1622     <dbk:entry>
1623       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1624       <xsl:apply-templates/>
1625     </dbk:entry>
1626   </xsl:template>
1627
1628   <xsl:template match='dbk:para' mode='rnd:table-title'>
1629     <dbk:title>
1630       <xsl:apply-templates/>
1631     </dbk:title>
1632   </xsl:template>
1633
1634   <!-- Find the caption associated with this table -->
1635   <xsl:template name='rnd:table-caption'>
1636     <xsl:variable name='candidate'
1637                   select='following-sibling::dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"][1]'/>
1638
1639     <xsl:if test='$candidate != "" and
1640                   generate-id($candidate/preceding-sibling::dbk:informaltable[1]) = generate-id(.)'>
1641       <dbk:caption>
1642         <dbk:para>
1643           <xsl:apply-templates select='$candidate/node()'/>
1644         </dbk:para>
1645       </dbk:caption>
1646     </xsl:if>
1647   </xsl:template>
1648
1649   <!-- Find table associated text -->
1650   <xsl:template name='rnd:table-textobject'>
1651     <xsl:variable name='caption'
1652                   select='following-sibling::dbk:para[@rnd:style = "caption" or @rnd:style = "Caption"][1]'/>
1653
1654     <xsl:if test='generate-id($caption/preceding-sibling::dbk:informaltable[1]) = generate-id(.)'>
1655       <xsl:variable name='content'
1656                     select='following-sibling::*[following-sibling::*[generate-id($caption) = generate-id()]]'/>
1657       <xsl:if test='$content'>
1658         <dbk:textobject>
1659           <xsl:apply-templates select='$content' mode='rnd:textobject'/>
1660         </dbk:textobject>
1661       </xsl:if>
1662     </xsl:if>
1663   </xsl:template>
1664   <xsl:template match='dbk:para' mode='rnd:textobject'>
1665     <xsl:call-template name='rnd:para'>
1666       <xsl:with-param name='suppress' select='false()'/>
1667     </xsl:call-template>
1668   </xsl:template>
1669
1670   <!-- Footnotes -->
1671   <xsl:template match='dbk:footnote'>
1672     <xsl:copy>
1673       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1674       <xsl:apply-templates/>
1675     </xsl:copy>
1676   </xsl:template>
1677
1678   <!-- utilities -->
1679
1680   <!-- rnd:attributes reconstitutes an element's attributes -->
1681   <xsl:template name='rnd:attributes'>
1682     <xsl:param name='node' select='.'/>
1683
1684     <xsl:apply-templates select='$node/@*[namespace-uri() != "http://docbook.org/ns/docbook/roundtrip"]' mode='rnd:copy'/>
1685   </xsl:template>
1686
1687   <xsl:template match='*' name='rnd:copy' mode='rnd:copy'>
1688     <xsl:copy>
1689       <xsl:apply-templates select='@*' mode='rnd:copy'/>
1690       <xsl:apply-templates mode='rnd:copy'/>
1691     </xsl:copy>
1692   </xsl:template>
1693   <xsl:template match='@*' mode='rnd:copy'>
1694     <xsl:copy/>
1695   </xsl:template>
1696
1697   <!-- These templates are invoked whenever an error condition is detected in the conversion of a document.
1698     -->
1699   <xsl:template name='rnd:error'>
1700     <xsl:param name='node' select='.'/>
1701     <xsl:param name='code'/>
1702     <xsl:param name='message'/>
1703
1704     <xsl:comment><xsl:value-of select='$message'/></xsl:comment>
1705     <xsl:message>ERROR "<xsl:value-of select='$code'/>": <xsl:value-of select='$message'/></xsl:message>
1706     <rnd:error>
1707       <rnd:code>
1708         <xsl:value-of select='$code'/>
1709       </rnd:code>
1710       <rnd:message>
1711         <xsl:value-of select='$message'/>
1712       </rnd:message>
1713     </rnd:error>
1714   </xsl:template>
1715   <xsl:template name='rnd:warning'>
1716     <xsl:param name='node' select='.'/>
1717     <xsl:param name='code'/>
1718     <xsl:param name='message'/>
1719
1720     <xsl:comment><xsl:value-of select='$message'/></xsl:comment>
1721     <xsl:message>WARNING "<xsl:value-of select='$code'/>": <xsl:value-of select='$message'/></xsl:message>
1722     <rnd:warning>
1723       <rnd:code>
1724         <xsl:value-of select='$code'/>
1725       </rnd:code>
1726       <rnd:message>
1727         <xsl:value-of select='$message'/>
1728       </rnd:message>
1729     </rnd:warning>
1730   </xsl:template>
1731
1732 </xsl:stylesheet>