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