]> git.stg.codes - stg.git/blob - doc/xslt/epub/docbook.xsl
Set output encoding to utf-8.
[stg.git] / doc / xslt / epub / docbook.xsl
1 <?xml version="1.0"?>
2 <xsl:stylesheet 
3   xmlns:db="http://docbook.org/ns/docbook"
4   xmlns:dc="http://purl.org/dc/elements/1.1/"  
5   xmlns:exsl="http://exslt.org/common" 
6   xmlns:h="http://www.w3.org/1999/xhtml"
7   xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/"
8   xmlns:ng="http://docbook.org/docbook-ng"
9   xmlns:opf="http://www.idpf.org/2007/opf"
10   xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
11   xmlns:str="http://exslt.org/strings"
12   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
13   xmlns:xtext="xalan://com.nwalsh.xalan.Text"
14
15   extension-element-prefixes="stext xtext"
16   exclude-result-prefixes="exsl db dc h ncx ng opf stext str xtext"
17
18   version="1.0">
19
20   <xsl:import href="../xhtml-1_1/docbook.xsl" />
21   <xsl:import href="../xhtml-1_1/chunk-common.xsl" />
22   <xsl:include href="../xhtml-1_1/chunk-code.xsl" />
23
24
25   <!-- We want a separate TOC file, please -->
26   <xsl:param name="chunk.tocs.and.lots">1</xsl:param>
27   <xsl:param name="toc.section.depth">2</xsl:param>
28   <xsl:param name="generate.toc">
29   book   toc,title
30   </xsl:param>
31
32   <xsl:param name="ade.extensions" select="0"/>
33   <xsl:param name="epub.autolabel" select="'1'"/> 
34   <xsl:param name="epub.ncx.depth">4</xsl:param> <!-- Not functional until http://code.google.com/p/epubcheck/issues/detail?id=70 is resolved -->
35
36
37   <xsl:param name="manifest.in.base.dir" select="'1'"/> 
38   <xsl:param name="base.dir" select="$epub.oebps.dir"/>
39
40   <xsl:param name="epub.oebps.dir" select="'OEBPS/'"/> 
41   <xsl:param name="epub.ncx.filename" select="'toc.ncx'"/> 
42   <xsl:param name="epub.container.filename" select="'container.xml'"/> 
43   <xsl:param name="epub.opf.filename" select="concat($epub.oebps.dir, 'content.opf')"/> 
44   <xsl:param name="epub.cover.filename" select="concat($epub.oebps.dir, 'cover', $html.ext)"/> 
45   <xsl:param name="epub.cover.id" select="'cover'"/> 
46   <xsl:param name="epub.cover.html" select="'cover.html'" />
47   <xsl:param name="epub.cover.image.id" select="'cover-image'"/> 
48   <xsl:param name="epub.cover.linear" select="0" />
49   <xsl:param name="epub.ncx.toc.id">ncxtoc</xsl:param>
50   <xsl:param name="epub.html.toc.id">htmltoc</xsl:param>
51   <xsl:param name="epub.metainf.dir" select="'META-INF/'"/> 
52
53   <xsl:param name="epub.embedded.fonts"></xsl:param>
54
55   <!-- Turning this on crashes ADE, which is unbelievably awesome -->
56   <xsl:param name="formal.object.break.after">0</xsl:param>
57
58
59   <!-- Per Bob Stayton:
60        """Process your documents with the css.decoration parameter set to zero. 
61           That will avoid the use of style attributes in XHTML elements where they are not permitted."""
62        http://www.sagehill.net/docbookxsl/OtherOutputForms.html#StrictXhtmlValid -->
63   <xsl:param name="css.decoration" select="0"/>
64   <xsl:param name="custom.css.source"></xsl:param> <!-- FIXME: Align with current CSS parameter design -->
65
66   <xsl:param name="callout.graphics" select="1"/>
67   <xsl:param name="callout.graphics.extension">.png</xsl:param>
68   <xsl:param name="callout.graphics.number.limit" select="15"/>
69   <xsl:param name="callout.graphics.path" select="'images/callouts/'"/>
70
71   <!-- no navigation in .epub -->
72   <xsl:param name="suppress.navigation" select="'1'"/> 
73
74   <xsl:variable name="toc.params">
75     <xsl:call-template name="find.path.params">
76       <xsl:with-param name="node" select="/*"/>
77       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
78     </xsl:call-template>
79   </xsl:variable>
80   <xsl:variable name="root.is.a.chunk">
81     <xsl:choose>
82       <xsl:when test="/*[not(self::book)][not(sect1) or not(section)]">
83         <xsl:text>1</xsl:text>
84       </xsl:when>
85       <xsl:when test="/book[*[last()][self::bookinfo]]|book[bookinfo]">
86         <xsl:text>1</xsl:text>
87       </xsl:when>
88       <xsl:when test="/book[*[last()][self::info]]|book[info]">
89         <xsl:text>1</xsl:text>
90       </xsl:when>
91       <xsl:when test="/bibliography">
92         <xsl:text>1</xsl:text>
93       </xsl:when>
94       <xsl:otherwise>
95         <xsl:text>0</xsl:text>
96       </xsl:otherwise>
97     </xsl:choose>
98   </xsl:variable>
99
100   <xsl:key name="image-filerefs" match="graphic|inlinegraphic|imagedata" use="@fileref"/>
101
102   <xsl:template match="/">
103     <!-- * Get a title for current doc so that we let the user -->
104     <!-- * know what document we are processing at this point. -->
105     <xsl:variable name="doc.title">
106       <xsl:call-template name="get.doc.title" />
107     </xsl:variable>
108     <xsl:choose>
109       <!-- Hack! If someone hands us a DocBook V5.x or DocBook NG document,
110         toss the namespace and continue.  Use the docbook5 namespaced
111         stylesheets for DocBook5 if you don't want to use this feature.-->
112       <!-- include extra test for Xalan quirk -->
113       <xsl:when test="$exsl.node.set.available != 0
114                     and (*/self::ng:* or */self::db:*)">
115         <xsl:call-template name="log.message">
116           <xsl:with-param name="level">Note</xsl:with-param>
117           <xsl:with-param name="source" select="$doc.title" />
118           <xsl:with-param name="context-desc">
119             <xsl:text>namesp. cut</xsl:text>
120           </xsl:with-param>
121           <xsl:with-param name="message">
122             <xsl:text>stripped namespace before processing</xsl:text>
123           </xsl:with-param>
124         </xsl:call-template>
125         <xsl:variable name="nons">
126           <xsl:apply-templates mode="stripNS" />
127         </xsl:variable>
128         <xsl:call-template name="log.message">
129           <xsl:with-param name="level">Note</xsl:with-param>
130           <xsl:with-param name="source" select="$doc.title" />
131           <xsl:with-param name="context-desc">
132             <xsl:text>namesp. cut</xsl:text>
133           </xsl:with-param>
134           <xsl:with-param name="message">
135             <xsl:text>processing stripped document</xsl:text>
136           </xsl:with-param>
137         </xsl:call-template>
138         <xsl:apply-templates select="exsl:node-set($nons)" />
139       </xsl:when>
140       <xsl:otherwise>
141         <xsl:choose>
142           <xsl:when test="$rootid != ''">
143             <xsl:choose>
144               <xsl:when
145                 test="count(key('id',$rootid)) = 0">
146                 <xsl:message terminate="yes">
147                   <xsl:text>ID '</xsl:text>
148                   <xsl:value-of select="$rootid" />
149                   <xsl:text>' not found in document.</xsl:text>
150                 </xsl:message>
151               </xsl:when>
152               <xsl:otherwise>
153                 <xsl:if
154                   test="$collect.xref.targets = 'yes' or
155                                 $collect.xref.targets = 'only'">
156                   <xsl:apply-templates
157                     select="key('id', $rootid)" mode="collect.targets" />
158                 </xsl:if>
159                 <xsl:if
160                   test="$collect.xref.targets != 'only'">
161                   <xsl:message>
162                     Formatting from
163                     <xsl:value-of select="$rootid" />
164                   </xsl:message>
165                   <xsl:apply-templates
166                     select="key('id',$rootid)" mode="process.root" />
167                   <xsl:call-template name="ncx" />
168                 </xsl:if>
169               </xsl:otherwise>
170             </xsl:choose>
171           </xsl:when>
172           <xsl:otherwise>
173             <xsl:if
174               test="$collect.xref.targets = 'yes' or
175                     $collect.xref.targets = 'only'">
176               <xsl:apply-templates select="/"
177                 mode="collect.targets" />
178             </xsl:if>
179             <xsl:if
180               test="$collect.xref.targets != 'only'">
181               <xsl:apply-templates select="/"
182                 mode="process.root" />
183               <xsl:call-template name="ncx" />
184               <xsl:call-template name="opf" />
185               <xsl:call-template name="cover" />
186               <xsl:call-template name="container" />
187             </xsl:if>
188           </xsl:otherwise>
189         </xsl:choose>
190       </xsl:otherwise>
191     </xsl:choose>
192   </xsl:template>
193
194   <xsl:template name="package-identifier">  
195     <xsl:choose>
196       <xsl:when test="/*/*[contains(name(.), 'info')]/biblioid">
197         <xsl:if test="/*/*[contains(name(.), 'info')][1]/biblioid[1][@class = 'doi' or 
198                                                                       @class = 'isbn' or
199                                                                       @class = 'isrn' or
200                                                                       @class = 'issn']">
201           <xsl:text>urn:</xsl:text>
202           <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/biblioid[1]/@class"/>
203           <xsl:text>:</xsl:text>
204         </xsl:if>
205         <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/biblioid[1]"/>
206       </xsl:when>
207       <xsl:when test="/*/*[contains(name(.), 'info')]/isbn">
208         <xsl:text>urn:isbn:</xsl:text>
209         <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/isbn[1]"/>
210       </xsl:when>
211       <xsl:when test="/*/*[contains(name(.), 'info')]/issn">
212         <xsl:text>urn:issn:</xsl:text>
213         <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/issn[1]"/>
214       </xsl:when>
215       <xsl:otherwise>
216         <xsl:choose>
217           <xsl:when test="/*/*[contains(name(.), 'info')]/invpartnumber"> <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/invpartnumber[1]"/> </xsl:when>
218           <xsl:when test="/*/*[contains(name(.), 'info')]/issuenum">      <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/issuenum[1]"/> </xsl:when>
219           <xsl:when test="/*/*[contains(name(.), 'info')]/productnumber"> <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/productnumber[1]"/> </xsl:when>
220           <xsl:when test="/*/*[contains(name(.), 'info')]/seriesvolnums"> <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/seriesvolnums[1]"/> </xsl:when>
221           <xsl:when test="/*/*[contains(name(.), 'info')]/volumenum">     <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/volumenum[1]"/> </xsl:when>
222           <!-- Deprecated -->
223           <xsl:when test="/*/*[contains(name(.), 'info')]/pubsnumber">    <xsl:value-of select="/*/*[contains(name(.), 'info')][1]/pubsnumber[1]"/> </xsl:when>
224         </xsl:choose>  
225         <xsl:text>_</xsl:text>
226         <xsl:choose>
227           <xsl:when test="/*/@id">
228             <xsl:value-of select="/*/@id"/>
229           </xsl:when>
230           <xsl:when test="/*/@xml:id">
231             <xsl:value-of select="/*/@xml:id"/>
232           </xsl:when>
233           <xsl:otherwise>
234             <!-- TODO: Do UUIDs here -->
235             <xsl:value-of select="generate-id(/*)"/>
236           </xsl:otherwise>
237         </xsl:choose>
238       </xsl:otherwise>
239     </xsl:choose>
240   </xsl:template>
241
242   <xsl:template name="opf">
243     <xsl:variable name="package-identifier-id"><xsl:value-of select="concat(name(/*), 'id')"/></xsl:variable>
244     <xsl:variable name="doc.title">
245       <xsl:call-template name="get.doc.title" />
246     </xsl:variable>
247     <xsl:call-template name="write.chunk">
248       <xsl:with-param name="filename">
249         <xsl:value-of select="$epub.opf.filename" />
250       </xsl:with-param>
251       <xsl:with-param name="method" select="'xml'" />
252       <xsl:with-param name="encoding" select="'utf-8'" />
253       <xsl:with-param name="indent" select="'no'" />
254       <xsl:with-param name="quiet" select="$chunk.quietly" />
255       <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
256       <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
257       <xsl:with-param name="content">
258         <xsl:element namespace="http://www.idpf.org/2007/opf" name="package">
259           <xsl:attribute name="version">2.0</xsl:attribute>
260           <xsl:attribute name="unique-identifier"> <xsl:value-of select="$package-identifier-id"/> </xsl:attribute>
261
262           <xsl:element namespace="http://www.idpf.org/2007/opf" name="metadata">
263             <xsl:element name="dc:identifier">
264               <xsl:attribute name="id"><xsl:value-of select="$package-identifier-id"/></xsl:attribute>
265               <xsl:call-template name="package-identifier"/>
266             </xsl:element>  
267
268             <xsl:element name="dc:title">
269               <xsl:value-of select="normalize-space($doc.title)"/>
270             </xsl:element>
271
272             <xsl:apply-templates select="/*/*[contains(name(.), 'info')]/*"
273                                  mode="opf.metadata"/>        
274             <xsl:element name="dc:language">
275               <xsl:call-template name="l10n.language">
276                 <xsl:with-param name="target" select="/*"/>
277               </xsl:call-template>  
278             </xsl:element>
279
280             <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
281               <xsl:element namespace="http://www.idpf.org/2007/opf" name="meta">
282                 <xsl:attribute name="name">cover</xsl:attribute>
283                 <xsl:attribute name="content">
284                   <xsl:value-of select="$epub.cover.image.id"/>
285                 </xsl:attribute>
286               </xsl:element>
287             </xsl:if>
288
289           </xsl:element>
290           <xsl:call-template name="opf.manifest"/>
291           <xsl:call-template name="opf.spine"/>
292           <xsl:call-template name="opf.guide"/>
293
294         </xsl:element>
295       </xsl:with-param>
296     </xsl:call-template>
297   </xsl:template>
298
299   <xsl:template name="container">
300     <xsl:call-template name="write.chunk">
301       <xsl:with-param name="filename">
302         <xsl:value-of select="$epub.metainf.dir" />
303         <xsl:value-of select="$epub.container.filename" />
304       </xsl:with-param>
305       <xsl:with-param name="method" select="'xml'" />
306       <xsl:with-param name="encoding" select="'utf-8'" />
307       <xsl:with-param name="indent" select="'no'" />
308       <xsl:with-param name="quiet" select="$chunk.quietly" />
309       <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
310       <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
311
312       <xsl:with-param name="content">
313         <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="container">
314           <xsl:attribute name="version">1.0</xsl:attribute>
315           <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfiles">
316             <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfile">
317               <xsl:attribute name="full-path">
318                 <xsl:value-of select="$epub.opf.filename" />
319               </xsl:attribute>
320               <xsl:attribute name="media-type">
321                 <xsl:text>application/oebps-package+xml</xsl:text>
322               </xsl:attribute>
323             </xsl:element>
324           </xsl:element>
325         </xsl:element>
326       </xsl:with-param>
327     </xsl:call-template>
328   </xsl:template>
329
330   <xsl:template name="ncx">
331     <xsl:call-template name="write.chunk">
332       <xsl:with-param name="filename">
333         <xsl:if test="$manifest.in.base.dir != 0">
334           <xsl:value-of select="$base.dir" />
335         </xsl:if>
336         <xsl:value-of select="$epub.ncx.filename" />
337       </xsl:with-param>
338       <xsl:with-param name="method" select="'xml'" />
339       <xsl:with-param name="encoding" select="'utf-8'" />
340       <xsl:with-param name="indent" select="'no'" />
341       <xsl:with-param name="quiet" select="$chunk.quietly" />
342       <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
343       <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
344       <xsl:with-param name="content">
345         <xsl:element name="ncx" namespace="http://www.daisy.org/z3986/2005/ncx/">
346           <xsl:attribute name="version">2005-1</xsl:attribute>
347
348             <!-- Via Martin Goerner: On covers: the IDPF2.0 standard unfortunately does not have a provision for
349             covers. We had to add one and we did so in conjunction with the IDPF and
350             various publishers. The tag chosen to define the covers is:
351             <meta name="cover" content="-reference to a manifest item-">
352             Then, we also added a bit of logic to get rid cleanly of the HTML cover
353             people usually add because the logical cover is not specced by the IDPF. So,
354             if the HTML cover item is marked linear="no" AND there is a guide item of
355             type="cover" pointing to it AND there is a logical cover specified in a
356             <meta name="cover"> tag, THEN, the HTML cover is discarded. -->
357           <xsl:element name="head" namespace="http://www.daisy.org/z3986/2005/ncx/">
358             <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
359               <xsl:element name="meta" namespace="http://www.daisy.org/z3986/2005/ncx/">
360                 <xsl:attribute name="name">cover</xsl:attribute>
361                 <xsl:attribute name="content">
362                   <xsl:value-of select="$epub.cover.id"/>
363                 </xsl:attribute>
364               </xsl:element>
365             </xsl:if>
366             <xsl:element name="meta" namespace="http://www.daisy.org/z3986/2005/ncx/">
367               <xsl:attribute name="name">dtb:uid</xsl:attribute>
368               <xsl:attribute name="content"><xsl:call-template name="package-identifier"/></xsl:attribute>
369             </xsl:element>
370           </xsl:element>
371
372           <xsl:choose>
373             <xsl:when test="$rootid != ''">
374               <xsl:variable name="title">
375                 <xsl:if test="$epub.autolabel != 0">
376                   <xsl:variable name="label.markup">
377                     <xsl:apply-templates select="key('id',$rootid)" mode="label.markup" />
378                   </xsl:variable>
379                   <xsl:if test="normalize-space($label.markup)">
380                     <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
381                   </xsl:if>
382                 </xsl:if>
383                 <xsl:apply-templates select="key('id',$rootid)" mode="title.markup" />
384               </xsl:variable>
385               <xsl:variable name="href">
386                 <xsl:call-template name="href.target.with.base.dir">
387                   <xsl:with-param name="object" select="key('id',$rootid)" />
388                 </xsl:call-template>
389               </xsl:variable>
390               <xsl:element name="docTitle" namespace="http://www.daisy.org/z3986/2005/ncx/">
391                 <xsl:element name="text" namespace="http://www.daisy.org/z3986/2005/ncx/"><xsl:value-of select="normalize-space($title)" />  </xsl:element>
392               </xsl:element>
393               <xsl:element name="navMap" namespace="http://www.daisy.org/z3986/2005/ncx/">
394                 <xsl:apply-templates select="key('id',$rootid)/*" mode="ncx" />
395               </xsl:element>
396             </xsl:when>
397             <xsl:otherwise>
398               <xsl:variable name="title">
399                 <xsl:if test="$epub.autolabel != 0">
400                   <xsl:variable name="label.markup">
401                     <xsl:apply-templates select="/*" mode="label.markup" />
402                   </xsl:variable>
403                   <xsl:if test="normalize-space($label.markup)">
404                     <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
405                   </xsl:if>
406                 </xsl:if>
407                 <xsl:apply-templates select="/*" mode="title.markup" />
408               </xsl:variable>
409               <xsl:variable name="href">
410                 <xsl:call-template name="href.target.with.base.dir">
411                   <xsl:with-param name="object" select="/" />
412                 </xsl:call-template>
413               </xsl:variable>
414               <xsl:element name="docTitle" namespace="http://www.daisy.org/z3986/2005/ncx/">
415                 <xsl:element name="text" namespace="http://www.daisy.org/z3986/2005/ncx/">
416                   <xsl:value-of select="normalize-space($title)" />
417                 </xsl:element>
418               </xsl:element>
419               <xsl:element name="navMap" namespace="http://www.daisy.org/z3986/2005/ncx/">
420                 <xsl:choose>
421                   <xsl:when test="$root.is.a.chunk != '0'">
422                     <xsl:apply-templates select="/*" mode="ncx" />
423                   </xsl:when>
424                   <xsl:otherwise>
425                     <xsl:apply-templates select="/*/*" mode="ncx" />
426                   </xsl:otherwise>
427                 </xsl:choose>
428               </xsl:element>
429             </xsl:otherwise>
430
431           </xsl:choose>
432         </xsl:element>
433       </xsl:with-param>
434     </xsl:call-template>
435   </xsl:template>
436
437   <xsl:template match="book|
438                        article|
439                        part|
440                        reference|
441                        preface|
442                        chapter|
443                        bibliography|
444                        appendix|
445                        glossary|
446                        section|
447                        sect1|
448                        sect2|
449                        sect3|
450                        sect4|
451                        sect5|
452                        refentry|
453                        colophon|
454                        bibliodiv[title]|
455                        setindex|
456                        index"
457                 mode="ncx">
458     <xsl:variable name="depth" select="count(ancestor::*)"/>
459     <xsl:variable name="title">
460       <xsl:if test="$epub.autolabel != 0">
461         <xsl:variable name="label.markup">
462           <xsl:apply-templates select="." mode="label.markup" />
463         </xsl:variable>
464         <xsl:if test="normalize-space($label.markup)">
465           <xsl:value-of
466             select="concat($label.markup,$autotoc.label.separator)" />
467         </xsl:if>
468       </xsl:if>
469       <xsl:apply-templates select="." mode="title.markup" />
470     </xsl:variable>
471
472     <xsl:variable name="href">
473       <xsl:call-template name="href.target.with.base.dir">
474         <xsl:with-param name="context" select="/" />
475         <!-- Generate links relative to the location of root file/toc.xml file -->
476       </xsl:call-template>
477     </xsl:variable>
478
479     <xsl:variable name="id">
480       <xsl:value-of select="generate-id(.)"/>
481     </xsl:variable>
482     <xsl:variable name="order">
483       <xsl:value-of select="$depth +
484                                   count(preceding::part|
485                                   preceding::reference|
486                                   preceding::book[parent::set]|
487                                   preceding::preface|
488                                   preceding::chapter|
489                                   preceding::bibliography|
490                                   preceding::appendix|
491                                   preceding::article|
492                                   preceding::glossary|
493                                   preceding::section[not(parent::partintro)]|
494                                   preceding::sect1[not(parent::partintro)]|
495                                   preceding::sect2[not(ancestor::partintro)]|
496                                   preceding::sect3[not(ancestor::partintro)]|
497                                   preceding::sect4[not(ancestor::partintro)]|
498                                   preceding::sect5[not(ancestor::partintro)]|
499                                   preceding::refentry|
500                                   preceding::colophon|
501                                   preceding::bibliodiv[title]|
502                                   preceding::index)"/>
503     </xsl:variable>
504
505     <xsl:element name="navPoint" namespace="http://www.daisy.org/z3986/2005/ncx/">
506       <xsl:attribute name="id">
507         <xsl:value-of select="$id"/>
508       </xsl:attribute>
509
510       <xsl:attribute name="playOrder">
511         <xsl:choose>
512           <xsl:when test="/*[self::set]">
513             <xsl:value-of select="$order"/>
514           </xsl:when>
515           <xsl:when test="$root.is.a.chunk != '0'">
516             <xsl:value-of select="$order + 1"/>
517           </xsl:when>
518           <xsl:otherwise>
519             <xsl:value-of select="$order - 0"/>
520           </xsl:otherwise>
521         </xsl:choose>
522       </xsl:attribute>
523       <xsl:element name="navLabel" namespace="http://www.daisy.org/z3986/2005/ncx/">
524         <xsl:element name="text" namespace="http://www.daisy.org/z3986/2005/ncx/"><xsl:value-of select="normalize-space($title)"/> </xsl:element>
525       </xsl:element>
526       <xsl:element name="content" namespace="http://www.daisy.org/z3986/2005/ncx/">
527         <xsl:attribute name="src">
528           <xsl:value-of select="$href"/>
529         </xsl:attribute>
530       </xsl:element>
531       <xsl:apply-templates select="book[parent::set]|part|reference|preface|chapter|bibliography|appendix|article|glossary|section|sect1|sect2|sect3|sect4|sect5|refentry|colophon|bibliodiv[title]|setindex|index" mode="ncx"/>
532     </xsl:element>
533
534   </xsl:template>
535
536   <xsl:template match="*" mode="opf.metadata">
537     <!-- override if you care -->
538   </xsl:template>
539
540   <xsl:template match="authorgroup" mode="opf.metadata">
541     <xsl:apply-templates select="author|corpauthor" mode="opf.metadata"/>
542   </xsl:template>
543
544   <xsl:template match="author|corpauthor" mode="opf.metadata">
545     <xsl:variable name="n">
546       <xsl:call-template name="person.name">
547         <xsl:with-param name="node" select="."/>
548       </xsl:call-template>
549     </xsl:variable>
550     <xsl:element name="dc:creator">
551       <xsl:attribute name="opf:file-as">
552         <xsl:call-template name="person.name.last-first">
553           <xsl:with-param name="node" select="."/>
554         </xsl:call-template>
555       </xsl:attribute>
556       <xsl:value-of select="normalize-space(string($n))"/>
557     </xsl:element>
558   </xsl:template>
559
560   <xsl:template match="date" mode="opf.metadata">
561     <xsl:element name="dc:date">
562       <xsl:value-of select="normalize-space(string(.))"/>
563     </xsl:element>
564   </xsl:template>
565
566
567   <!-- Space separate the compontents of the abstract (dropping the inline markup, sadly) -->
568   <xsl:template match="abstract" mode="opf.metadata">
569     <xsl:element name="dc:description">
570       <xsl:for-each select="formalpara|para|simpara|title">
571         <xsl:choose>
572           <xsl:when test="self::formalpara">
573             <xsl:value-of select="normalize-space(string(title))"/>
574             <xsl:text>: </xsl:text>
575             <xsl:value-of select="normalize-space(string(para))"/>
576           </xsl:when>
577           <xsl:otherwise>
578             <xsl:value-of select="normalize-space(string(.))"/>
579           </xsl:otherwise>  
580         </xsl:choose>
581         <xsl:if test="self::title">
582           <xsl:text>:</xsl:text>
583         </xsl:if>
584         <xsl:if test="not(position() = last())">
585           <xsl:text> </xsl:text>
586         </xsl:if>
587       </xsl:for-each>  
588     </xsl:element>
589   </xsl:template>
590
591   <xsl:template match="subjectset" mode="opf.metadata">
592     <xsl:apply-templates select="subject/subjectterm" mode="opf.metadata"/>
593   </xsl:template>
594   
595   <xsl:template match="subjectterm" mode="opf.metadata">
596     <xsl:element name="dc:subject">
597       <xsl:value-of select="normalize-space(string(.))"/>
598     </xsl:element>
599   </xsl:template>
600
601   <xsl:template match="publisher" mode="opf.metadata">
602     <xsl:apply-templates select="publishername" mode="opf.metadata"/>
603   </xsl:template>
604   
605   <xsl:template match="publishername" mode="opf.metadata">
606     <xsl:element name="dc:publisher">
607       <xsl:value-of select="normalize-space(string(.))"/>
608     </xsl:element>
609   </xsl:template>
610
611   <xsl:template match="copyright" mode="opf.metadata">
612     <xsl:variable name="copyright.date">
613       <xsl:call-template name="copyright.years">
614         <xsl:with-param name="years" select="year"/>
615         <xsl:with-param name="print.ranges" select="$make.year.ranges"/>
616         <xsl:with-param name="single.year.ranges" select="$make.single.year.ranges"/>
617       </xsl:call-template>
618     </xsl:variable>
619     <xsl:if test="not(../date)">
620       <xsl:element name="dc:date">
621         <xsl:call-template name="copyright.years">
622           <xsl:with-param name="years" select="year[last()]"/>
623           <xsl:with-param name="print.ranges" select="0"/>
624           <xsl:with-param name="single.year.ranges" select="0"/>
625         </xsl:call-template>
626       </xsl:element>
627     </xsl:if>
628     <xsl:element name="dc:rights">
629       <xsl:call-template name="gentext">
630         <xsl:with-param name="key" select="'Copyright'"/>
631       </xsl:call-template>
632       <xsl:call-template name="gentext.space"/>
633       <xsl:text>&#x00A9;</xsl:text>
634       <xsl:call-template name="gentext.space"/>
635       <xsl:value-of select="$copyright.date"/>
636       <xsl:call-template name="gentext.space"/>
637       <xsl:apply-templates select="holder" mode="titlepage.mode"/>
638     </xsl:element>
639   </xsl:template>
640
641   <xsl:template name="opf.guide">
642     <xsl:if test="contains($toc.params, 'toc') or 
643                   /*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
644       <xsl:element namespace="http://www.idpf.org/2007/opf" name="guide">
645         <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
646           <xsl:element namespace="http://www.idpf.org/2007/opf" name="reference">
647             <xsl:attribute name="href">
648               <xsl:value-of select="$epub.cover.html" />
649             </xsl:attribute>
650             <xsl:attribute name="type">cover</xsl:attribute>
651             <xsl:attribute name="title">Cover</xsl:attribute>
652           </xsl:element>
653         </xsl:if>  
654
655         <xsl:if test="contains($toc.params, 'toc')">
656           <xsl:element namespace="http://www.idpf.org/2007/opf" name="reference">
657             <xsl:attribute name="href">
658               <xsl:call-template name="toc-href">
659                 <xsl:with-param name="node" select="/*"/>
660               </xsl:call-template>
661             </xsl:attribute>
662             <xsl:attribute name="type">toc</xsl:attribute>
663             <xsl:attribute name="title">Table of Contents</xsl:attribute>
664           </xsl:element>
665         </xsl:if>  
666       </xsl:element>  
667     </xsl:if>  
668   </xsl:template>
669
670   <xsl:template name="opf.spine">
671
672     <xsl:element namespace="http://www.idpf.org/2007/opf" name="spine">
673       <xsl:attribute name="toc">
674         <xsl:value-of select="$epub.ncx.toc.id"/>
675       </xsl:attribute>
676
677       <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
678         <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
679           <xsl:attribute name="idref">
680             <xsl:value-of select="$epub.cover.id"/>
681           </xsl:attribute>
682           <xsl:attribute name="linear">
683           <xsl:choose>
684             <xsl:when test="$epub.cover.linear">
685               <xsl:text>yes</xsl:text>
686             </xsl:when>
687             <xsl:otherwise>no</xsl:otherwise>
688           </xsl:choose>
689           </xsl:attribute>
690         </xsl:element>
691       </xsl:if>
692
693
694       <xsl:if test="contains($toc.params, 'toc')">
695         <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
696           <xsl:attribute name="idref"> <xsl:value-of select="$epub.html.toc.id"/> </xsl:attribute>
697           <xsl:attribute name="linear">yes</xsl:attribute>
698         </xsl:element>
699       </xsl:if>  
700
701       <!-- TODO: be nice to have a idref="titlepage" here -->
702       <xsl:choose>
703         <xsl:when test="$root.is.a.chunk != '0'">
704           <xsl:apply-templates select="/*" mode="opf.spine"/>
705         </xsl:when>
706         <xsl:otherwise>
707           <xsl:apply-templates select="/*/*" mode="opf.spine"/>
708         </xsl:otherwise>
709       </xsl:choose>
710                                    
711     </xsl:element>
712   </xsl:template>
713
714   <xsl:template match="*" mode="opf.spine">
715     <xsl:variable name="is.chunk">
716       <xsl:call-template name="chunk">
717         <xsl:with-param name="node" select="."/>
718       </xsl:call-template>
719     </xsl:variable>
720
721     <xsl:if test="$is.chunk != 0">
722       <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
723         <xsl:attribute name="idref">
724           <xsl:value-of select="generate-id(.)"/>
725         </xsl:attribute>
726       </xsl:element>
727       <xsl:apply-templates select="*|.//refentry" mode="opf.spine"/>
728     </xsl:if>
729   </xsl:template>
730
731   <xsl:template name="opf.manifest">
732     <xsl:element namespace="http://www.idpf.org/2007/opf" name="manifest">
733       <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
734         <xsl:attribute name="id"> <xsl:value-of select="$epub.ncx.toc.id"/> </xsl:attribute>
735         <xsl:attribute name="media-type">application/x-dtbncx+xml</xsl:attribute>
736         <xsl:attribute name="href"><xsl:value-of select="$epub.ncx.filename"/> </xsl:attribute>
737       </xsl:element>
738
739       <xsl:if test="contains($toc.params, 'toc')">
740         <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
741           <xsl:attribute name="id"> <xsl:value-of select="$epub.html.toc.id"/> </xsl:attribute>
742           <xsl:attribute name="media-type">application/xhtml+xml</xsl:attribute>
743           <xsl:attribute name="href">
744             <xsl:call-template name="toc-href">
745               <xsl:with-param name="node" select="/*"/>
746             </xsl:call-template>
747           </xsl:attribute>
748         </xsl:element>
749       </xsl:if>  
750
751       <xsl:if test="$html.stylesheet != ''">
752         <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
753           <xsl:attribute name="media-type">text/css</xsl:attribute>
754           <xsl:attribute name="id">css</xsl:attribute>
755           <xsl:attribute name="href"><xsl:value-of select="$html.stylesheet"/></xsl:attribute>
756         </xsl:element>
757       </xsl:if>
758
759       <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
760         <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
761           <xsl:attribute name="id"> <xsl:value-of select="$epub.cover.id"/> </xsl:attribute>
762           <xsl:attribute name="href"> 
763             <xsl:value-of select="$epub.cover.html"/>
764           </xsl:attribute>
765           <xsl:attribute name="media-type">application/xhtml+xml</xsl:attribute>
766         </xsl:element>
767       </xsl:if>  
768
769       <xsl:choose>
770         <xsl:when test="$epub.embedded.fonts != '' and not(contains($epub.embedded.fonts, ','))">
771           <xsl:call-template name="embedded-font-item">
772             <xsl:with-param name="font.file" select="$epub.embedded.fonts"/> <!-- There is just one -->
773           </xsl:call-template>
774         </xsl:when>
775         <xsl:when test="$epub.embedded.fonts != ''">
776           <xsl:variable name="font.file.tokens" select="str:tokenize($epub.embedded.fonts, ',')"/>
777           <xsl:for-each select="exsl:node-set($font.file.tokens)">
778             <xsl:call-template name="embedded-font-item">
779               <xsl:with-param name="font.file" select="."/>
780               <xsl:with-param name="font.order" select="position()"/>
781             </xsl:call-template>
782           </xsl:for-each>
783         </xsl:when>
784       </xsl:choose>
785
786       <!-- TODO: be nice to have a id="titlepage" here -->
787       <xsl:apply-templates select="//part|
788                                    //book[*[last()][self::bookinfo]]|
789                                    //book[bookinfo]|
790                                    /set|
791                                    /set/book|
792                                    //reference|
793                                    //preface|
794                                    //chapter|
795                                    //bibliography|
796                                    //appendix|
797                                    //article|
798                                    //glossary|
799                                    //section|
800                                    //sect1|
801                                    //sect2|
802                                    //sect3|
803                                    //sect4|
804                                    //sect5|
805                                    //refentry|
806                                    //colophon|
807                                    //bibliodiv[title]|
808                                    //index|
809                                    //setindex|
810                                    //graphic|
811                                    //inlinegraphic|
812                                    //mediaobject|
813                                    //mediaobjectco|
814                                    //inlinemediaobject" 
815                            mode="opf.manifest"/>
816       <xsl:call-template name="opf.calloutlist"/>
817     </xsl:element>
818   </xsl:template>
819
820   <xsl:template name="opf.calloutlist">
821     <xsl:variable name="format">
822       <xsl:call-template name="guess-media-type">
823         <xsl:with-param name="ext" select="$callout.graphics.extension"/>
824       </xsl:call-template>
825     </xsl:variable>  
826     <xsl:if test="(//calloutlist|//co)">
827       <xsl:call-template name="opf.reference.callout">
828         <xsl:with-param name="conum" select="1"/>
829         <xsl:with-param name="format" select="$format"/>
830       </xsl:call-template>
831     </xsl:if>
832   </xsl:template>
833
834   <xsl:template name="opf.reference.callout">
835     <xsl:param name="conum"/>
836     <xsl:param name="format"/>
837
838     <xsl:variable name="filename" select="concat($callout.graphics.path, $conum, $callout.graphics.extension)"/>
839
840     <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
841       <xsl:attribute name="id"> <xsl:value-of select="concat(generate-id(.), 'callout', $conum)"/> </xsl:attribute>
842       <xsl:attribute name="href"> <xsl:value-of select="$filename"/> </xsl:attribute>
843       <xsl:attribute name="media-type">
844         <xsl:value-of select="$format"/>
845       </xsl:attribute>
846     </xsl:element>
847     <xsl:if test="($conum &lt; $callout.graphics.number.limit)">
848       <xsl:call-template name="opf.reference.callout">
849         <xsl:with-param name="conum" select="$conum + 1"/>
850         <xsl:with-param name="format" select="$format"/>
851       </xsl:call-template>
852     </xsl:if>
853   </xsl:template>
854
855   <xsl:template name="guess-media-type">
856     <xsl:param name="ext"></xsl:param>
857     <xsl:choose>
858       <xsl:when test="contains($ext, '.gif')">
859         <xsl:text>image/gif</xsl:text>
860       </xsl:when>
861       <xsl:when test="contains($ext, 'GIF')">
862         <xsl:text>image/gif</xsl:text>
863       </xsl:when>
864       <xsl:when test="contains($ext, '.png')">
865         <xsl:text>image/png</xsl:text>
866       </xsl:when>
867       <xsl:when test="contains($ext, 'PNG')">
868         <xsl:text>image/png</xsl:text>
869       </xsl:when>
870       <xsl:when test="contains($ext, '.jpeg')">
871         <xsl:text>image/jpeg</xsl:text>
872       </xsl:when>
873       <xsl:when test="contains($ext, 'JPEG')">
874         <xsl:text>image/jpeg</xsl:text>
875       </xsl:when>
876       <xsl:when test="contains($ext, '.jpg')">
877         <xsl:text>image/jpeg</xsl:text>
878       </xsl:when>
879       <xsl:when test="contains($ext, 'JPG')">
880         <xsl:text>image/jpeg</xsl:text>
881       </xsl:when>
882       <xsl:when test="contains($ext, '.svg')">
883         <xsl:text>image/svg+xml</xsl:text>
884       </xsl:when>
885       <xsl:when test="contains($ext, 'SVG')">
886         <xsl:text>image/svg+xml</xsl:text>
887       </xsl:when>
888       <xsl:otherwise>
889         <!-- we failed -->
890         <xsl:text></xsl:text>
891       </xsl:otherwise>
892     </xsl:choose>
893   </xsl:template>
894
895   <xsl:template match="mediaobject|
896                        mediaobjectco|
897                        inlinemediaobject" 
898                 mode="opf.manifest">
899
900     <xsl:variable name="olist" select="imageobject|imageobjectco                      |videoobject|audioobject                      |textobject"/>
901
902     <xsl:variable name="object.index">
903       <xsl:call-template name="select.mediaobject.index">
904         <xsl:with-param name="olist" select="$olist"/>
905         <xsl:with-param name="count" select="1"/>
906       </xsl:call-template>
907     </xsl:variable>
908
909     <xsl:variable name="object" select="$olist[position() = $object.index]"/>
910
911     <xsl:choose>
912       <xsl:when test="$object/descendant::imagedata[@format = 'GIF' or 
913                                                     @format = 'GIF87a' or 
914                                                     @format = 'GIF89a' or 
915                                                     @format = 'JPEG' or 
916                                                     @format = 'JPG' or 
917                                                     @format = 'PNG' or 
918                                                     @format = 'SVG']">
919         <xsl:apply-templates select="$object[descendant::imagedata[@format = 'GIF' or 
920                                                                    @format = 'GIF87a' or 
921                                                                    @format = 'GIF89a' or 
922                                                                    @format = 'JPEG' or 
923                                                                    @format = 'JPG' or 
924                                                                    @format = 'PNG' or 
925                                                                    @format = 'SVG']][1]/imagedata"
926                              mode="opf.manifest"/>              
927       </xsl:when>
928       <xsl:otherwise>
929         <xsl:apply-templates select="$object/imagedata[1]"
930                              mode="opf.manifest"/>              
931       </xsl:otherwise>
932     </xsl:choose>  
933   </xsl:template>
934
935   <xsl:template match="cover/mediaobject|
936                        mediaobject[@role='cover']"
937                 mode="opf.manifest">
938     <xsl:choose>
939       <xsl:when test="imageobject[@role='front-large']">
940         <xsl:apply-templates select="imageobject[@role='front-large']/imagedata"
941                              mode="opf.manifest"/>              
942       </xsl:when>
943       <xsl:otherwise>
944         <xsl:apply-templates select="imageobject/imagedata[1]"
945                              mode="opf.manifest"/>              
946       </xsl:otherwise>
947     </xsl:choose>  
948   </xsl:template>
949
950   <xsl:template match="mediaobjectco"
951                 mode="opf.manifest">
952     <xsl:message>WARNING: mediaobjectco almost certainly will not render as expected in .epub!</xsl:message>
953     <xsl:apply-templates select="imageobjectco/imageobject/imagedata" 
954                          mode="opf.manifest"/>              
955   </xsl:template>
956
957   <!-- TODO: Barf (xsl:message terminate=yes) if you find a graphic with no reasonable format or a mediaobject w/o same? [option to not die?] -->
958
959   <!-- wish I had XSLT2 ...-->
960   <!-- TODO: priority a hack -->
961   <xsl:template match="graphic[not(@format)]|
962                        inlinegraphic[not(@format)]|
963                        imagedata[not(@format)]"
964                 mode="opf.manifest">        
965     <xsl:variable name="filename">
966       <xsl:choose>
967         <xsl:when test="contains(name(.), 'graphic')">
968           <xsl:choose>
969             <xsl:when test="@entityref">
970               <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
971             </xsl:when>
972             <xsl:otherwise>
973               <xsl:apply-templates select="@fileref"/>
974             </xsl:otherwise>
975           </xsl:choose>
976         </xsl:when>
977         <xsl:otherwise>
978           <xsl:call-template name="mediaobject.filename">
979             <xsl:with-param name="object" select=".."/>
980           </xsl:call-template>
981         </xsl:otherwise>
982       </xsl:choose>
983     </xsl:variable>  
984     <xsl:variable name="format"> 
985       <xsl:call-template name="guess-media-type">
986         <xsl:with-param name="ext" select="@fileref"/>
987       </xsl:call-template>
988     </xsl:variable>
989     <xsl:variable name="fr" select="@fileref"/>
990     <xsl:if test="$format != ''">
991       <!-- only do this if we're the first file to match -->
992       <!-- TODO: Why can't this be simple equality?? (I couldn't get it to work) -->
993       <xsl:if test="generate-id(.) = generate-id(key('image-filerefs', $fr)[1])">
994         <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
995           <xsl:attribute name="id"> 
996             <xsl:choose>
997               <xsl:when test="ancestor::mediaobject[@role='cover'] and parent::*[@role='front-large']">
998                 <xsl:value-of select="$epub.cover.image.id"/>
999               </xsl:when>
1000               <xsl:when test="ancestor::mediaobject[@role='cover'] and (count(ancestor::mediaobject//imageobject) = 1)">
1001                 <xsl:value-of select="$epub.cover.image.id"/>
1002               </xsl:when>
1003               <xsl:when test="ancestor::cover">
1004                 <xsl:value-of select="$epub.cover.image.id"/>
1005               </xsl:when>
1006               <xsl:otherwise>
1007                 <xsl:value-of select="generate-id(.)"/> 
1008               </xsl:otherwise>
1009             </xsl:choose>
1010           </xsl:attribute>  
1011           <xsl:attribute name="href"> <xsl:value-of select="$filename"/> </xsl:attribute>
1012           <xsl:attribute name="media-type">
1013             <xsl:value-of select="$format"/>
1014           </xsl:attribute>
1015         </xsl:element>
1016       </xsl:if>
1017     </xsl:if>
1018   </xsl:template>
1019
1020   <!-- Note: Selection of the first interesting imagedata is done in the select -->
1021   <xsl:template match="graphic[@format = 'GIF' or @format = 'GIF87a' or @format = 'GIF89a' or @format = 'JPEG' or @format = 'JPG' or @format = 'PNG' or @format = 'SVG']|
1022                        inlinegraphic[@format = 'GIF' or @format = 'GIF87a' or @format = 'GIF89a' or @format = 'JPEG' or @format = 'JPG' or @format = 'PNG' or @format = 'SVG']|
1023                        imagedata[@format]"
1024                 mode="opf.manifest">
1025     <xsl:variable name="filename">
1026       <xsl:choose>
1027         <xsl:when test="contains(name(.), 'graphic')">
1028           <xsl:choose>
1029             <xsl:when test="@entityref">
1030               <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1031             </xsl:when>
1032             <xsl:otherwise>
1033               <xsl:apply-templates select="@fileref"/>
1034             </xsl:otherwise>
1035           </xsl:choose>
1036         </xsl:when>
1037         <xsl:otherwise>
1038           <xsl:call-template name="mediaobject.filename">
1039             <xsl:with-param name="object" select=".."/>
1040           </xsl:call-template>
1041         </xsl:otherwise>
1042       </xsl:choose>
1043     </xsl:variable>  
1044     <xsl:variable name="fr" select="@fileref"/>
1045     <!-- only do this if we're the first file to match -->
1046     <!-- TODO: Why can't this be simple equality?? (I couldn't get it to work) -->
1047     <xsl:if test="generate-id(.) = generate-id(key('image-filerefs', $fr)[1])">
1048       <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
1049         <xsl:attribute name="id"> 
1050           <xsl:choose>
1051             <xsl:when test="ancestor::mediaobject[@role='cover'] and parent::*[@role='front-large']">
1052               <xsl:value-of select="$epub.cover.image.id"/>
1053             </xsl:when>
1054             <xsl:when test="ancestor::mediaobject[@role='cover'] and (count(ancestor::mediaobject//imageobject) = 1)">
1055               <xsl:value-of select="$epub.cover.image.id"/>
1056             </xsl:when>
1057             <xsl:when test="ancestor::cover">
1058               <xsl:value-of select="$epub.cover.image.id"/>
1059             </xsl:when>
1060             <xsl:otherwise>
1061               <xsl:value-of select="generate-id(.)"/> 
1062             </xsl:otherwise>
1063           </xsl:choose>
1064         </xsl:attribute>
1065         <xsl:attribute name="href"> <xsl:value-of select="$filename"/> </xsl:attribute>
1066         <xsl:attribute name="media-type">
1067           <xsl:call-template name="guess-media-type">
1068             <xsl:with-param name="ext" select="@format"/>
1069           </xsl:call-template>
1070         </xsl:attribute>
1071       </xsl:element>
1072     </xsl:if>
1073   </xsl:template>
1074
1075   <!-- Warning: While the test indicate this match list is accurate, it may 
1076        need further tweaking to ensure _never_ dropping generated content (XHTML)
1077        from the manifest (OPF file) -->
1078   <xsl:template
1079       match="set|
1080             book[parent::set]|
1081             book[*[last()][self::bookinfo]]|
1082             book[bookinfo]|
1083             article|
1084             part|
1085             reference|
1086             preface|
1087             chapter|
1088             bibliography|
1089             appendix|
1090             glossary|
1091             section|
1092             sect1|
1093             sect2|
1094             sect3|
1095             sect4|
1096             sect5|
1097             refentry|
1098             colophon|
1099             bibliodiv[title]|
1100             setindex|
1101             index"
1102       mode="opf.manifest">
1103     <xsl:variable name="href">
1104       <xsl:call-template name="href.target.with.base.dir">
1105         <xsl:with-param name="context" select="/" />
1106         <!-- Generate links relative to the location of root file/toc.xml file -->
1107       </xsl:call-template>
1108     </xsl:variable>
1109
1110     <xsl:variable name="id">
1111       <xsl:value-of select="generate-id(.)"/>
1112     </xsl:variable>
1113
1114     <xsl:variable name="is.chunk">
1115       <xsl:call-template name="chunk">
1116         <xsl:with-param name="node" select="."/>
1117       </xsl:call-template>
1118     </xsl:variable>
1119
1120     <xsl:if test="$is.chunk != 0">
1121       <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
1122         <xsl:attribute name="id"> <xsl:value-of select="$id"/> </xsl:attribute>
1123         <xsl:attribute name="href"> <xsl:value-of select="$href"/> </xsl:attribute>
1124         <xsl:attribute name="media-type">application/xhtml+xml</xsl:attribute>
1125       </xsl:element>
1126     </xsl:if>  
1127   </xsl:template>  
1128
1129   <xsl:template match="text()" mode="ncx" />
1130
1131   <xsl:template name="html.head">
1132     <xsl:param name="prev" select="/foo"/>
1133     <xsl:param name="next" select="/foo"/>
1134     <xsl:variable name="this" select="."/>
1135     <xsl:variable name="home" select="/*[1]"/>
1136     <xsl:variable name="up" select="parent::*"/>
1137
1138     <head xmlns="http://www.w3.org/1999/xhtml">
1139       <xsl:call-template name="system.head.content"/>
1140       <xsl:call-template name="head.content"/>
1141
1142       <xsl:call-template name="user.head.content"/>
1143     </head>
1144   </xsl:template>
1145
1146   <!-- OVERRIDES xhtml-1_1/graphics.xsl -->
1147   <!-- we can't deal with no img/@alt, because it's required. Try grabbing a title before it instead (hopefully meaningful) -->
1148   <xsl:template name="process.image.attributes">
1149     <xsl:param name="alt"/>
1150     <xsl:param name="html.width"/>
1151     <xsl:param name="html.depth"/>
1152     <xsl:param name="longdesc"/>
1153     <xsl:param name="scale"/>
1154     <xsl:param name="scalefit"/>
1155     <xsl:param name="scaled.contentdepth"/>
1156     <xsl:param name="scaled.contentwidth"/>
1157     <xsl:param name="viewport"/>
1158
1159     <xsl:choose>
1160       <xsl:when test="@contentwidth or @contentdepth">
1161         <!-- ignore @width/@depth, @scale, and @scalefit if specified -->
1162         <xsl:if test="@contentwidth and $scaled.contentwidth != ''">
1163           <xsl:attribute name="width">
1164             <xsl:value-of select="$scaled.contentwidth"/>
1165           </xsl:attribute>
1166         </xsl:if>
1167         <xsl:if test="@contentdepth and $scaled.contentdepth != ''">
1168           <xsl:attribute name="height">
1169             <xsl:value-of select="$scaled.contentdepth"/>
1170           </xsl:attribute>
1171         </xsl:if>
1172       </xsl:when>
1173
1174       <xsl:when test="number($scale) != 1.0">
1175         <!-- scaling is always uniform, so we only have to specify one dimension -->
1176         <!-- ignore @scalefit if specified -->
1177         <xsl:attribute name="width">
1178           <xsl:value-of select="$scaled.contentwidth"/>
1179         </xsl:attribute>
1180       </xsl:when>
1181
1182       <xsl:when test="$scalefit != 0">
1183         <xsl:choose>
1184           <xsl:when test="contains($html.width, '%')">
1185             <xsl:choose>
1186               <xsl:when test="$viewport != 0">
1187                 <!-- The *viewport* will be scaled, so use 100% here! -->
1188                 <xsl:attribute name="width">
1189                   <xsl:value-of select="'100%'"/>
1190                 </xsl:attribute>
1191               </xsl:when>
1192               <xsl:otherwise>
1193                 <xsl:attribute name="width">
1194                   <xsl:value-of select="$html.width"/>
1195                 </xsl:attribute>
1196               </xsl:otherwise>
1197             </xsl:choose>
1198           </xsl:when>
1199
1200           <xsl:when test="contains($html.depth, '%')">
1201             <!-- HTML doesn't deal with this case very well...do nothing -->
1202           </xsl:when>
1203
1204           <xsl:when test="$scaled.contentwidth != '' and $html.width != ''                         and $scaled.contentdepth != '' and $html.depth != ''">
1205             <!-- scalefit should not be anamorphic; figure out which direction -->
1206             <!-- has the limiting scale factor and scale in that direction -->
1207             <xsl:choose>
1208               <xsl:when test="$html.width div $scaled.contentwidth &gt;                             $html.depth div $scaled.contentdepth">
1209                 <xsl:attribute name="height">
1210                   <xsl:value-of select="$html.depth"/>
1211                 </xsl:attribute>
1212               </xsl:when>
1213               <xsl:otherwise>
1214                 <xsl:attribute name="width">
1215                   <xsl:value-of select="$html.width"/>
1216                 </xsl:attribute>
1217               </xsl:otherwise>
1218             </xsl:choose>
1219           </xsl:when>
1220
1221           <xsl:when test="$scaled.contentwidth != '' and $html.width != ''">
1222             <xsl:attribute name="width">
1223               <xsl:value-of select="$html.width"/>
1224             </xsl:attribute>
1225           </xsl:when>
1226
1227           <xsl:when test="$scaled.contentdepth != '' and $html.depth != ''">
1228             <xsl:attribute name="height">
1229               <xsl:value-of select="$html.depth"/>
1230             </xsl:attribute>
1231           </xsl:when>
1232         </xsl:choose>
1233       </xsl:when>
1234     </xsl:choose>
1235
1236     <!-- AN OVERRIDE -->
1237     <xsl:if test="not(@format ='SVG')">
1238       <xsl:attribute name="alt">
1239         <xsl:choose>
1240           <xsl:when test="$alt != ''">
1241             <xsl:value-of select="normalize-space($alt)"/>
1242           </xsl:when>
1243           <xsl:when test="preceding::title[1]">
1244             <xsl:value-of select="normalize-space(preceding::title[1])"/>
1245           </xsl:when>
1246           <xsl:otherwise>
1247             <xsl:text>(missing alt)</xsl:text>
1248           </xsl:otherwise>
1249         </xsl:choose>
1250       </xsl:attribute>
1251     </xsl:if>
1252     <!-- END OF OVERRIDE -->
1253
1254     <xsl:if test="$longdesc != ''">
1255       <xsl:attribute name="longdesc">
1256         <xsl:value-of select="$longdesc"/>
1257       </xsl:attribute>
1258     </xsl:if>
1259
1260     <xsl:if test="@align and $viewport = 0">
1261       <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
1262         <xsl:choose>
1263           <xsl:when test="@align = 'center'">middle</xsl:when>
1264           <xsl:otherwise>
1265             <xsl:value-of select="@align"/>
1266           </xsl:otherwise>
1267         </xsl:choose>
1268       </xsl:attribute>
1269     </xsl:if>
1270   </xsl:template>
1271   
1272   <!-- OVERRIDES xhtml-1_1/chunk-common.xsl   -->
1273   <!-- make a bibliography always a chunk -->
1274   <xsl:template name="chunk"
1275                 priority="1">       
1276     <xsl:param name="node" select="."/>
1277     <!-- returns 1 if $node is a chunk -->
1278
1279     <!-- ==================================================================== -->
1280     <!-- What's a chunk?
1281
1282         The root element
1283         appendix
1284         article
1285         bibliography  ### NO LONGER TRUE in article or part or book
1286         book
1287         chapter
1288         colophon
1289         glossary      in article or part or book
1290         index         in article or part or book
1291         part
1292         preface
1293         refentry
1294         reference
1295         sect{1,2,3,4,5}  if position()>1 && depth < chunk.section.depth
1296         section          if position()>1 && depth < chunk.section.depth
1297         set
1298         setindex
1299                                                                               -->
1300     <!-- ==================================================================== -->
1301
1302   <!--
1303     <xsl:message>
1304       <xsl:text>chunk: </xsl:text>
1305       <xsl:value-of select="name($node)"/>
1306       <xsl:text>(</xsl:text>
1307       <xsl:value-of select="$node/@id"/>
1308       <xsl:text>)</xsl:text>
1309       <xsl:text> csd: </xsl:text>
1310       <xsl:value-of select="$chunk.section.depth"/>
1311       <xsl:text> cfs: </xsl:text>
1312       <xsl:value-of select="$chunk.first.sections"/>
1313       <xsl:text> ps: </xsl:text>
1314       <xsl:value-of select="count($node/parent::section)"/>
1315       <xsl:text> prs: </xsl:text>
1316       <xsl:value-of select="count($node/preceding-sibling::section)"/>
1317     </xsl:message>
1318   -->
1319
1320     <xsl:choose>
1321       <xsl:when test="not($node/parent::*)">1</xsl:when>
1322
1323       <xsl:when test="local-name($node) = 'sect1'                     and $chunk.section.depth &gt;= 1                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::sect1) &gt; 0)">
1324         <xsl:text>1</xsl:text>
1325       </xsl:when>
1326       <xsl:when test="local-name($node) = 'sect2'                     and $chunk.section.depth &gt;= 2                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::sect2) &gt; 0)">
1327         <xsl:call-template name="chunk">
1328           <xsl:with-param name="node" select="$node/parent::*"/>
1329         </xsl:call-template>
1330       </xsl:when>
1331       <xsl:when test="local-name($node) = 'sect3'                     and $chunk.section.depth &gt;= 3                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::sect3) &gt; 0)">
1332         <xsl:call-template name="chunk">
1333           <xsl:with-param name="node" select="$node/parent::*"/>
1334         </xsl:call-template>
1335       </xsl:when>
1336       <xsl:when test="local-name($node) = 'sect4'                     and $chunk.section.depth &gt;= 4                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::sect4) &gt; 0)">
1337         <xsl:call-template name="chunk">
1338           <xsl:with-param name="node" select="$node/parent::*"/>
1339         </xsl:call-template>
1340       </xsl:when>
1341       <xsl:when test="local-name($node) = 'sect5'                     and $chunk.section.depth &gt;= 5                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::sect5) &gt; 0)">
1342         <xsl:call-template name="chunk">
1343           <xsl:with-param name="node" select="$node/parent::*"/>
1344         </xsl:call-template>
1345       </xsl:when>
1346       <xsl:when test="local-name($node) = 'section'                     and $chunk.section.depth &gt;= count($node/ancestor::section)+1                     and ($chunk.first.sections != 0                          or count($node/preceding-sibling::section) &gt; 0)">
1347         <xsl:call-template name="chunk">
1348           <xsl:with-param name="node" select="$node/parent::*"/>
1349         </xsl:call-template>
1350       </xsl:when>
1351
1352       <xsl:when test="local-name($node)='preface'">1</xsl:when>
1353       <xsl:when test="local-name($node)='chapter'">1</xsl:when>
1354       <xsl:when test="local-name($node)='appendix'">1</xsl:when>
1355       <xsl:when test="local-name($node)='article'">1</xsl:when>
1356       <xsl:when test="local-name($node)='part'">1</xsl:when>
1357       <xsl:when test="local-name($node)='reference'">1</xsl:when>
1358       <xsl:when test="local-name($node)='refentry'">1</xsl:when>
1359       <xsl:when test="local-name($node)='index' and ($generate.index != 0 or count($node/*) &gt; 0)                     and (local-name($node/parent::*) = 'article'                     or local-name($node/parent::*) = 'book'                     or local-name($node/parent::*) = 'part'                     )">1</xsl:when>
1360       <!-- AN OVERRIDE -->
1361       <xsl:when test="local-name($node)='bibliography'">1</xsl:when>
1362       <!-- END OF OVERRIDE -->
1363       <xsl:when test="local-name($node)='glossary'                     and (local-name($node/parent::*) = 'article'                     or local-name($node/parent::*) = 'book'                     or local-name($node/parent::*) = 'part'                     )">1</xsl:when>
1364       <xsl:when test="local-name($node)='colophon'">1</xsl:when>
1365       <xsl:when test="local-name($node)='book'">1</xsl:when>
1366       <xsl:when test="local-name($node)='set'">1</xsl:when>
1367       <xsl:when test="local-name($node)='setindex'">1</xsl:when>
1368       <xsl:when test="local-name($node)='legalnotice'                     and $generate.legalnotice.link != 0">1</xsl:when>
1369       <xsl:otherwise>0</xsl:otherwise>
1370     </xsl:choose>
1371   </xsl:template>
1372
1373   <!-- OVERRIDES xhtml-1_1/chunk-code.xsl   -->
1374   <!-- Add chunking for bibliography as root element -->
1375   <!-- AN OVERRIDE --> 
1376   <xsl:template match="set|
1377                        book|
1378                        part|
1379                        preface|
1380                        chapter|
1381                        appendix|
1382                        article|
1383                        reference|
1384                        refentry|
1385                        book/glossary|
1386                        article/glossary|
1387                        part/glossary|
1388                        bibliography|
1389                        colophon"
1390                 priority="1">       
1391   <!-- END OF OVERRIDE --> 
1392     <xsl:choose>
1393       <xsl:when test="$onechunk != 0 and parent::*">
1394         <xsl:apply-imports/>
1395       </xsl:when>
1396       <xsl:otherwise>
1397         <xsl:call-template name="process-chunk-element"/>
1398       </xsl:otherwise>
1399     </xsl:choose>
1400   </xsl:template>
1401
1402   <!-- OVERRIDES xhtml-1_1/graphics.xsl   -->
1403   <!-- Do _NOT_ output any xlink garbage, so if you don't have 
1404        processor with extensions, you're screwed and we're terminating -->
1405   <xsl:template match="inlinegraphic">
1406     <xsl:variable name="filename">
1407       <xsl:choose>
1408         <xsl:when test="@entityref">
1409           <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1410         </xsl:when>
1411         <xsl:otherwise>
1412           <xsl:apply-templates select="@fileref"/>
1413         </xsl:otherwise>
1414       </xsl:choose>
1415     </xsl:variable>
1416
1417     <xsl:call-template name="anchor"/>
1418
1419     <xsl:choose>
1420       <xsl:when test="@format='linespecific'">
1421         <xsl:choose>
1422           <xsl:when test="$use.extensions != '0'                         and $textinsert.extension != '0'">
1423             <xsl:choose>
1424               <xsl:when test="element-available('stext:insertfile')">
1425                 <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
1426               </xsl:when>
1427               <xsl:when test="element-available('xtext:insertfile')">
1428                 <xtext:insertfile href="{$filename}"/>
1429               </xsl:when>
1430               <xsl:otherwise>
1431                 <xsl:message terminate="yes">
1432                   <xsl:text>No insertfile extension available.</xsl:text>
1433                 </xsl:message>
1434               </xsl:otherwise>
1435             </xsl:choose>
1436           </xsl:when>
1437           <xsl:otherwise>
1438             <!-- AN OVERRIDE --> 
1439             <xsl:message terminate="yes">
1440               <xsl:text>No insertfile extension available. Use a different processor (with extensions) or turn on $use.extensions and $textinsert.extension (see docs for more).  </xsl:text>
1441             </xsl:message>
1442             <!-- END OF OVERRIDE --> 
1443           </xsl:otherwise>
1444         </xsl:choose>
1445       </xsl:when>
1446       <xsl:otherwise>
1447         <xsl:call-template name="process.image"/>
1448       </xsl:otherwise>
1449     </xsl:choose>
1450   </xsl:template>  
1451
1452   <xsl:template name="cover">
1453     <xsl:apply-templates select="/*/*[contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"/>
1454   </xsl:template>  
1455
1456   <xsl:template match="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]">
1457     <xsl:call-template name="write.chunk">
1458       <xsl:with-param name="filename">
1459         <xsl:value-of select="$epub.cover.filename" />
1460       </xsl:with-param>
1461       <xsl:with-param name="method" select="'xml'" />
1462       <xsl:with-param name="encoding" select="'utf-8'" />
1463       <xsl:with-param name="indent" select="'no'" />
1464       <xsl:with-param name="quiet" select="$chunk.quietly" />
1465       <xsl:with-param name="content">
1466         <xsl:element namespace="http://www.w3.org/1999/xhtml" name="html">
1467           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="head">
1468             <xsl:element namespace="http://www.w3.org/1999/xhtml" name="title">Cover</xsl:element>
1469             <xsl:element namespace="http://www.w3.org/1999/xhtml" name="style">
1470               <xsl:attribute name="type">text/css</xsl:attribute>
1471               <!-- Help the cover image scale nicely in the CSS then apply a max-width to look better in Adobe Digital Editions -->
1472               <xsl:text> img { max-width: 100%; }</xsl:text>
1473             </xsl:element>
1474           </xsl:element>
1475           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="body">
1476             <xsl:element namespace="http://www.w3.org/1999/xhtml" name="div">
1477               <xsl:attribute name="id">
1478                 <xsl:value-of select="$epub.cover.image.id"/>
1479               </xsl:attribute>
1480               <xsl:choose>
1481                 <xsl:when test="imageobject[@role='front-large']">
1482                   <xsl:apply-templates select="imageobject[@role='front-large']"/>
1483                 </xsl:when>
1484                 <xsl:otherwise>
1485                   <xsl:apply-templates select="imageobject[1]"/>
1486                 </xsl:otherwise>
1487               </xsl:choose>
1488             </xsl:element>
1489             <!-- If this is defined as an explicit cover page, then process
1490             any remaining text -->
1491             <xsl:if test="ancestor::cover">
1492               <xsl:apply-templates select="ancestor::cover/para"/>
1493             </xsl:if>
1494           </xsl:element>
1495         </xsl:element>
1496       </xsl:with-param>  
1497     </xsl:call-template>  
1498   </xsl:template>
1499
1500   <xsl:template name="cover-svg">
1501     <xsl:param name="node"/>
1502   </xsl:template>
1503
1504   <xsl:template name="toc-href">
1505     <xsl:param name="node" select="."/>
1506     <xsl:apply-templates select="$node" mode="recursive-chunk-filename">
1507       <xsl:with-param name="recursive" select="true()"/>
1508     </xsl:apply-templates>
1509     <xsl:text>-toc</xsl:text>
1510     <xsl:value-of select="$html.ext"/>
1511   </xsl:template>
1512
1513   <xsl:template match="bibliodiv[title]" mode="label.markup">
1514   </xsl:template>
1515
1516   <xsl:template match="token" mode="opf.manifest.font">
1517     <xsl:call-template name="embedded-font-item">
1518       <xsl:with-param name="font.file" select="."/>
1519     </xsl:call-template>
1520   </xsl:template>
1521
1522   <xsl:template name="embedded-font-item">
1523     <xsl:param name="font.file"/>
1524     <xsl:param name="font.order" select="1"/>
1525
1526     <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
1527       <xsl:attribute name="id">
1528         <xsl:value-of select="concat('epub.embedded.font.', $font.order)"/>
1529       </xsl:attribute>
1530       <xsl:attribute name="href"><xsl:value-of select="$font.file"/></xsl:attribute>
1531       <xsl:choose>
1532         <xsl:when test="contains($font.file, 'otf')">
1533           <xsl:attribute name="media-type">font/opentype</xsl:attribute>
1534         </xsl:when>
1535         <xsl:otherwise>
1536           <xsl:message>
1537             <xsl:text>WARNING: OpenType fonts should be supplied! (</xsl:text>
1538             <xsl:value-of select="$font.file"/>
1539             <xsl:text>)</xsl:text>
1540           </xsl:message>
1541         </xsl:otherwise>  
1542       </xsl:choose>
1543     </xsl:element>
1544   </xsl:template>
1545
1546 <!-- Change section.heading to improve SEO on generated HTML by doing heading levels 
1547      "correctly". SEO rules are sometimes silly silly, but this does actually create 
1548      a semantic improvement.
1549      Note: This template needs to be manually maintained outside of the html/sections.xsl 
1550      code, so make sure important changes get reintegrated. -->
1551 <xsl:template name="section.heading">
1552   <xsl:param name="section" select="."/>
1553   <xsl:param name="level" select="1"/>
1554   <xsl:param name="allow-anchors" select="1"/>
1555   <xsl:param name="title"/>
1556   <xsl:param name="class" select="'title'"/>
1557
1558   <xsl:variable name="id">
1559     <xsl:choose>
1560       <!-- Make sure the subtitle doesn't get the same id as the title -->
1561       <xsl:when test="self::subtitle">
1562         <xsl:call-template name="object.id">
1563           <xsl:with-param name="object" select="."/>
1564         </xsl:call-template>
1565       </xsl:when>
1566       <!-- if title is in an *info wrapper, get the grandparent -->
1567       <xsl:when test="contains(local-name(..), 'info')">
1568         <xsl:call-template name="object.id">
1569           <xsl:with-param name="object" select="../.."/>
1570         </xsl:call-template>
1571       </xsl:when>
1572       <xsl:otherwise>
1573         <xsl:call-template name="object.id">
1574           <xsl:with-param name="object" select=".."/>
1575         </xsl:call-template>
1576       </xsl:otherwise>
1577     </xsl:choose>
1578   </xsl:variable>
1579
1580   <!-- For SEO, we try to actually ensure we *always* output one and only one h1,
1581        so unlike the regular stylesheets, we don't add one to the section level and
1582        we get the right behavior because of chunking. -->
1583   <xsl:variable name="hlevel">
1584     <xsl:choose>
1585       <!-- highest valid HTML H level is H6; so anything nested deeper
1586            than 7 levels down just becomes H6 -->
1587       <xsl:when test="$level &gt; 6">6</xsl:when>
1588       <xsl:otherwise>
1589         <xsl:value-of select="$level"/>
1590       </xsl:otherwise>
1591     </xsl:choose>
1592   </xsl:variable>
1593   <xsl:element name="h{$hlevel}" namespace="http://www.w3.org/1999/xhtml">
1594     <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
1595     <xsl:if test="$css.decoration != '0'">
1596       <xsl:if test="$hlevel&lt;3">
1597         <xsl:attribute name="style">clear: both</xsl:attribute>
1598       </xsl:if>
1599     </xsl:if>
1600     <xsl:if test="$allow-anchors != 0 and $generate.id.attributes = 0">
1601       <xsl:call-template name="anchor">
1602         <xsl:with-param name="node" select="$section"/>
1603         <xsl:with-param name="conditional" select="0"/>
1604       </xsl:call-template>
1605     </xsl:if>
1606     <xsl:if test="$generate.id.attributes != 0 and not(local-name(.) = 'appendix')">
1607       <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
1608     </xsl:if>
1609     <xsl:copy-of select="$title"/>
1610   </xsl:element>
1611 </xsl:template>
1612
1613 <!-- ==================================================================== -->
1614
1615 <xsl:template match="bridgehead">
1616   <xsl:variable name="container" select="(ancestor::appendix                         |ancestor::article                         |ancestor::bibliography                         |ancestor::chapter                         |ancestor::glossary                         |ancestor::glossdiv                         |ancestor::index                         |ancestor::partintro                         |ancestor::preface                         |ancestor::refsect1                         |ancestor::refsect2                         |ancestor::refsect3                         |ancestor::sect1                         |ancestor::sect2                         |ancestor::sect3                         |ancestor::sect4                         |ancestor::sect5                         |ancestor::section                         |ancestor::setindex                         |ancestor::simplesect)[last()]"/>
1617
1618   <xsl:variable name="clevel">
1619     <xsl:choose>
1620       <xsl:when test="local-name($container) = 'appendix'                       or local-name($container) = 'chapter'                       or local-name($container) = 'article'                       or local-name($container) = 'bibliography'                       or local-name($container) = 'glossary'                       or local-name($container) = 'index'                       or local-name($container) = 'partintro'                       or local-name($container) = 'preface'                       or local-name($container) = 'setindex'">1</xsl:when>
1621       <xsl:when test="local-name($container) = 'glossdiv'">
1622         <xsl:value-of select="count(ancestor::glossdiv)+1"/>
1623       </xsl:when>
1624       <xsl:when test="local-name($container) = 'sect1'                       or local-name($container) = 'sect2'                       or local-name($container) = 'sect3'                       or local-name($container) = 'sect4'                       or local-name($container) = 'sect5'                       or local-name($container) = 'refsect1'                       or local-name($container) = 'refsect2'                       or local-name($container) = 'refsect3'                       or local-name($container) = 'section'                       or local-name($container) = 'simplesect'">
1625         <xsl:variable name="slevel">
1626           <xsl:call-template name="section.level">
1627             <xsl:with-param name="node" select="$container"/>
1628           </xsl:call-template>
1629         </xsl:variable>
1630         <xsl:value-of select="$slevel + 1"/>
1631       </xsl:when>
1632       <xsl:otherwise>1</xsl:otherwise>
1633     </xsl:choose>
1634   </xsl:variable>
1635
1636   <!-- HTML H level is one higher than section level -->
1637   <xsl:variable name="hlevel">
1638     <xsl:choose>
1639       <xsl:when test="@renderas = 'sect1'">1</xsl:when>
1640       <xsl:when test="@renderas = 'sect2'">2</xsl:when>
1641       <xsl:when test="@renderas = 'sect3'">3</xsl:when>
1642       <xsl:when test="@renderas = 'sect4'">4</xsl:when>
1643       <xsl:when test="@renderas = 'sect5'">5</xsl:when>
1644       <xsl:otherwise>
1645         <xsl:value-of select="$clevel + 1"/>
1646       </xsl:otherwise>
1647     </xsl:choose>
1648   </xsl:variable>
1649
1650   <xsl:element name="h{$hlevel}" namespace="http://www.w3.org/1999/xhtml">
1651     <xsl:call-template name="anchor">
1652       <xsl:with-param name="conditional" select="0"/>
1653     </xsl:call-template>
1654     <xsl:apply-templates/>
1655   </xsl:element>
1656 </xsl:template>
1657
1658 <!-- SEO customization #2 -->
1659 <xsl:template name="component.title">
1660   <xsl:param name="node" select="."/>
1661
1662   <xsl:variable name="level">
1663     <xsl:choose>
1664       <xsl:when test="ancestor::section">
1665         <xsl:value-of select="count(ancestor::section)+1"/>
1666       </xsl:when>
1667       <xsl:when test="ancestor::sect5">6</xsl:when>
1668       <xsl:when test="ancestor::sect4">5</xsl:when>
1669       <xsl:when test="ancestor::sect3">4</xsl:when>
1670       <xsl:when test="ancestor::sect2">3</xsl:when>
1671       <xsl:when test="ancestor::sect1">2</xsl:when>
1672       <xsl:otherwise>1</xsl:otherwise>
1673     </xsl:choose>
1674   </xsl:variable>
1675
1676   <xsl:element name="h{$level}" namespace="http://www.w3.org/1999/xhtml">
1677     <xsl:attribute name="class">title</xsl:attribute>
1678     <xsl:if test="$generate.id.attributes = 0">
1679       <xsl:call-template name="anchor">
1680         <xsl:with-param name="node" select="$node"/>
1681         <xsl:with-param name="conditional" select="0"/>
1682       </xsl:call-template>
1683     </xsl:if>
1684       <xsl:apply-templates select="$node" mode="object.title.markup">
1685       <xsl:with-param name="allow-anchors" select="1"/>
1686     </xsl:apply-templates>
1687   </xsl:element>
1688 </xsl:template>
1689
1690 </xsl:stylesheet>