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