Imported Upstream version 1.78.1
[platform/upstream/docbook-xsl-stylesheets.git] / epub3 / epub3-element-mods.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE xsl:stylesheet [
3 <!ENTITY uppercase "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'">
4 <!ENTITY lowercase "'abcdefghijklmnopqrstuvwxyz'">
5 ]>
6
7 <xsl:stylesheet 
8   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
9   xmlns:exsl="http://exslt.org/common"
10   xmlns:set="http://exslt.org/sets"
11   xmlns="http://www.w3.org/1999/xhtml"
12   xmlns:epub="http://www.idpf.org/2007/ops"
13   xmlns:m="http://www.w3.org/1998/Math/MathML"
14   xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon"
15   xmlns:ssml="http://www.w3.org/2001/10/synthesis"
16   xmlns:svg="http://www.w3.org/2000/svg"
17   xmlns:opf="http://www.idpf.org/2007/opf"
18   xmlns:dc="http://purl.org/dc/elements/1.1/"  
19   xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"
20   xmlns:date="http://exslt.org/dates-and-times"
21   xmlns:dcterms="http://purl.org/dc/terms/"
22   xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/"
23   xmlns:db="http://docbook.org/ns/docbook"
24   xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
25   xmlns:str="http://exslt.org/strings"
26   xmlns:xtext="xalan://com.nwalsh.xalan.Text"
27
28   extension-element-prefixes="stext xtext"
29   exclude-result-prefixes="#default cf date db dc dcterms epub exsl m ncx opf pls set ssml stext str svg xtext"
30   version="1.0">
31
32 <!-- $Id: epub3-element-mods.xsl,v 1.1 2011-09-16 21:43:45 bobs Exp $ -->
33
34 <xsl:import href="titlepage.templates.xsl"/>
35
36 <!--
37 <xsl:key name="image-filerefs" match="graphic|inlinegraphic|imagedata" use="@fileref"/>
38 -->
39
40 <!--==============================================================-->
41 <!--  DocBook XSL Parameter settings                              -->
42 <!--==============================================================-->
43 <!-- separate file for toc -->
44 <xsl:param name="chunk.tocs.and.lots" select="1"/>
45 <xsl:param name="toc.section.depth" select="2"/>
46 <xsl:param name="generate.toc">
47 book  toc,title,figure,table,example,equation
48 article  toc,title,figure,table,example,equation
49 </xsl:param>
50 <xsl:param name="generate.manifest" select="0"/>
51 <xsl:param name="manifest.in.base.dir" select="1"/>
52 <xsl:param name="base.dir" select="'OEBPS/'"/>
53 <xsl:param name="index.links.to.section" select="0"/>
54
55 <!-- Epub does not yet support external links -->
56 <xsl:param name="activate.external.olinks" select="0"/>
57
58 <!-- Turning this on crashes ADE, which is unbelievably awesome -->
59 <xsl:param name="formal.object.break.after">0</xsl:param>
60
61 <xsl:param name="callout.graphics" select="1"/>
62 <xsl:param name="callout.graphics.extension">.png</xsl:param>
63 <xsl:param name="callout.graphics.number.limit" select="15"/>
64 <xsl:param name="callout.graphics.path" select="'images/callouts/'"/>
65 <xsl:param name="show.comments" select="0"/>
66
67 <!-- no navigation in .epub -->
68 <xsl:param name="suppress.navigation" select="'1'"/> 
69
70 <!-- EPUB3: use ol lists in table of contents -->
71 <xsl:param name="toc.list.type">ol</xsl:param>
72 <xsl:param name="autotoc.label.in.hyperlink" select="1"/>
73
74 <xsl:param name="css.decoration" select="1"/>
75 <!-- generate the css file from a source file -->
76 <xsl:param name="make.clean.html" select="1"/>
77 <!-- specify the default epub3 stylesheet -->
78 <xsl:param name="docbook.css.source">docbook-epub.css.xml</xsl:param>
79 <!-- for custom CSS, use the custom.css.source param -->
80 <xsl:param name="custom.css.source"></xsl:param>
81
82 <!--==============================================================-->
83 <!--  New EPUB3 Parameters                                        -->
84 <!--==============================================================-->
85 <xsl:param name="epub.version">3.0</xsl:param>
86 <!-- optional ncx for backwards compatibility -->
87 <xsl:param name="epub.include.ncx" select="1"/>
88 <xsl:param name="epub.ncx.depth">4</xsl:param> <!-- Not functional until http://code.google.com/p/epubcheck/issues/detail?id=70 is resolved -->
89 <!-- currently optional duplicate dcterms properties, may be required in future -->
90 <xsl:param name="epub.include.metadata.dcterms" select="1"/>
91 <!-- optional guide element for backwards compatibility -->
92 <xsl:param name="epub.include.guide" select="1"/>
93 <!-- some dc: currently required, to be replaced in future version -->
94 <xsl:param name="epub.include.metadata.dc.elements" select="1"/>
95 <!-- Some dc: elements will remain optional according to the spec -->
96 <xsl:param name="epub.include.optional.metadata.dc.elements" select="1"/>
97 <xsl:param name="epub.autolabel" select="0"/>
98 <xsl:param 
99   name="epub.vocabulary.profile.content">http://www.idpf.org/epub/30/profile/content/</xsl:param>
100 <xsl:param 
101   name="epub.vocabulary.profile.package">http://www.idpf.org/epub/30/profile/package/</xsl:param>
102 <xsl:param name="epub.output.epub.types" select="1"/>
103 <xsl:param name="epub.oebps.dir" select="'OEBPS'"/> 
104 <xsl:param name="epub.metainf.dir" select="'META-INF/'"/> 
105 <xsl:param name="epub.ncx.filename" select="'toc.ncx'"/> 
106 <xsl:param name="epub.mimetype.filename" select="'mimetype'"/> 
107 <xsl:param name="epub.mimetype.value" select="'application/epub+zip'"/> 
108 <xsl:param name="epub.container.filename" select="'container.xml'"/> 
109 <xsl:param name="epub.package.filename" select="'package.opf'"/> 
110 <xsl:param name="epub.cover.filename" select="concat('cover', $html.ext)"/> 
111 <xsl:param name="epub.cover.linear" select="0" />
112
113 <!-- names of id attributes used in package files -->
114 <xsl:param name="epub.meta.identifier.id">meta-identifier</xsl:param> 
115 <xsl:param name="epub.dc.identifier.id">pub-identifier</xsl:param> 
116 <xsl:param name="epub.meta.title.id">meta-title</xsl:param> 
117 <xsl:param name="epub.dc.title.id">pub-title</xsl:param> 
118 <xsl:param name="epub.meta.language.id">meta-language</xsl:param> 
119 <xsl:param name="epub.dc.language.id">pub-language</xsl:param> 
120 <xsl:param name="epub.meta.creator.id">meta-creator</xsl:param> 
121 <xsl:param name="epub.dc.creator.id">pub-creator</xsl:param> 
122 <xsl:param name="epub.ncx.toc.id">ncxtoc</xsl:param>
123 <xsl:param name="epub.ncx.manifest.id">ncx</xsl:param>
124 <xsl:param name="epub.ncx.mediatype">application/x-dtbncx+xml</xsl:param>
125 <xsl:param name="epub.xhtml.mediatype">application/xhtml+xml</xsl:param>
126 <xsl:param name="epub.html.toc.id">htmltoc</xsl:param>
127 <xsl:param name="epub.cover.filename.id" select="'cover'"/> 
128 <xsl:param name="epub.cover.image.id" select="'cover-image'"/> 
129
130 <xsl:param name="epub.embedded.fonts"></xsl:param>
131 <xsl:param name="epub.namespace">http://www.idpf.org/2007/ops</xsl:param>
132 <xsl:param name="opf.namespace">http://www.idpf.org/2007/opf</xsl:param>
133 <xsl:param name="ncx.namespace">http://www.daisy.org/z3986/2005/ncx/</xsl:param>
134 <xsl:param name="dc.namespace">http://purl.org/dc/elements/1.1/</xsl:param>
135 <!-- prefix generated ids in package elements so they differ from content ids -->
136 <xsl:param name="epub.package.id.prefix">id-</xsl:param>
137 <!-- editor is either a creator or contributor -->
138 <xsl:param name="editor.property">contributor</xsl:param> 
139
140 <!-- Generate full output path -->
141 <xsl:param name="epub.package.dir" select="concat($chunk.base.dir, '../')"/>
142
143 <xsl:param name="epub.ncx.pathname" 
144            select="concat($chunk.base.dir, $epub.ncx.filename)"/>
145 <xsl:param name="epub.container.pathname"
146            select="concat($epub.package.dir, $epub.metainf.dir, 
147            $epub.container.filename)"/>
148 <xsl:param name="epub.package.pathname"
149            select="concat($chunk.base.dir, $epub.package.filename)"/>
150 <xsl:param name="epub.cover.pathname"
151            select="concat($chunk.base.dir, $epub.cover.filename)"/>
152 <xsl:param name="epub.mimetype.pathname"
153            select="concat($epub.package.dir, $epub.mimetype.filename)"/>
154
155 <xsl:param name="kindle.extensions" select="0"/>
156
157 <!--==============================================================-->
158 <!--  Internal variables used for computing certain metadata      -->
159 <!--==============================================================-->
160 <xsl:variable name="epub3.chunk.hierarchy">
161   <xsl:apply-templates select="/*" mode="find.chunks"/>
162 </xsl:variable>
163
164 <xsl:variable name="chunkset" select="exsl:node-set($epub3.chunk.hierarchy)//cf:div"/>
165
166 <!--==============================================================-->
167 <!--  Template customizations                                     -->
168 <!--==============================================================-->
169
170 <!-- This is used only by ncx piece -->
171 <xsl:variable name="root.is.a.chunk">
172   <xsl:choose>
173     <xsl:when test="/*[not(self::book)][not(sect1) or not(section)]">
174       <xsl:text>1</xsl:text>
175     </xsl:when>
176     <xsl:when test="/book[*[last()][self::bookinfo]]|book[bookinfo]">
177       <xsl:text>1</xsl:text>
178     </xsl:when>
179     <xsl:when test="/book[*[last()][self::info]]|book[info]">
180       <xsl:text>1</xsl:text>
181     </xsl:when>
182     <xsl:when test="/bibliography">
183       <xsl:text>1</xsl:text>
184     </xsl:when>
185     <xsl:otherwise>
186       <xsl:text>0</xsl:text>
187     </xsl:otherwise>
188   </xsl:choose>
189 </xsl:variable>
190
191 <!-- EPUB3: Add generation of epub publication files -->
192 <xsl:template match="*" mode="process.root">
193   <xsl:variable name="doc" select="self::*"/>
194
195   <xsl:call-template name="user.preroot"/>
196   <xsl:call-template name="root.messages"/>
197
198   <html>
199     <head>
200       <xsl:call-template name="system.head.content">
201         <xsl:with-param name="node" select="$doc"/>
202       </xsl:call-template>
203       <xsl:call-template name="head.content">
204         <xsl:with-param name="node" select="$doc"/>
205       </xsl:call-template>
206       <xsl:call-template name="user.head.content">
207         <xsl:with-param name="node" select="$doc"/>
208       </xsl:call-template>
209     </head>
210     <body>
211       <xsl:call-template name="body.attributes"/>
212       <xsl:call-template name="user.header.content">
213         <xsl:with-param name="node" select="$doc"/>
214       </xsl:call-template>
215       <xsl:apply-templates select="."/>
216       <xsl:call-template name="user.footer.content">
217         <xsl:with-param name="node" select="$doc"/>
218       </xsl:call-template>
219     </body>
220   </html>
221   <xsl:value-of select="$html.append"/>
222   
223   <!-- Generate any css files only once, not once per chunk -->
224   <xsl:call-template name="generate.css.files"/>
225
226   <xsl:call-template name="generate.epub.files"/>
227
228 </xsl:template>
229
230 <xsl:template name="generate.epub.files">
231   <!-- Generate epub3 files -->
232   <xsl:message>
233     <xsl:text>Generating EPUB package files.</xsl:text>
234   </xsl:message>
235
236   <xsl:apply-templates select="." mode="opf"/>
237   <xsl:apply-templates select="." mode="container"/>
238   <xsl:call-template name="mimetype"/>
239   <xsl:if test="$epub.include.ncx != 0">
240     <xsl:call-template name="ncx"/>
241   </xsl:if>
242 </xsl:template>
243
244 <!-- Generate the package file -->
245 <xsl:template match="*" mode="opf">
246
247   <xsl:variable name="lang">
248     <xsl:call-template name="l10n.language"/>
249   </xsl:variable>
250
251   <xsl:variable name="content">
252     <!-- use xsl:element so output does not have a namespace prefix -->
253     <xsl:element name="package" namespace="{$opf.namespace}">
254       <!-- Add the package namespaces at the top -->
255       <xsl:call-template name="add.package.namespaces"/>
256
257       <xsl:attribute name="version">
258         <xsl:value-of select="$epub.version"/>
259       </xsl:attribute>
260       <xsl:attribute name="xml:lang">
261         <xsl:value-of select="$lang"/>
262       </xsl:attribute>
263       <!-- No profile att in 2011-09-06 spec 
264       <xsl:attribute name="profile">
265         <xsl:value-of select="$epub.vocabulary.profile.package"/>
266       </xsl:attribute>
267       -->
268       <xsl:attribute name="unique-identifier">
269         <xsl:value-of select="$epub.dc.identifier.id"/>
270       </xsl:attribute>
271
272
273       <xsl:call-template name="package.metadata"/>
274       <xsl:call-template name="package.manifest"/>
275       <xsl:call-template name="package.spine"/>
276
277       <xsl:if test="$epub.include.guide != 0">
278         <xsl:call-template name="package.guide"/>
279       </xsl:if>
280
281     </xsl:element>
282   </xsl:variable>
283
284   <xsl:call-template name="write.chunk">
285     <xsl:with-param name="filename">
286       <xsl:value-of select="$epub.package.pathname" />
287     </xsl:with-param>
288     <xsl:with-param name="content" select="$content"/>
289     <xsl:with-param name="method" select="'xml'" />
290     <xsl:with-param name="encoding" select="'utf-8'" />
291     <xsl:with-param name="indent" select="'yes'" />
292     <xsl:with-param name="quiet" select="$chunk.quietly" />
293     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
294     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
295   </xsl:call-template>
296 </xsl:template>
297
298 <xsl:template name="package.metadata">
299
300   <xsl:element name="metadata" namespace="{$opf.namespace}">
301
302     <xsl:call-template name="metadata.identifier"/>
303     <xsl:call-template name="metadata.title"/>
304     <xsl:call-template name="metadata.language"/>
305     <xsl:call-template name="metadata.modified"/>
306     <xsl:call-template name="metadata.cover"/>
307     <xsl:call-template name="metadata.other.info"/>
308     
309   </xsl:element>
310 </xsl:template>
311
312 <xsl:template name="doc.title">
313   <xsl:apply-templates select="." mode="title.markup"/>
314 </xsl:template>
315
316 <xsl:template name="metadata.identifier">
317   <xsl:variable name="package.id.value">
318     <xsl:call-template name="package-identifier"/>
319   </xsl:variable>
320
321   <xsl:if test="$epub.include.metadata.dc.elements != 0">
322     <!-- dc:identifier element -->
323     <dc:identifier>
324       <xsl:attribute name="id">
325         <xsl:value-of select="$epub.dc.identifier.id"/>
326       </xsl:attribute>
327       <!--
328       <xsl:attribute name="prefer">
329         <xsl:value-of select="$epub.meta.identifier.id"/>
330       </xsl:attribute>
331       -->
332       <xsl:copy-of select="$package.id.value"/>
333     </dc:identifier>
334   </xsl:if>
335
336   <xsl:if test="$epub.include.metadata.dcterms != 0">
337     <!-- equivalent meta identifier element -->
338     <xsl:element name="meta" namespace="{$opf.namespace}">
339       <xsl:attribute name="id">
340         <xsl:value-of select="$epub.meta.identifier.id"/>
341       </xsl:attribute>
342       <xsl:attribute name="property">dcterms:identifier</xsl:attribute>
343       <xsl:copy-of select="$package.id.value"/>
344     </xsl:element>
345   </xsl:if>
346 </xsl:template>
347
348 <xsl:template name="metadata.title">
349   <xsl:variable name="doc.title">
350     <xsl:call-template name="doc.title"/>
351   </xsl:variable>
352
353   <xsl:if test="$epub.include.metadata.dc.elements != 0">
354     <dc:title>
355       <xsl:attribute name="id">
356         <xsl:value-of select="$epub.dc.title.id"/>
357       </xsl:attribute>
358       <!--
359       <xsl:attribute name="prefer">
360         <xsl:value-of select="$epub.meta.title.id"/>
361       </xsl:attribute>
362       -->
363       <xsl:value-of select="normalize-space($doc.title)"/>
364     </dc:title>
365   </xsl:if>
366
367   <xsl:if test="$epub.include.metadata.dcterms != 0">
368   <!-- equivalent meta title element -->
369     <xsl:element name="meta" namespace="{$opf.namespace}">
370       <xsl:attribute name="property">dcterms:title</xsl:attribute>
371       <xsl:attribute name="id">
372         <xsl:value-of select="$epub.meta.title.id"/>
373       </xsl:attribute>
374       <xsl:value-of select="normalize-space($doc.title)"/>
375     </xsl:element>
376   </xsl:if>
377 </xsl:template>
378
379 <xsl:template name="metadata.language">
380   <xsl:variable name="lang">
381     <xsl:call-template name="l10n.language"/>
382   </xsl:variable>
383
384   <xsl:if test="$epub.include.metadata.dc.elements != 0">
385     <dc:language>
386       <xsl:if test="$kindle.extensions = 0">
387         <xsl:attribute name="id">
388           <xsl:value-of select="$epub.dc.language.id"/>
389         </xsl:attribute>
390       </xsl:if>
391       <xsl:value-of select="$lang"/>
392     </dc:language>
393   </xsl:if>
394
395   <xsl:if test="$epub.include.metadata.dcterms != 0">
396   <!-- equivalent meta lang element -->
397     <xsl:element name="meta" namespace="{$opf.namespace}">
398       <xsl:attribute name="property">dcterms:language</xsl:attribute>
399       <xsl:attribute name="id">
400         <xsl:value-of select="$epub.meta.language.id"/>
401       </xsl:attribute>
402       <xsl:value-of select="$lang"/>
403     </xsl:element>
404   </xsl:if>
405 </xsl:template>
406   
407 <xsl:template name="metadata.modified">
408   <xsl:variable name="local.datetime" select="date:date-time()"/>
409   <xsl:variable name="utc.datetime">
410     <xsl:call-template name="convert.date.to.utc">
411       <xsl:with-param name="date" select="$local.datetime"/>
412     </xsl:call-template>
413   </xsl:variable>
414   <xsl:choose>
415     <xsl:when test="string-length($utc.datetime) != 0">
416       <xsl:element name="meta" namespace="{$opf.namespace}">
417         <xsl:attribute name="property">dcterms:modified</xsl:attribute>
418         <xsl:value-of select="$utc.datetime"/>
419       </xsl:element>
420       <xsl:comment>The preceding date value is actually local time (not UTC) in UTC format because there is no function in XSLT 1.0 to generate a correct UTC time</xsl:comment>
421     </xsl:when>
422     <xsl:otherwise>
423       <xsl:message terminate="yes">
424         <xsl:text>ERROR: no last-modified date value could be determined, </xsl:text>
425         <xsl:text>so cannot output required meta element with </xsl:text>
426         <xsl:text>dcterms:modified attribute. Exiting.</xsl:text>
427       </xsl:message>
428     </xsl:otherwise>
429   </xsl:choose>
430 </xsl:template>
431
432 <xsl:template name="convert.date.to.utc">
433   <xsl:param name="date" select="''"/>
434   <!-- input format is YYYY-MM-DDTHH:MM:SS-X:00
435      where -X:00 is the offset from UTC. -->
436
437   <!-- output format is YYYY-MM-DDTHH:MM:SSZ with no offset -->
438   <!-- FIX ME:  Not so easy without a proper UTC date function. -->
439   <!-- Currently it just converts the local time to this format, which is
440        not the correct UTC time. -->
441   <xsl:value-of select="concat(substring($date,1,19), 'Z')"/>
442 </xsl:template>
443
444 <!-- This cover meta element used by kindlegen, at least -->
445 <xsl:template name="metadata.cover">
446   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
447   <xsl:variable name="cover.image" 
448                 select="$info//mediaobject[@role='cover' or ancestor::cover]"/>
449
450   <xsl:if test="$cover.image">
451     <xsl:element name="meta" namespace="{$opf.namespace}">
452       <xsl:attribute name="content">
453         <xsl:value-of select="$epub.cover.image.id"/>
454       </xsl:attribute>
455       <xsl:attribute name="name">cover</xsl:attribute>
456     </xsl:element>
457   </xsl:if>
458 </xsl:template>
459
460 <xsl:template name="metadata.other.info">
461   <!-- Take info relative to selected root element -->
462   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
463
464   <xsl:apply-templates select="$info/*" mode="opf.metadata"/>        
465 </xsl:template>
466
467 <xsl:template match="*" mode="opf.metadata">
468   <!-- default is no output -->
469 </xsl:template>
470
471 <xsl:template match="authorgroup" mode="opf.metadata">
472   <xsl:apply-templates select="*" mode="opf.metadata"/>
473 </xsl:template>
474
475 <xsl:template match="author|corpauthor" mode="opf.metadata">
476   <xsl:variable name="n">
477     <xsl:choose>
478       <xsl:when test="self::corpauthor">
479         <xsl:apply-templates/>
480       </xsl:when>
481       <xsl:when test="org/orgname">
482         <xsl:apply-templates select="org/orgname"/>
483       </xsl:when>
484       <xsl:when test="orgname">
485         <xsl:apply-templates select="orgname"/>
486       </xsl:when>
487       <xsl:otherwise>
488         <xsl:call-template name="person.name">
489           <xsl:with-param name="node" select="."/>
490         </xsl:call-template>
491       </xsl:otherwise>
492     </xsl:choose>
493   </xsl:variable>
494
495   <xsl:if test="string-length($n) != 0">
496     <xsl:element name="meta" namespace="{$opf.namespace}">
497       <xsl:attribute name="id">
498         <xsl:value-of select="concat($epub.meta.creator.id, position())"/>
499       </xsl:attribute>
500       <xsl:attribute name="property">dcterms:creator</xsl:attribute>
501       <xsl:value-of select="normalize-space(string($n))"/>
502     </xsl:element>
503
504     <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
505       <dc:creator>
506         <xsl:attribute name="id">
507           <xsl:value-of select="concat($epub.dc.creator.id, position())"/>
508         </xsl:attribute>
509         <xsl:value-of select="$n"/>
510       </dc:creator>
511     </xsl:if>
512   </xsl:if>
513 </xsl:template>
514
515 <xsl:template match="editor" mode="opf.metadata">
516   <xsl:variable name="n">
517     <xsl:choose>
518       <xsl:when test="orgname">
519         <xsl:apply-templates select="orgname"/>
520       </xsl:when>
521       <xsl:otherwise>
522         <xsl:call-template name="person.name">
523           <xsl:with-param name="node" select="."/>
524         </xsl:call-template>
525       </xsl:otherwise>
526     </xsl:choose>
527   </xsl:variable>
528
529   <xsl:variable name="name">
530     <xsl:choose>
531       <xsl:when test="string-length($editor.property) != 0">
532         <xsl:value-of select="$editor.property"/>
533       </xsl:when>
534       <xsl:otherwise>
535         <xsl:text>contributor</xsl:text>
536       </xsl:otherwise>
537     </xsl:choose>
538   </xsl:variable>
539
540   <xsl:element name="meta" namespace="{$opf.namespace}">
541     <xsl:attribute name="property">
542       <xsl:text>dcterms:</xsl:text>
543       <xsl:value-of select="$name"/>
544     </xsl:attribute>
545     <xsl:value-of select="normalize-space($n)"/>
546   </xsl:element>
547
548   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
549     <xsl:choose>
550       <xsl:when test="$name = 'creator'">
551         <dc:creator>
552           <xsl:value-of select="normalize-space($n)"/>
553         </dc:creator>
554       </xsl:when>
555       <xsl:when test="$name = 'contributor'">
556         <dc:contributor>
557           <xsl:value-of select="normalize-space($n)"/>
558         </dc:contributor>
559       </xsl:when>
560       <xsl:otherwise>
561         <xsl:element namespace="{$dc.namespace}" name="{$name}">
562           <xsl:value-of select="normalize-space($n)"/>
563         </xsl:element>
564       </xsl:otherwise>
565     </xsl:choose>
566   </xsl:if>
567
568 </xsl:template>
569
570 <xsl:template match="corpcredit" mode="opf.metadata">
571   <xsl:element name="meta" namespace="{$opf.namespace}">
572     <xsl:attribute name="property">dcterms:contributor</xsl:attribute>
573     <xsl:value-of select="normalize-space(.)"/>
574   </xsl:element>
575
576   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
577     <dc:contributor>
578       <xsl:value-of select="normalize-space(.)"/>
579     </dc:contributor>
580   </xsl:if>
581 </xsl:template>
582
583 <xsl:template match="collab|othercredit" mode="opf.metadata">
584   <xsl:variable name="content">
585     <xsl:choose>
586       <xsl:when test="collabname">
587         <xsl:apply-templates select="collabname"/>
588       </xsl:when>
589       <xsl:when test="org/orgname">
590         <xsl:apply-templates select="org/orgname"/>
591       </xsl:when>
592       <xsl:when test="orgname">
593         <xsl:apply-templates select="orgname"/>
594       </xsl:when>
595       <xsl:when test="personname|firstname|surname|othername">
596         <xsl:call-template name="person.name"/>
597       </xsl:when>
598       <xsl:otherwise>
599         <xsl:value-of select="."/>
600       </xsl:otherwise>
601     </xsl:choose>
602   </xsl:variable>
603
604   <xsl:element name="meta" namespace="{$opf.namespace}">
605     <xsl:attribute name="property">dcterms:contributor</xsl:attribute>
606     <xsl:value-of select="normalize-space($content)"/>
607   </xsl:element>
608
609   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
610     <dc:contributor>
611       <xsl:value-of select="normalize-space($content)"/>
612     </dc:contributor>
613   </xsl:if>
614
615 </xsl:template>
616
617 <xsl:template match="date|pubdate" mode="opf.metadata">
618   <xsl:variable name="date">
619     <xsl:call-template name="format.meta.date">
620       <xsl:with-param name="string" select="normalize-space(.)"/>
621     </xsl:call-template>
622   </xsl:variable>
623
624   <xsl:if test="string-length($date) != 0">
625     <xsl:element name="meta" namespace="{$opf.namespace}">
626       <xsl:attribute name="property">dcterms:date</xsl:attribute>
627       <xsl:value-of select="$date"/>
628     </xsl:element>
629   
630     <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
631       <dc:date>
632         <xsl:value-of select="$date"/>
633       </dc:date>
634     </xsl:if>
635   </xsl:if>
636
637 </xsl:template>
638
639 <!-- EPUB3 meta date should be of the form:
640   YYYY, YYYY-MM or YYYY-MM-DD -->
641 <xsl:template name="format.meta.date">
642   <xsl:param name="string" select="''"/>
643   <xsl:param name="node" select="."/>
644   
645   <!-- FIXME: this needs further work, so just check the
646   string format and return the date string for now -->
647   <xsl:variable name="normalized" 
648                 select="translate($string, '0123456789', '##########')"/>
649
650   <xsl:variable name="date.ok">
651     <xsl:choose>
652       <xsl:when test="string-length($string) = 4 and
653                       $normalized = '####'">1</xsl:when>
654       <xsl:when test="string-length($string) = 7 and
655                       $normalized = '####-##'">1</xsl:when>
656       <xsl:when test="string-length($string) = 10 and
657                       $normalized = '####-##-##'">1</xsl:when>
658       <xsl:when test="string-length($string) = 10 and
659                       $normalized = '####-##-##'">1</xsl:when>
660       <xsl:otherwise>0</xsl:otherwise>
661     </xsl:choose>
662   </xsl:variable>
663
664   <xsl:if test="$date.ok = 0">
665     <xsl:message>
666       <xsl:text>WARNING: wrong metadata date format: '</xsl:text>
667       <xsl:value-of select="$string"/>
668       <xsl:text>' in element </xsl:text>
669       <xsl:value-of select="local-name($node/..)"/>
670       <xsl:text>/</xsl:text>
671       <xsl:value-of select="local-name($node)"/>
672       <xsl:text>. It must be in one of these forms: </xsl:text>
673       <xsl:text>YYYY, YYYY-MM, or YYYY-MM-DD.</xsl:text>
674     </xsl:message>
675   </xsl:if>
676
677   <!-- return the string anyway -->
678   <xsl:value-of select="$string"/>
679
680 </xsl:template>
681
682
683 <!-- Space separate the compontents of the abstract (dropping the inline markup, sadly) -->
684 <xsl:template match="abstract" mode="opf.metadata">
685   <xsl:variable name="content">
686     <xsl:for-each select="formalpara|para|simpara|title">
687       <xsl:choose>
688         <xsl:when test="self::formalpara">
689           <xsl:value-of select="normalize-space(string(title))"/>
690           <xsl:text>: </xsl:text>
691           <xsl:value-of select="normalize-space(string(para))"/>
692         </xsl:when>
693         <xsl:otherwise>
694           <xsl:value-of select="normalize-space(string(.))"/>
695         </xsl:otherwise>  
696       </xsl:choose>
697       <xsl:if test="self::title">
698         <xsl:text>:</xsl:text>
699       </xsl:if>
700       <xsl:if test="not(position() = last())">
701         <xsl:text> </xsl:text>
702       </xsl:if>
703     </xsl:for-each>  
704   </xsl:variable>
705
706   <xsl:element name="meta" namespace="{$opf.namespace}">
707     <xsl:attribute name="property">dcterms:description</xsl:attribute>
708     <xsl:copy-of select="$content"/>
709   </xsl:element>
710
711   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
712     <dc:description>
713       <xsl:copy-of select="$content"/>
714     </dc:description>
715   </xsl:if>
716 </xsl:template>
717
718 <xsl:template match="subjectset" mode="opf.metadata">
719   <xsl:apply-templates select="subject/subjectterm" mode="opf.metadata"/>
720 </xsl:template>
721
722 <xsl:template match="subjectterm" mode="opf.metadata">
723   <xsl:element name="meta" namespace="{$opf.namespace}">
724     <xsl:attribute name="property">dcterms:subject</xsl:attribute>
725     <xsl:value-of select="normalize-space(string(.))"/>
726   </xsl:element>
727
728   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
729     <dc:subject>
730       <xsl:value-of select="normalize-space(string(.))"/>
731     </dc:subject>
732   </xsl:if>
733 </xsl:template>
734
735 <xsl:template match="keywordset" mode="opf.metadata">
736   <xsl:apply-templates select="keyword" mode="opf.metadata"/>
737 </xsl:template>
738
739 <xsl:template match="keyword" mode="opf.metadata">
740   <xsl:element name="meta" namespace="{$opf.namespace}">
741     <xsl:attribute name="property">dcterms:subject</xsl:attribute>
742     <xsl:value-of select="normalize-space(string(.))"/>
743   </xsl:element>
744
745   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
746     <dc:subject>
747       <xsl:value-of select="normalize-space(string(.))"/>
748     </dc:subject>
749   </xsl:if>
750 </xsl:template>
751
752 <xsl:template match="publisher" mode="opf.metadata">
753   <xsl:apply-templates select="publishername" mode="opf.metadata"/>
754 </xsl:template>
755
756 <xsl:template match="publishername" mode="opf.metadata">
757   <xsl:element name="meta" namespace="{$opf.namespace}">
758     <xsl:attribute name="property">dcterms:publisher</xsl:attribute>
759     <xsl:value-of select="normalize-space(string(.))"/>
760   </xsl:element>
761
762   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
763     <dc:publisher>
764       <xsl:value-of select="normalize-space(string(.))"/>
765     </dc:publisher>
766   </xsl:if>
767 </xsl:template>
768
769 <xsl:template match="bibliocoverage" mode="opf.metadata">
770   <xsl:element name="meta" namespace="{$opf.namespace}">
771     <xsl:attribute name="property">dcterms:coverage</xsl:attribute>
772     <xsl:value-of select="normalize-space(string(.))"/>
773   </xsl:element>
774
775   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
776     <dc:coverage>
777       <xsl:value-of select="normalize-space(string(.))"/>
778     </dc:coverage>
779   </xsl:if>
780 </xsl:template>
781
782 <xsl:template match="bibliorelation" mode="opf.metadata">
783   <xsl:element name="meta" namespace="{$opf.namespace}">
784     <xsl:attribute name="property">dcterms:relation</xsl:attribute>
785     <xsl:value-of select="normalize-space(string(.))"/>
786   </xsl:element>
787
788   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
789     <dc:relation>
790       <xsl:value-of select="normalize-space(string(.))"/>
791     </dc:relation>
792   </xsl:if>
793 </xsl:template>
794
795 <xsl:template match="bibliosource" mode="opf.metadata">
796   <xsl:element name="meta" namespace="{$opf.namespace}">
797     <xsl:attribute name="property">dcterms:source</xsl:attribute>
798     <xsl:value-of select="normalize-space(string(.))"/>
799   </xsl:element>
800
801   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
802     <dc:source>
803       <xsl:value-of select="normalize-space(string(.))"/>
804     </dc:source>
805   </xsl:if>
806 </xsl:template>
807
808 <xsl:template match="copyright" mode="opf.metadata">
809   <xsl:variable name="copyright.date">
810     <xsl:call-template name="copyright.years">
811       <xsl:with-param name="years" select="year"/>
812       <xsl:with-param name="print.ranges" select="$make.year.ranges"/>
813       <xsl:with-param name="single.year.ranges" select="$make.single.year.ranges"/>
814     </xsl:call-template>
815   </xsl:variable>
816
817   <!-- if no docbook date element, use copyright year for single date metadata -->
818   <xsl:if test="not(../date)">
819     <xsl:variable name="date.content">
820       <xsl:call-template name="format.meta.date">
821         <xsl:with-param name="string">
822           <xsl:call-template name="copyright.years">
823             <xsl:with-param name="years" select="year[last()]"/>
824             <xsl:with-param name="print.ranges" select="0"/>
825             <xsl:with-param name="single.year.ranges" select="0"/>
826           </xsl:call-template>
827         </xsl:with-param>
828       </xsl:call-template>
829     </xsl:variable>
830     <xsl:element name="meta" namespace="{$opf.namespace}">
831       <xsl:attribute name="property">dcterms:date</xsl:attribute>
832       <xsl:copy-of select="$date.content"/>
833     </xsl:element>
834     <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
835       <dc:date>
836         <xsl:copy-of select="$date.content"/>
837       </dc:date>
838     </xsl:if>
839   </xsl:if>
840
841   <xsl:variable name="rights.content">
842     <xsl:call-template name="gentext">
843       <xsl:with-param name="key" select="'Copyright'"/>
844     </xsl:call-template>
845     <xsl:call-template name="gentext.space"/>
846     <xsl:text>&#x00A9;</xsl:text>
847     <xsl:call-template name="gentext.space"/>
848     <xsl:value-of select="$copyright.date"/>
849     <xsl:call-template name="gentext.space"/>
850     <xsl:apply-templates select="holder" mode="titlepage.mode"/>
851   </xsl:variable>
852
853   <xsl:element name="meta" namespace="{$opf.namespace}">
854     <xsl:attribute name="property">dcterms:rights</xsl:attribute>
855     <xsl:copy-of select="$rights.content"/>
856   </xsl:element>
857   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
858     <dc:rights>
859       <xsl:copy-of select="$rights.content"/>
860     </dc:rights>
861   </xsl:if>
862
863   <xsl:element name="meta" namespace="{$opf.namespace}">
864     <xsl:attribute name="property">dcterms:rightsHolder</xsl:attribute>
865     <xsl:apply-templates select="holder" mode="titlepage.mode"/>
866   </xsl:element>
867 </xsl:template>
868
869 <xsl:template name="package.guide">
870
871   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
872
873   <xsl:variable name="toc.params">
874     <xsl:call-template name="find.path.params">
875       <xsl:with-param name="node" select="."/>
876       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
877     </xsl:call-template>
878   </xsl:variable>
879
880   <xsl:if test="contains($toc.params, 'toc') or 
881                 $info/cover or 
882                 $info//mediaobject[@role='cover' or ancestor::cover]"> 
883     <xsl:element namespace="{$opf.namespace}" name="guide">
884       <xsl:if test="$info/cover or 
885                     $info//mediaobject[@role='cover' or ancestor::cover]"> 
886         <xsl:element namespace="{$opf.namespace}" name="reference">
887           <xsl:attribute name="href">
888             <xsl:value-of select="$epub.cover.filename" />
889           </xsl:attribute>
890           <xsl:attribute name="type">cover</xsl:attribute>
891           <xsl:attribute name="title">Cover</xsl:attribute>
892         </xsl:element>
893       </xsl:if>  
894
895       <xsl:if test="contains($toc.params, 'toc')">
896         <xsl:element namespace="{$opf.namespace}" name="reference">
897           <xsl:attribute name="href">
898             <xsl:call-template name="toc-href">
899               <xsl:with-param name="node" select="."/>
900             </xsl:call-template>
901           </xsl:attribute>
902           <xsl:attribute name="type">toc</xsl:attribute>
903           <xsl:attribute name="title">Table of Contents</xsl:attribute>
904         </xsl:element>
905       </xsl:if>  
906     </xsl:element>  
907   </xsl:if>  
908 </xsl:template>
909
910
911 <xsl:template name="package-identifier">  
912
913   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
914
915   <xsl:choose>
916     <xsl:when test="$info/biblioid">
917       <xsl:if test="$info/biblioid[1][@class = 'doi' or 
918                                       @class = 'isbn' or
919                                       @class = 'isrn' or
920                                       @class = 'istc' or
921                                       @class = 'issn']">
922         <xsl:text>urn:</xsl:text>
923         <xsl:value-of select="$info/biblioid[1]/@class"/>
924         <xsl:text>:</xsl:text>
925       </xsl:if>
926       <xsl:value-of select="normalize-space($info/biblioid[1])"/>
927     </xsl:when>
928     <xsl:when test="$info/isbn">
929       <xsl:text>urn:isbn:</xsl:text>
930       <xsl:value-of select="$info/isbn[1]"/>
931     </xsl:when>
932     <xsl:when test="$info/issn">
933       <xsl:text>urn:issn:</xsl:text>
934       <xsl:value-of select="$info/issn[1]"/>
935     </xsl:when>
936     <xsl:otherwise>
937       <xsl:choose>
938         <xsl:when test="$info/invpartnumber">
939           <xsl:value-of select="$info/invpartnumber[1]"/>
940         </xsl:when>
941         <xsl:when test="$info/issuenum">
942           <xsl:value-of select="$info/issuenum[1]"/>
943         </xsl:when>
944         <xsl:when test="$info/productnumber">
945           <xsl:value-of select="$info/productnumber[1]"/>
946         </xsl:when>
947         <xsl:when test="$info/seriesvolnums">
948           <xsl:value-of select="$info/seriesvolnums[1]"/>
949         </xsl:when>
950         <xsl:when test="$info/volumenum">
951           <xsl:value-of select="$info/volumenum[1]"/>
952         </xsl:when>
953         <!-- Deprecated -->
954         <xsl:when test="$info/pubsnumber">
955           <xsl:value-of select="$info/pubsnumber[1]"/>
956         </xsl:when>
957       </xsl:choose>  
958       <xsl:text>_</xsl:text>
959       <xsl:choose>
960         <xsl:when test="@id">
961           <xsl:value-of select="@id"/>
962         </xsl:when>
963         <xsl:when test="@xml:id">
964           <xsl:value-of select="@xml:id"/>
965         </xsl:when>
966         <xsl:otherwise>
967           <!-- TODO: Do UUIDs here -->
968           <xsl:value-of select="generate-id(.)"/>
969         </xsl:otherwise>
970       </xsl:choose>
971     </xsl:otherwise>
972   </xsl:choose>
973 </xsl:template>
974
975 <!-- EPUB3: add epub:type attribute where appropriate -->
976 <xsl:template match="*" mode="common.html.attributes">
977   <xsl:param name="class" select="local-name(.)"/>
978   <xsl:param name="inherit" select="0"/>
979   <xsl:call-template name="generate.html.lang"/>
980   <xsl:call-template name="dir">
981     <xsl:with-param name="inherit" select="$inherit"/>
982   </xsl:call-template>
983   <xsl:apply-templates select="." mode="class.attribute">
984     <xsl:with-param name="class" select="$class"/>
985   </xsl:apply-templates>
986   <xsl:call-template name="generate.html.title"/>
987   <xsl:apply-templates select="." mode="epub.type"/>
988 </xsl:template>
989
990 <xsl:template match="*" mode="epub.type" priority="-1"/>
991
992 <xsl:template match="chapter
993                     |appendix
994                     |epigraph
995                     |warning
996                     |preface
997                     |index
998                     |colophon
999                     |glossary
1000                     |biblioentry
1001                     |bibliography
1002                     |dedication
1003                     |sidebar
1004                     |footnote
1005                     |glossterm
1006                     |glossdef
1007                     |bridgehead
1008                     |part" mode="epub.type">
1009   <xsl:variable name="type" select="local-name()"/>
1010
1011   <xsl:if test="$epub.output.epub.types != 0">
1012     <xsl:attribute name="epub:type">
1013       <xsl:value-of select="$type"/>
1014     </xsl:attribute>
1015   </xsl:if>
1016 </xsl:template>
1017
1018 <xsl:template match="section[parent::chapter] | sect1" mode="epub.type">
1019   <xsl:if test="$epub.output.epub.types != 0">
1020     <xsl:attribute name="epub:type">subchapter</xsl:attribute>
1021   </xsl:if>
1022 </xsl:template>
1023
1024 <xsl:template match="section[not(parent::chapter)] |
1025                      sect2 |
1026                      sect3 |
1027                      sect4 |
1028                      sect5 |
1029                      sect6" mode="epub.type">
1030   <xsl:if test="$epub.output.epub.types != 0">
1031     <xsl:attribute name="epub:type">division</xsl:attribute>
1032   </xsl:if>
1033 </xsl:template>
1034
1035 <xsl:template match="note|tip|caution|important" mode="epub.type">
1036   <xsl:if test="$epub.output.epub.types != 0">
1037     <xsl:attribute name="epub:type">notice</xsl:attribute>
1038   </xsl:if>
1039 </xsl:template>
1040
1041 <xsl:template match="orderedlist|itemizedlist|variablelist|simplelist" mode="epub.type">
1042   <xsl:if test="$epub.output.epub.types != 0">
1043     <xsl:attribute name="epub:type">list</xsl:attribute>
1044   </xsl:if>
1045 </xsl:template>
1046
1047 <xsl:template match="listitem" mode="epub.type">
1048   <xsl:if test="$epub.output.epub.types != 0">
1049     <xsl:attribute name="epub:type">list-item</xsl:attribute>
1050   </xsl:if>
1051 </xsl:template>
1052
1053 <!-- EPUB3: to add attributes to root output element -->
1054 <xsl:template name="root.attributes">
1055   <!-- collect and output all namespace declarations -->
1056   <xsl:call-template name="all.namespaces"/>
1057 </xsl:template>
1058
1059 <xsl:template name="all.namespaces">
1060   <!-- add the epub3 namespaces to the top output element -->
1061   <xsl:variable name="temp">
1062     <epub:foo/>
1063     <m:foo/>
1064     <pls:foo/>
1065     <ssml:foo/>
1066     <svg:foo/>
1067   </xsl:variable>
1068
1069   <xsl:variable name="nodes" select="exsl:node-set($temp)"/>
1070   <xsl:for-each select="$nodes//*/namespace::*">
1071     <xsl:copy-of select="."/>
1072   </xsl:for-each>
1073
1074 </xsl:template>
1075
1076 <xsl:template name="add.package.namespaces">
1077   <!-- add the epub3 package namespaces to the top output element -->
1078   <xsl:variable name="temp">
1079     <dc:foo/>
1080     <dcterms:foo/>
1081   </xsl:variable>
1082
1083   <xsl:variable name="nodes" select="exsl:node-set($temp)"/>
1084   <xsl:for-each select="$nodes//*[local-name(.) ='foo']/namespace::*">
1085     <xsl:if test="contains(., 'purl.org')">
1086       <xsl:copy-of select="."/>
1087     </xsl:if>
1088   </xsl:for-each>
1089
1090 </xsl:template>
1091
1092
1093 <xsl:template name="footnotes.attributes">
1094   <xsl:if test="$epub.output.epub.types != 0">
1095     <xsl:attribute name="epub:type">footnotes</xsl:attribute>
1096   </xsl:if>
1097 </xsl:template>
1098
1099 <xsl:template name="package.manifest">
1100   <xsl:element name="manifest" namespace="{$opf.namespace}">
1101     <xsl:if test="$epub.include.ncx != 0">
1102       <xsl:call-template name="manifest.ncx"/>
1103     </xsl:if>
1104     <xsl:call-template name="manifest.fonts"/>
1105     <xsl:call-template name="manifest.toc"/>
1106     <xsl:call-template name="manifest.css"/>
1107     <xsl:call-template name="manifest.cover"/>
1108     <xsl:call-template name="manifest.other.items"/>
1109     <xsl:call-template name="manifest.content.items"/>
1110     <xsl:call-template name="user.manifest.items"/>
1111   </xsl:element>
1112 </xsl:template>
1113
1114 <xsl:template name="user.manifest.items"/>
1115
1116 <xsl:template name="manifest.css">
1117   <xsl:if test="$html.stylesheet != ''">
1118     <xsl:call-template name="css.item">
1119       <xsl:with-param name="stylesheets" select="$html.stylesheet"/>
1120     </xsl:call-template>
1121   </xsl:if>
1122   <xsl:if test="string-length($docbook.css.source) != 0">
1123     <xsl:variable name="dfilename">
1124       <xsl:call-template name="css.output.filename">
1125         <xsl:with-param name="src" select="$docbook.css.source"/>
1126       </xsl:call-template>
1127     </xsl:variable>
1128     <xsl:element namespace="{$opf.namespace}" name="item">
1129       <xsl:attribute name="media-type">text/css</xsl:attribute>
1130       <xsl:attribute name="id">docbook-css</xsl:attribute>
1131       <xsl:attribute name="href">
1132         <xsl:value-of select="$dfilename"/>
1133       </xsl:attribute>
1134     </xsl:element>
1135   </xsl:if>
1136   <xsl:if test="string-length($custom.css.source) != 0">
1137     <xsl:variable name="cfilename">
1138       <xsl:call-template name="css.output.filename">
1139         <xsl:with-param name="src" select="$custom.css.source"/>
1140       </xsl:call-template>
1141     </xsl:variable>
1142     <xsl:element namespace="{$opf.namespace}" name="item">
1143       <xsl:attribute name="media-type">text/css</xsl:attribute>
1144       <xsl:attribute name="id">custom-css</xsl:attribute>
1145       <xsl:attribute name="href">
1146         <xsl:value-of select="$cfilename"/>
1147       </xsl:attribute>
1148     </xsl:element>
1149   </xsl:if>
1150 </xsl:template>
1151
1152 <xsl:template name="css.item">
1153   <xsl:param name="stylesheets" select="''"/>
1154   <xsl:param name="count" select="1"/>
1155
1156   <xsl:choose>
1157     <xsl:when test="contains($stylesheets, ' ')">
1158       <xsl:variable name="css.filename" select="substring-before($stylesheets, ' ')"/>
1159       <xsl:if test="$css.filename != ''">
1160         <xsl:element namespace="{$opf.namespace}" name="item">
1161           <xsl:attribute name="media-type">text/css</xsl:attribute>
1162           <xsl:attribute name="id">
1163             <xsl:text>html-css</xsl:text>
1164             <xsl:if test="$count &gt; 1">
1165               <xsl:value-of select="$count"/>
1166             </xsl:if>
1167           </xsl:attribute>
1168           <xsl:attribute name="href">
1169             <xsl:value-of select="$css.filename"/>
1170           </xsl:attribute>
1171         </xsl:element>
1172       </xsl:if>
1173
1174       <xsl:call-template name="css.item">
1175         <xsl:with-param name="stylesheets" select="substring-after($stylesheets, ' ')"/>
1176         <xsl:with-param name="count" select="$count + 1"/>
1177       </xsl:call-template>
1178     </xsl:when>
1179     <xsl:otherwise>
1180       <xsl:if test="$stylesheets != ''">
1181         <xsl:element namespace="{$opf.namespace}" name="item">
1182           <xsl:attribute name="media-type">text/css</xsl:attribute>
1183           <xsl:attribute name="id">
1184             <xsl:text>html-css</xsl:text>
1185             <xsl:if test="$count &gt; 1">
1186               <xsl:value-of select="$count"/>
1187             </xsl:if>
1188           </xsl:attribute>
1189           <xsl:attribute name="href">
1190             <xsl:value-of select="$stylesheets"/>
1191           </xsl:attribute>
1192         </xsl:element>
1193       </xsl:if>
1194     </xsl:otherwise>
1195   </xsl:choose>
1196 </xsl:template>
1197
1198 <xsl:template name="manifest.ncx">
1199   <xsl:element name="item" namespace="{$opf.namespace}">
1200     <xsl:attribute name="id">
1201       <xsl:value-of select="$epub.ncx.manifest.id"/>
1202     </xsl:attribute>
1203     <xsl:attribute name="href">
1204       <xsl:value-of select="$epub.ncx.filename"/>
1205     </xsl:attribute>
1206     <xsl:attribute name="media-type">
1207       <xsl:value-of select="$epub.ncx.mediatype"/>
1208     </xsl:attribute>
1209   </xsl:element>
1210 </xsl:template>
1211
1212 <xsl:template name="manifest.fonts"/>
1213
1214 <!--Misc items in the manifest based on content -->
1215 <xsl:template name="manifest.other.items">
1216 </xsl:template>
1217
1218
1219 <xsl:template name="manifest.cover">
1220   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
1221   <xsl:variable name="cover.image" 
1222                 select="$info//mediaobject[@role='cover' or ancestor::cover]"/>
1223
1224   <xsl:if test="$cover.image">
1225
1226     <!-- generate the manifest link to that page -->
1227     <xsl:variable name="olist" select="$cover.image/imageobject|$cover.image/imageobjectco
1228                        |$cover.image/videoobject|$cover.image/audioobject
1229                        |$cover.image/textobject"/>
1230   
1231     <xsl:variable name="object.index">
1232       <xsl:call-template name="select.mediaobject.index">
1233         <xsl:with-param name="olist" select="$olist"/>
1234         <xsl:with-param name="count" select="1"/>
1235       </xsl:call-template>
1236     </xsl:variable>
1237   
1238     <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1239
1240     <xsl:variable name="output_filename">
1241       <xsl:call-template name="mediaobject.filename">
1242         <xsl:with-param name="object" select="$object"/>
1243       </xsl:call-template>
1244     </xsl:variable>
1245
1246     <xsl:variable name="image.filename">
1247        <xsl:if test="$img.src.path != '' and
1248                        not(starts-with($output_filename, '/')) and
1249                        not(contains($output_filename, '://'))">
1250          <xsl:value-of select="$img.src.path"/>
1251        </xsl:if>
1252        <xsl:value-of select="$output_filename"/>
1253     </xsl:variable>
1254     <xsl:variable name="image.extension">
1255       <xsl:call-template name="filename-extension">
1256         <xsl:with-param name="filename" select="$image.filename"/>
1257       </xsl:call-template>
1258     </xsl:variable>
1259
1260     <xsl:variable name="image.type">
1261       <xsl:call-template name="graphic.format.content-type">
1262         <xsl:with-param name="format" select="translate($image.extension, 
1263                    &lowercase;, &uppercase;)"/>
1264       </xsl:call-template>
1265     </xsl:variable>
1266
1267     <!-- Reference to cover html file -->
1268     <xsl:element namespace="{$opf.namespace}" name="item">
1269       <xsl:attribute name="id">
1270         <xsl:value-of select="$epub.cover.filename.id"/>
1271       </xsl:attribute>
1272       <xsl:attribute name="href">
1273         <xsl:value-of select="$epub.cover.filename"/>
1274       </xsl:attribute>
1275       <xsl:attribute name="media-type">
1276         <xsl:value-of select="$epub.xhtml.mediatype"/>
1277       </xsl:attribute>
1278     </xsl:element>
1279
1280     <!-- special item with property="cover-image" -->
1281     <xsl:element namespace="{$opf.namespace}" name="item">
1282       <xsl:attribute name="id">
1283         <xsl:value-of select="$epub.cover.image.id"/>
1284       </xsl:attribute>
1285       <xsl:attribute name="properties">cover-image</xsl:attribute>
1286       <xsl:attribute name="href">
1287         <xsl:value-of select="$image.filename"/>
1288       </xsl:attribute>
1289       <xsl:if test="$image.type">
1290         <xsl:attribute name="media-type">
1291           <xsl:value-of select="$image.type"/>
1292         </xsl:attribute>
1293       </xsl:if>
1294     </xsl:element>
1295
1296     <!-- And generate the cover html file -->
1297     <xsl:apply-templates select="$cover.image"/>
1298
1299   </xsl:if>
1300 </xsl:template>
1301
1302 <xsl:template name="manifest.toc">
1303   <xsl:variable name="toc.params">
1304     <xsl:call-template name="find.path.params">
1305       <xsl:with-param name="node" select="."/>
1306       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
1307     </xsl:call-template>
1308   </xsl:variable>
1309
1310   <xsl:if test="contains($toc.params, 'toc')">
1311     <xsl:element namespace="{$opf.namespace}" name="item">
1312       <xsl:attribute name="id">
1313         <xsl:value-of select="$epub.html.toc.id"/>
1314       </xsl:attribute>
1315       <xsl:attribute name="properties">nav</xsl:attribute>
1316       <xsl:attribute name="media-type">
1317         <xsl:value-of select="$epub.xhtml.mediatype"/>
1318       </xsl:attribute>
1319       <xsl:attribute name="href">
1320         <xsl:call-template name="toc-href">
1321           <xsl:with-param name="node" select="."/>
1322         </xsl:call-template>
1323       </xsl:attribute>
1324     </xsl:element>
1325   </xsl:if>  
1326 </xsl:template>
1327
1328 <xsl:template name="toc-href">
1329   <xsl:param name="node" select="."/>
1330   <!-- FIXME -->
1331   <xsl:apply-templates select="$node" mode="recursive-chunk-filename">
1332     <xsl:with-param name="recursive" select="true()"/>
1333   </xsl:apply-templates>
1334   <xsl:text>-toc</xsl:text>
1335   <xsl:value-of select="$html.ext"/>
1336 </xsl:template>
1337
1338 <xsl:template match="*" mode="recursive-chunk-filename">
1339   <!-- placeholder for real template in chunking stylesheet -->
1340   <xsl:value-of select="concat(local-name(.), '-', generate-id(.))"/>
1341 </xsl:template>
1342
1343 <xsl:template name="manifest.content.items">
1344   <xsl:apply-templates select="." mode="package.manifest"/>
1345   <xsl:call-template name="manifest.images"/>
1346 </xsl:template>
1347
1348 <xsl:template match="text()" mode="package.manifest"/>
1349
1350 <xsl:template match="mediaobject|mediaobjectco|inlinemediaobject" priority="1"
1351               mode="package.manifest">
1352   <!-- These are handled out of line so a unique list is created
1353        to remove duplicate references -->
1354 </xsl:template>
1355
1356 <xsl:template match="*" mode="package.manifest">
1357
1358   <xsl:variable name="is.chunk">
1359     <xsl:call-template name="chunk">
1360       <xsl:with-param name="node" select="."/>
1361     </xsl:call-template>
1362   </xsl:variable>
1363
1364   <xsl:if test="$is.chunk != 0">
1365     <xsl:variable name="href">
1366       <xsl:call-template name="href.target.with.base.dir">
1367         <xsl:with-param name="context" select="/"/>
1368       </xsl:call-template>
1369     </xsl:variable>
1370   
1371     <xsl:variable name="id" select="concat($epub.package.id.prefix, generate-id())"/>
1372
1373     <xsl:variable name="properties.set">
1374       <xsl:call-template name="svg.property"/>
1375       <xsl:text> </xsl:text>
1376       <xsl:call-template name="mathml.property"/>
1377     </xsl:variable>
1378
1379     <xsl:variable name="properties" select="normalize-space($properties.set)"/>
1380
1381     <xsl:element namespace="{$opf.namespace}" name="item">
1382       <xsl:attribute name="id">
1383         <xsl:value-of select="$id"/>
1384       </xsl:attribute>
1385       <xsl:attribute name="href">
1386         <xsl:value-of select="$href"/>
1387       </xsl:attribute>
1388       <xsl:attribute name="media-type">application/xhtml+xml</xsl:attribute>
1389       <xsl:if test="string-length($properties) != 0">
1390         <xsl:attribute name="properties">
1391           <xsl:value-of select="$properties"/>
1392         </xsl:attribute>
1393       </xsl:if>
1394     </xsl:element>
1395   </xsl:if>  
1396   <xsl:apply-templates mode="package.manifest"/>
1397
1398 </xsl:template>
1399
1400 <xsl:template name="svg.property">
1401   <xsl:param name="this.chunk" select="."/>
1402
1403   <xsl:variable name="genid" select="generate-id($this.chunk)"/>
1404
1405   <!-- get the chunkfast div element for this chunk -->
1406   <xsl:variable name="div" select="$chunkset[@id=$genid or @xml:id=$genid]"/>
1407
1408   <!-- get the chunkfast div element the next chunk -->
1409   <xsl:variable name="nextdiv"
1410                 select="($div/following-sibling::cf:div|
1411                          $div/following::cf:div|
1412                          $div/cf:div)[1]"/>
1413
1414   <!-- get the element corresponding to the next chunk -->
1415   <xsl:variable name="next.chunk" select="key('genid', ($nextdiv/@id|$nextdiv/@xml:id)[1])"/>
1416
1417   <xsl:choose>
1418     <xsl:when test="$next.chunk">
1419       <xsl:variable name="this.imagedata"
1420                     select="$this.chunk//mediaobject"/>
1421       <xsl:variable name="before.next"
1422                     select="$next.chunk/preceding::mediaobject"/>
1423       
1424       <!-- select for an SVG imagedata in the intersection of them -->
1425       <xsl:variable name="mediaobject.set"
1426           select="$this.imagedata[count(.|$before.next) = count($before.next)]"/>
1427       <xsl:variable name="svg.imagedata">
1428         <xsl:for-each select="$mediaobject.set">
1429           <xsl:variable name="olist" select="imageobject[not(@role = 'poster')] |
1430                                              imageobjectco"/>
1431           <xsl:variable name="mediaobject.index">
1432             <xsl:call-template name="select.mediaobject.index">
1433               <xsl:with-param name="olist" select="$olist"/>
1434             </xsl:call-template>
1435           </xsl:variable>
1436           <xsl:variable name="object" select="$olist[position() = $mediaobject.index]"/>
1437           <xsl:if test="$object/imagedata[contains(
1438                       substring(@fileref, string-length(@fileref)-3,4), '.svg')]">
1439             <xsl:text>svg</xsl:text>
1440           </xsl:if>
1441         </xsl:for-each>
1442       </xsl:variable>
1443     
1444       <xsl:if test="contains($svg.imagedata, 'svg')">
1445         <xsl:text>svg</xsl:text>
1446      </xsl:if>
1447     </xsl:when>
1448     <xsl:otherwise>
1449       <xsl:variable name="mediaobject.set"
1450                     select="$this.chunk//mediaobject"/>
1451       <xsl:variable name="svg.imagedata">
1452         <xsl:for-each select="$mediaobject.set">
1453           <xsl:variable name="olist" select="imageobject[not(@role = 'poster')] |
1454                                              imageobjectco"/>
1455           <xsl:variable name="mediaobject.index">
1456             <xsl:call-template name="select.mediaobject.index">
1457               <xsl:with-param name="olist" select="$olist"/>
1458             </xsl:call-template>
1459           </xsl:variable>
1460           <xsl:variable name="object" select="$olist[position() = $mediaobject.index]"/>
1461           <xsl:if test="$object/imagedata[contains(
1462                       substring(@fileref, string-length(@fileref)-3,4), '.svg')]">
1463             <xsl:text>svg</xsl:text>
1464           </xsl:if>
1465         </xsl:for-each>
1466       </xsl:variable>
1467     
1468       <xsl:if test="contains($svg.imagedata, 'svg')">
1469         <xsl:text>svg</xsl:text>
1470      </xsl:if>
1471     </xsl:otherwise>
1472   </xsl:choose>
1473
1474 </xsl:template>
1475
1476 <xsl:template name="mathml.property">
1477   <xsl:param name="this.chunk" select="."/>
1478
1479   <xsl:variable name="genid" select="generate-id($this.chunk)"/>
1480
1481   <!-- get the chunkfast div element for this chunk -->
1482   <xsl:variable name="div" select="$chunkset[@id=$genid or @xml:id=$genid]"/>
1483
1484   <!-- get the chunkfast div element the next chunk -->
1485   <xsl:variable name="nextdiv"
1486                 select="($div/following-sibling::cf:div|
1487                          $div/following::cf:div|
1488                          $div/cf:div)[1]"/>
1489
1490   <!-- get the element corresponding to the next chunk -->
1491   <xsl:variable name="next.chunk" select="key('genid', ($nextdiv/@id|$nextdiv/@xml:id)[1])"/>
1492
1493   <xsl:variable name="this.math"
1494                 select="$this.chunk//m:*"/>
1495   <xsl:variable name="before.next"
1496                 select="$next.chunk/preceding::m:*"/>
1497   
1498   <!-- select for an SVG imagedata in the intersection of them -->
1499   <xsl:variable name="intersection"
1500       select="$this.math[count(.|$before.next) = count($before.next)]"/>
1501
1502   <xsl:if test="count($intersection) != 0">
1503     <xsl:text>mathml</xsl:text>
1504  </xsl:if>
1505 </xsl:template>
1506
1507 <xsl:template name="manifest.image.item">
1508 </xsl:template>
1509
1510 <!-- Need a uniqued list of images -->
1511 <xsl:template name="manifest.images">
1512   <xsl:message>Generating image list ...</xsl:message>
1513   <xsl:variable name="imagelist">
1514     <xsl:choose>
1515       <xsl:when test="$rootid != ''">
1516         <xsl:apply-templates select="key('id', $rootid)" mode="enumerate-images"/>
1517       </xsl:when>
1518       <xsl:otherwise>
1519         <xsl:apply-templates select="/" mode="enumerate-images"/>
1520       </xsl:otherwise>
1521     </xsl:choose>
1522   </xsl:variable>
1523
1524   <xsl:choose>
1525     <xsl:when test="$exsl.node.set.available != 0 and 
1526                     function-available('set:distinct')">
1527       <xsl:for-each select="set:distinct(exsl:node-set($imagelist)/*)">
1528         <xsl:if test="string-length(tmp-href) != 0">
1529           <!-- convert the child elements to attributes -->
1530           <xsl:element name="item" namespace="{$opf.namespace}">
1531             <xsl:attribute name="id">
1532               <xsl:value-of select="generate-id()"/>
1533             </xsl:attribute>
1534             <xsl:attribute name="href">
1535               <xsl:value-of select="tmp-href"/>
1536             </xsl:attribute>
1537             <xsl:attribute name="media-type">
1538               <xsl:value-of select="media-type"/>
1539             </xsl:attribute>
1540           </xsl:element>
1541         </xsl:if>
1542       </xsl:for-each>
1543     </xsl:when>
1544     <xsl:when test="$exsl.node.set.available != 0">
1545       <xsl:for-each select="exsl:node-set($imagelist)/*">
1546         <xsl:element name="item" namespace="{$opf.namespace}">
1547           <xsl:attribute name="id">
1548             <xsl:value-of select="generate-id()"/>
1549           </xsl:attribute>
1550           <xsl:attribute name="href">
1551             <xsl:value-of select="tmp-href"/>
1552           </xsl:attribute>
1553           <xsl:attribute name="media-type">
1554             <xsl:value-of select="media-type"/>
1555           </xsl:attribute>
1556         </xsl:element>
1557       </xsl:for-each>
1558     </xsl:when>
1559     <xsl:otherwise>
1560       <xsl:message terminate="yes">
1561         <xsl:text>ERROR: cannot process images list without </xsl:text>
1562         <xsl:text>exsl:node-set() function</xsl:text>
1563       </xsl:message>
1564       <!-- cannot do this without node-set() -->
1565     </xsl:otherwise>
1566   </xsl:choose>
1567
1568 </xsl:template>
1569
1570 <xsl:template match="*" mode="enumerate-images">
1571   <xsl:apply-templates mode="enumerate-images"/>
1572 </xsl:template>
1573
1574 <xsl:template match="text()" mode="enumerate-images"/>
1575
1576 <xsl:template match="graphic|inlinegraphic[@format!='linespecific']" 
1577               mode="enumerate-images">
1578 </xsl:template>
1579
1580 <!-- cover image handled separates to give it an extra property attribute -->
1581 <xsl:template match="mediaobject[@role='cover' or ancestor::cover]"
1582               mode="enumerate-images"/>
1583
1584 <xsl:template match="mediaobject|inlinemediaobject" mode="enumerate-images">
1585
1586   <xsl:variable name="olist" 
1587                 select="imageobject[not(@role = 'poster')] 
1588                        |imageobjectco
1589                        |videoobject
1590                        |audioobject
1591                        |textobject"/>
1592  
1593   <xsl:variable name="object.index">
1594     <xsl:call-template name="select.mediaobject.index">
1595       <xsl:with-param name="olist" select="$olist"/>
1596       <xsl:with-param name="count" select="1"/>
1597     </xsl:call-template>
1598   </xsl:variable>
1599   
1600   <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1601
1602   <xsl:apply-templates select="$object" mode="enumerate-images"/>
1603
1604   <!-- also include a poster image if present -->
1605   <xsl:apply-templates select="imageobject[@role = 'poster']" mode="enumerate-images"/>
1606
1607 </xsl:template>
1608
1609 <xsl:template match="imageobject|videoobject|audioobject" mode="enumerate-images">
1610   <xsl:param name="object" select="."/>
1611
1612   <xsl:if test="$object">
1613     <xsl:variable name="output_filename">
1614       <xsl:call-template name="mediaobject.filename">
1615         <xsl:with-param name="object" select="$object"/>
1616       </xsl:call-template>
1617     </xsl:variable>
1618
1619     <xsl:variable name="image.filename">
1620        <xsl:if test="$img.src.path != '' and
1621                        not(starts-with($output_filename, '/')) and
1622                        not(contains($output_filename, '://'))">
1623          <xsl:value-of select="$img.src.path"/>
1624        </xsl:if>
1625        <xsl:value-of select="$output_filename"/>
1626     </xsl:variable>
1627
1628     <xsl:variable name="image.extension">
1629       <xsl:call-template name="filename-extension">
1630         <xsl:with-param name="filename" select="$image.filename"/>
1631       </xsl:call-template>
1632     </xsl:variable>
1633
1634     <xsl:variable name="image.type">
1635       <xsl:call-template name="graphic.format.content-type">
1636         <xsl:with-param name="format" select="translate($image.extension, 
1637                    &lowercase;, &uppercase;)"/>
1638       </xsl:call-template>
1639     </xsl:variable>
1640
1641     <xsl:element name="tmp-filename" namespace="">
1642       <xsl:element name="tmp-href" namespace="">
1643         <xsl:value-of select="$image.filename"/>
1644       </xsl:element>
1645       <xsl:element name="media-type" namespace="">
1646         <xsl:value-of select="$image.type"/>
1647       </xsl:element>
1648     </xsl:element>
1649
1650   </xsl:if>
1651 </xsl:template>
1652  
1653 <!-- Add in the generated images -->
1654 <xsl:template match="note|caution|warning|important|tip" mode="enumerate-images">
1655   <xsl:if test="$admon.graphics != 0">
1656     <xsl:variable name="image.filename">
1657       <xsl:call-template name="admon.graphic"/>
1658     </xsl:variable>
1659
1660     <xsl:variable name="image.type">
1661       <xsl:call-template name="graphic.format.content-type">
1662         <xsl:with-param name="format" select="translate(
1663                substring-after($admon.graphics.extension,'.'), 
1664                    &lowercase;, &uppercase;)"/>
1665       </xsl:call-template>
1666     </xsl:variable>
1667
1668     <xsl:element name="tmp-filename" namespace="">
1669       <xsl:element name="tmp-href" namespace="">
1670         <xsl:value-of select="$image.filename"/>
1671       </xsl:element>
1672       <xsl:element name="media-type" namespace="">
1673         <xsl:value-of select="$image.type"/>
1674       </xsl:element>
1675     </xsl:element>
1676
1677   </xsl:if>
1678 </xsl:template>
1679
1680 <xsl:template match="callout" mode="enumerate-images">
1681   <!-- process arearefs to get name of callout bug image files -->
1682   <xsl:if test="$callout.graphics != 0">
1683     <xsl:variable name="arearefs">
1684       <xsl:call-template name="callout.arearefs">
1685         <xsl:with-param name="arearefs" select="@arearefs"/>
1686       </xsl:call-template>
1687     </xsl:variable>
1688
1689     <xsl:variable name="nodes" select="exsl:node-set($arearefs)"/>
1690
1691     <xsl:for-each select="$nodes//*[@src]">
1692       <xsl:variable name="image.filename" select="@src"/>
1693
1694       <xsl:variable name="image.type">
1695         <xsl:call-template name="graphic.format.content-type">
1696           <xsl:with-param name="format" select="translate(
1697                  substring-after($callout.graphics.extension,'.'), 
1698                      &lowercase;, &uppercase;)"/>
1699         </xsl:call-template>
1700       </xsl:variable>
1701
1702       <xsl:element name="tmp-filename" namespace="">
1703         <xsl:element name="tmp-href" namespace="">
1704           <xsl:value-of select="$image.filename"/>
1705         </xsl:element>
1706         <xsl:element name="media-type" namespace="">
1707           <xsl:value-of select="$image.type"/>
1708         </xsl:element>
1709       </xsl:element>
1710     </xsl:for-each>
1711
1712   </xsl:if>
1713 </xsl:template>
1714
1715 <xsl:template match="co" mode="enumerate-images">
1716   <!-- process co to get name of callout bug image file -->
1717   <xsl:if test="$callout.graphics != 0">
1718     <xsl:variable name="result">
1719       <xsl:apply-templates select="." mode="callout-bug"/>
1720     </xsl:variable>
1721
1722     <xsl:variable name="nodes" select="exsl:node-set($result)"/>
1723
1724     <xsl:for-each select="$nodes//*[@src]">
1725       <xsl:variable name="image.filename" select="@src"/>
1726
1727       <xsl:variable name="image.type">
1728         <xsl:call-template name="graphic.format.content-type">
1729           <xsl:with-param name="format" select="translate(
1730                  substring-after($callout.graphics.extension,'.'), 
1731                      &lowercase;, &uppercase;)"/>
1732         </xsl:call-template>
1733       </xsl:variable>
1734
1735       <xsl:element name="tmp-filename" namespace="">
1736         <xsl:element name="tmp-href" namespace="">
1737           <xsl:value-of select="$image.filename"/>
1738         </xsl:element>
1739         <xsl:element name="media-type" namespace="">
1740           <xsl:value-of select="$image.type"/>
1741         </xsl:element>
1742       </xsl:element>
1743     </xsl:for-each>
1744
1745   </xsl:if>
1746
1747 </xsl:template>
1748
1749 <!-- ======================================================== -->
1750 <!-- NCX templates are for backwards compatibility with EPUB2 -->
1751 <!-- ======================================================== -->
1752
1753 <xsl:template name="ncx">
1754   <xsl:message>Generating NCX file ...</xsl:message>
1755   <xsl:call-template name="write.chunk">
1756     <xsl:with-param name="filename">
1757       <xsl:value-of select="$epub.ncx.pathname" />
1758     </xsl:with-param>
1759     <xsl:with-param name="method" select="'xml'" />
1760     <xsl:with-param name="encoding" select="'utf-8'" />
1761     <xsl:with-param name="indent" select="'no'" />
1762     <xsl:with-param name="quiet" select="$chunk.quietly" />
1763     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
1764     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
1765     <xsl:with-param name="content">
1766       <xsl:element name="ncx" namespace="{$ncx.namespace}">
1767         <xsl:attribute name="version">2005-1</xsl:attribute>
1768
1769           <!-- Via Martin Goerner: On covers: the IDPF2.0 standard unfortunately does not have a provision for
1770           covers. We had to add one and we did so in conjunction with the IDPF and
1771           various publishers. The tag chosen to define the covers is:
1772           <meta name="cover" content="-reference to a manifest item-">
1773           Then, we also added a bit of logic to get rid cleanly of the HTML cover
1774           people usually add because the logical cover is not specced by the IDPF. So,
1775           if the HTML cover item is marked linear="no" AND there is a guide item of
1776           type="cover" pointing to it AND there is a logical cover specified in a
1777           <meta name="cover"> tag, THEN, the HTML cover is discarded. -->
1778         <xsl:element name="head" namespace="{$ncx.namespace}">
1779           <xsl:if test="/*/*[cover or contains(name(.), 'info')]//mediaobject[@role='cover' or ancestor::cover]"> 
1780             <xsl:element name="meta" namespace="{$ncx.namespace}">
1781               <xsl:attribute name="name">cover</xsl:attribute>
1782               <xsl:attribute name="content">
1783                 <xsl:value-of select="$epub.cover.filename.id"/>
1784               </xsl:attribute>
1785             </xsl:element>
1786           </xsl:if>
1787           <xsl:element name="meta" namespace="{$ncx.namespace}">
1788             <xsl:attribute name="name">dtb:uid</xsl:attribute>
1789             <xsl:attribute name="content"><xsl:call-template name="package-identifier"/></xsl:attribute>
1790           </xsl:element>
1791         </xsl:element>
1792
1793         <xsl:choose>
1794           <xsl:when test="$rootid != ''">
1795             <xsl:variable name="title">
1796               <xsl:if test="$epub.autolabel != 0">
1797                 <xsl:variable name="label.markup">
1798                   <xsl:apply-templates select="key('id',$rootid)" mode="label.markup" />
1799                 </xsl:variable>
1800                 <xsl:if test="normalize-space($label.markup)">
1801                   <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
1802                 </xsl:if>
1803               </xsl:if>
1804               <xsl:apply-templates select="key('id',$rootid)" mode="title.markup" />
1805             </xsl:variable>
1806             <xsl:variable name="href">
1807               <xsl:call-template name="href.target.with.base.dir">
1808                 <xsl:with-param name="object" select="key('id',$rootid)" />
1809               </xsl:call-template>
1810             </xsl:variable>
1811             <xsl:element name="docTitle" namespace="{$ncx.namespace}">
1812               <xsl:element name="text" namespace="{$ncx.namespace}"><xsl:value-of select="normalize-space($title)" />  </xsl:element>
1813             </xsl:element>
1814             <xsl:element name="navMap" namespace="{$ncx.namespace}">
1815               <xsl:apply-templates select="key('id',$rootid)/*" mode="ncx" />
1816             </xsl:element>
1817           </xsl:when>
1818           <xsl:otherwise>
1819             <xsl:variable name="title">
1820               <xsl:if test="$epub.autolabel != 0">
1821                 <xsl:variable name="label.markup">
1822                   <xsl:apply-templates select="/*" mode="label.markup" />
1823                 </xsl:variable>
1824                 <xsl:if test="normalize-space($label.markup)">
1825                   <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
1826                 </xsl:if>
1827               </xsl:if>
1828               <xsl:apply-templates select="/*" mode="title.markup" />
1829             </xsl:variable>
1830             <xsl:variable name="href">
1831               <xsl:call-template name="href.target.with.base.dir">
1832                 <xsl:with-param name="object" select="/" />
1833               </xsl:call-template>
1834             </xsl:variable>
1835             <xsl:element name="docTitle" namespace="{$ncx.namespace}">
1836               <xsl:element name="text" namespace="{$ncx.namespace}">
1837                 <xsl:value-of select="normalize-space($title)" />
1838               </xsl:element>
1839             </xsl:element>
1840             <xsl:element name="navMap" namespace="{$ncx.namespace}">
1841               <xsl:choose>
1842                 <xsl:when test="$root.is.a.chunk != '0'">
1843                   <xsl:apply-templates select="/*" mode="ncx" />
1844                   <xsl:apply-templates select="/*/*" mode="ncx" />
1845                 </xsl:when>
1846                 <xsl:otherwise>
1847                   <xsl:apply-templates select="/*/*" mode="ncx" />
1848                 </xsl:otherwise>
1849               </xsl:choose>
1850             </xsl:element>
1851           </xsl:otherwise>
1852
1853         </xsl:choose>
1854       </xsl:element>
1855     </xsl:with-param>
1856   </xsl:call-template>
1857 </xsl:template>
1858
1859 <xsl:template match="book|
1860                      article|
1861                      topic|
1862                      part|
1863                      reference|
1864                      preface|
1865                      chapter|
1866                      bibliography|
1867                      appendix|
1868                      glossary|
1869                      section|
1870                      sect1|
1871                      sect2|
1872                      sect3|
1873                      sect4|
1874                      sect5|
1875                      refentry|
1876                      colophon|
1877                      bibliodiv[title]|
1878                      setindex|
1879                      index"
1880               mode="ncx">
1881   <xsl:variable name="depth" select="count(ancestor::*)"/>
1882   <xsl:variable name="title">
1883     <xsl:if test="$epub.autolabel != 0">
1884       <xsl:variable name="label.markup">
1885         <xsl:apply-templates select="." mode="label.markup" />
1886       </xsl:variable>
1887       <xsl:if test="normalize-space($label.markup)">
1888         <xsl:value-of
1889           select="concat($label.markup,$autotoc.label.separator)" />
1890       </xsl:if>
1891     </xsl:if>
1892     <xsl:apply-templates select="." mode="title.markup" />
1893   </xsl:variable>
1894
1895   <xsl:variable name="href">
1896     <xsl:call-template name="href.target.with.base.dir">
1897       <xsl:with-param name="context" select="/" />
1898       <!-- Generate links relative to the location of root file/toc.xml file -->
1899     </xsl:call-template>
1900   </xsl:variable>
1901
1902   <xsl:variable name="id">
1903     <xsl:value-of select="generate-id(.)"/>
1904   </xsl:variable>
1905   <xsl:variable name="order">
1906     <xsl:value-of select="$depth +
1907                                 count(preceding::part|
1908                                 preceding::reference|
1909                                 preceding::book[parent::set]|
1910                                 preceding::preface|
1911                                 preceding::chapter|
1912                                 preceding::bibliography|
1913                                 preceding::appendix|
1914                                 preceding::article|
1915                                 preceding::topic|
1916                                 preceding::glossary|
1917                                 preceding::section[not(parent::partintro)]|
1918                                 preceding::sect1[not(parent::partintro)]|
1919                                 preceding::sect2[not(ancestor::partintro)]|
1920                                 preceding::sect3[not(ancestor::partintro)]|
1921                                 preceding::sect4[not(ancestor::partintro)]|
1922                                 preceding::sect5[not(ancestor::partintro)]|
1923                                 preceding::refentry|
1924                                 preceding::colophon|
1925                                 preceding::bibliodiv[title]|
1926                                 preceding::index)"/>
1927   </xsl:variable>
1928
1929   <xsl:element name="navPoint" namespace="{$ncx.namespace}">
1930     <xsl:attribute name="id">
1931       <xsl:value-of select="$id"/>
1932     </xsl:attribute>
1933
1934     <xsl:attribute name="playOrder">
1935       <xsl:choose>
1936         <xsl:when test="/*[self::set]">
1937           <xsl:value-of select="$order"/>
1938         </xsl:when>
1939         <xsl:when test="$root.is.a.chunk != '0'">
1940           <xsl:value-of select="$order + 1"/>
1941         </xsl:when>
1942         <xsl:otherwise>
1943           <xsl:value-of select="$order - 0"/>
1944         </xsl:otherwise>
1945       </xsl:choose>
1946     </xsl:attribute>
1947     <xsl:element name="navLabel" namespace="{$ncx.namespace}">
1948       <xsl:element name="text" namespace="{$ncx.namespace}"><xsl:value-of select="normalize-space($title)"/> </xsl:element>
1949     </xsl:element>
1950     <xsl:element name="content" namespace="{$ncx.namespace}">
1951       <xsl:attribute name="src">
1952         <xsl:value-of select="$href"/>
1953       </xsl:attribute>
1954     </xsl:element>
1955     <xsl:if test="$depth != 0">
1956       <!-- Don't recurse on root element, but treat it as a single point so
1957       the progress bar shows all top level children -->
1958       <xsl:apply-templates select="book[parent::set]|part|reference|preface|chapter|bibliography|appendix|article|topic|glossary|section|sect1|sect2|sect3|sect4|sect5|refentry|colophon|bibliodiv[title]|setindex|index" mode="ncx"/>
1959     </xsl:if>
1960   </xsl:element>
1961
1962 </xsl:template>
1963
1964 <xsl:template match="text()" mode="ncx" />
1965
1966 <xsl:template name="package.spine">
1967
1968   <xsl:variable name="toc.params">
1969     <xsl:call-template name="find.path.params">
1970       <xsl:with-param name="node" select="."/>
1971       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
1972     </xsl:call-template>
1973   </xsl:variable>
1974
1975   <xsl:element namespace="http://www.idpf.org/2007/opf" name="spine">
1976     <xsl:if test="$epub.include.ncx != 0">
1977       <xsl:attribute name="toc">
1978         <xsl:value-of select="$epub.ncx.manifest.id"/>
1979       </xsl:attribute>
1980     </xsl:if>
1981
1982     <xsl:call-template name="spine.cover"/>
1983
1984
1985     <xsl:if test="contains($toc.params, 'toc')">
1986       <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
1987         <xsl:attribute name="idref"> <xsl:value-of select="$epub.html.toc.id"/> </xsl:attribute>
1988         <xsl:attribute name="linear">yes</xsl:attribute>
1989       </xsl:element>
1990     </xsl:if>  
1991
1992     <!-- TODO: be nice to have a idref="titlepage" here -->
1993     <xsl:choose>
1994       <xsl:when test="$root.is.a.chunk != '0'">
1995         <xsl:apply-templates select="/*" mode="package.spine"/>
1996       </xsl:when>
1997       <xsl:otherwise>
1998         <xsl:apply-templates select="/*/*" mode="package.spine"/>
1999       </xsl:otherwise>
2000     </xsl:choose>
2001                                  
2002   </xsl:element>
2003 </xsl:template>
2004
2005 <xsl:template name="spine.cover">
2006   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
2007   <xsl:variable name="cover.image" 
2008                 select="$info//mediaobject[@role='cover' or ancestor::cover]"/>
2009
2010   <xsl:if test="$cover.image">
2011     <!-- generate the spine reference to that cover html file -->
2012     <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
2013       <xsl:attribute name="idref">
2014         <xsl:value-of select="$epub.cover.filename.id"/>
2015       </xsl:attribute>
2016       <xsl:attribute name="linear">
2017       <xsl:choose>
2018         <xsl:when test="$epub.cover.linear">
2019           <xsl:text>yes</xsl:text>
2020         </xsl:when>
2021         <xsl:otherwise>no</xsl:otherwise>
2022       </xsl:choose>
2023       </xsl:attribute>
2024     </xsl:element>
2025   </xsl:if>
2026 </xsl:template>
2027
2028 <xsl:template match="*" mode="package.spine">
2029   <xsl:variable name="is.chunk">
2030     <xsl:call-template name="chunk">
2031       <xsl:with-param name="node" select="."/>
2032     </xsl:call-template>
2033   </xsl:variable>
2034
2035   <xsl:if test="$is.chunk != 0">
2036     <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
2037       <xsl:attribute name="idref">
2038         <xsl:value-of select="concat($epub.package.id.prefix, generate-id(.))"/>
2039       </xsl:attribute>
2040     </xsl:element>
2041     <xsl:apply-templates select="*|.//refentry" mode="package.spine"/>
2042   </xsl:if>
2043 </xsl:template>
2044
2045 <xsl:template match="*" mode="container">
2046   <xsl:call-template name="container"/>
2047 </xsl:template>
2048
2049 <xsl:template name="container">
2050   <!-- The path in rootfile does not include all of base.dir, only the last part -->
2051   <xsl:variable name="full-path-dir">
2052     <xsl:call-template name="filename-basename">
2053       <xsl:with-param name="filename" select="$chunk.base.dir"/>
2054     </xsl:call-template>
2055   </xsl:variable>
2056   <xsl:call-template name="write.chunk">
2057     <xsl:with-param name="filename">
2058       <xsl:value-of select="$epub.container.pathname" />
2059     </xsl:with-param>
2060     <xsl:with-param name="method" select="'xml'" />
2061     <xsl:with-param name="encoding" select="'utf-8'" />
2062     <xsl:with-param name="indent" select="'no'" />
2063     <xsl:with-param name="quiet" select="$chunk.quietly" />
2064     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
2065     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
2066
2067     <xsl:with-param name="content">
2068       <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="container">
2069         <xsl:attribute name="version">1.0</xsl:attribute>
2070         <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfiles">
2071           <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfile">
2072             <xsl:attribute name="full-path">
2073               <xsl:value-of 
2074                      select="concat($full-path-dir, $epub.package.filename)"/>
2075             </xsl:attribute>
2076             <xsl:attribute name="media-type">
2077               <xsl:text>application/oebps-package+xml</xsl:text>
2078             </xsl:attribute>
2079           </xsl:element>
2080         </xsl:element>
2081       </xsl:element>
2082     </xsl:with-param>
2083   </xsl:call-template>
2084 </xsl:template>
2085
2086 <xsl:template name="mimetype">
2087   <xsl:call-template name="write.text.chunk">
2088     <xsl:with-param name="filename" select="$epub.mimetype.pathname"/>
2089     <xsl:with-param name="content" select="$epub.mimetype.value"/>
2090   </xsl:call-template>
2091 </xsl:template>
2092
2093 <xsl:template match="mediaobject[@role='cover' or ancestor::cover]">
2094   <xsl:call-template name="write.chunk">
2095     <xsl:with-param name="filename">
2096       <xsl:value-of select="$epub.cover.pathname" />
2097     </xsl:with-param>
2098     <xsl:with-param name="method" select="'xml'" />
2099     <xsl:with-param name="encoding" select="'utf-8'" />
2100     <xsl:with-param name="indent" select="'no'" />
2101     <xsl:with-param name="quiet" select="$chunk.quietly" />
2102     <xsl:with-param name="content">
2103       <xsl:element namespace="http://www.w3.org/1999/xhtml" name="html">
2104         <xsl:element namespace="http://www.w3.org/1999/xhtml" name="head">
2105           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="title">Cover</xsl:element>
2106           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="style">
2107             <xsl:attribute name="type">text/css</xsl:attribute>
2108             <!-- Help the cover image scale nicely in the CSS then apply a max-width to look better in Adobe Digital Editions -->
2109             <xsl:text> img { max-width: 100%; }</xsl:text>
2110           </xsl:element>
2111         </xsl:element>
2112         <xsl:element namespace="http://www.w3.org/1999/xhtml" name="body">
2113           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="div">
2114             <xsl:attribute name="id">
2115               <xsl:value-of select="$epub.cover.image.id"/>
2116             </xsl:attribute>
2117             <xsl:choose>
2118               <xsl:when test="imageobject[@role='front-large']">
2119                 <xsl:apply-templates select="imageobject[@role='front-large']"/>
2120               </xsl:when>
2121               <xsl:otherwise>
2122                 <xsl:apply-templates select="imageobject[1]"/>
2123               </xsl:otherwise>
2124             </xsl:choose>
2125           </xsl:element>
2126           <!-- If this is defined as an explicit cover page, then process
2127           any remaining text -->
2128           <xsl:if test="ancestor::cover">
2129             <xsl:apply-templates select="ancestor::cover/para"/>
2130           </xsl:if>
2131         </xsl:element>
2132       </xsl:element>
2133     </xsl:with-param>  
2134   </xsl:call-template>  
2135 </xsl:template>
2136
2137 <!-- EPUB3: use <nav> and <ol> in TOC lists  -->
2138 <xsl:template name="make.toc">
2139   <xsl:param name="toc-context" select="."/>
2140   <xsl:param name="toc.title.p" select="true()"/>
2141   <xsl:param name="nodes" select="/NOT-AN-ELEMENT"/>
2142
2143   <xsl:variable name="nodes.plus" select="$nodes | qandaset"/>
2144
2145   <xsl:variable name="toc.title">
2146     <xsl:if test="$toc.title.p">
2147       <xsl:choose>
2148         <xsl:when test="$make.clean.html != 0">
2149           <div class="toc-title">
2150             <xsl:call-template name="gentext">
2151               <xsl:with-param name="key">TableofContents</xsl:with-param>
2152             </xsl:call-template>
2153           </div>
2154         </xsl:when>
2155         <xsl:otherwise>
2156           <p>
2157             <b>
2158               <xsl:call-template name="gentext">
2159                 <xsl:with-param name="key">TableofContents</xsl:with-param>
2160               </xsl:call-template>
2161             </b>
2162           </p>
2163         </xsl:otherwise>
2164       </xsl:choose>
2165     </xsl:if>
2166   </xsl:variable>
2167
2168   <xsl:choose>
2169     <xsl:when test="$manual.toc != ''">
2170       <xsl:variable name="id">
2171         <xsl:call-template name="object.id"/>
2172       </xsl:variable>
2173       <xsl:variable name="toc" select="document($manual.toc, .)"/>
2174       <xsl:variable name="tocentry" select="$toc//tocentry[@linkend=$id]"/>
2175       <xsl:if test="$tocentry and $tocentry/*">
2176         <div class="toc">
2177           <xsl:copy-of select="$toc.title"/>
2178           <nav epub:type="toc">
2179             <xsl:element name="{$toc.list.type}">
2180               <xsl:call-template name="manual-toc">
2181                 <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
2182               </xsl:call-template>
2183             </xsl:element>
2184           </nav>
2185         </div>
2186       </xsl:if>
2187     </xsl:when>
2188     <xsl:otherwise>
2189       <xsl:choose>
2190         <xsl:when test="$qanda.in.toc != 0">
2191           <div class="toc">
2192             <xsl:copy-of select="$toc.title"/>
2193             <nav epub:type="toc">
2194               <xsl:element name="{$toc.list.type}">
2195                 <xsl:if test="$nodes.plus">
2196                   <xsl:apply-templates select="$nodes.plus" mode="toc">
2197                     <xsl:with-param name="toc-context" select="$toc-context"/>
2198                   </xsl:apply-templates>
2199                 </xsl:if>
2200               </xsl:element>
2201             </nav>
2202           </div>
2203         </xsl:when>
2204         <xsl:otherwise>
2205           <div class="toc">
2206             <xsl:copy-of select="$toc.title"/>
2207             <nav epub:type="toc">
2208               <xsl:element name="{$toc.list.type}">
2209                 <xsl:if test="$nodes">
2210                   <xsl:apply-templates select="$nodes" mode="toc">
2211                     <xsl:with-param name="toc-context" select="$toc-context"/>
2212                   </xsl:apply-templates>
2213                 </xsl:if>
2214               </xsl:element>
2215             </nav>
2216           </div>
2217         </xsl:otherwise>
2218       </xsl:choose>
2219
2220     </xsl:otherwise>
2221   </xsl:choose>
2222 </xsl:template>
2223
2224 <xsl:template name="list.of.titles">
2225   <xsl:param name="toc-context" select="."/>
2226   <xsl:param name="titles" select="'table'"/>
2227   <xsl:param name="nodes" select=".//table"/>
2228
2229   <xsl:variable name="epub.type">
2230     <xsl:choose>
2231       <xsl:when test="$titles='table'">lot</xsl:when>
2232       <xsl:when test="$titles='figure'">loi</xsl:when>
2233       <xsl:when test="$titles='equation'">loi</xsl:when>
2234       <xsl:when test="$titles='example'">loi</xsl:when>
2235       <xsl:when test="$titles='procedure'">loi</xsl:when>
2236       <xsl:otherwise>loi</xsl:otherwise>
2237     </xsl:choose>
2238   </xsl:variable>
2239
2240   <xsl:if test="$nodes">
2241     <div class="list-of-{$titles}s">
2242       <nav epub:type="{$epub.type}">
2243         <h4 class="toc-title">
2244           <xsl:call-template name="gentext">
2245             <xsl:with-param name="key">
2246               <xsl:choose>
2247                 <xsl:when test="$titles='table'">ListofTables</xsl:when>
2248                 <xsl:when test="$titles='figure'">ListofFigures</xsl:when>
2249                 <xsl:when test="$titles='equation'">ListofEquations</xsl:when>
2250                 <xsl:when test="$titles='example'">ListofExamples</xsl:when>
2251                 <xsl:when test="$titles='procedure'">ListofProcedures</xsl:when>
2252                 <xsl:otherwise>ListofUnknown</xsl:otherwise>
2253               </xsl:choose>
2254             </xsl:with-param>
2255           </xsl:call-template>
2256         </h4>
2257
2258         <xsl:element name="{$toc.list.type}">
2259           <xsl:apply-templates select="$nodes" mode="toc">
2260             <xsl:with-param name="toc-context" select="$toc-context"/>
2261           </xsl:apply-templates>
2262         </xsl:element>
2263       </nav>
2264     </div>
2265   </xsl:if>
2266 </xsl:template>
2267
2268 <!-- EPUB3: add hidden="" for sections below toc.section.depth -->
2269 <xsl:template name="subtoc">
2270   <xsl:param name="toc-context" select="."/>
2271   <xsl:param name="nodes" select="NOT-AN-ELEMENT"/>
2272
2273   <xsl:variable name="nodes.plus" select="$nodes | qandaset"/>
2274
2275   <xsl:variable name="depth">
2276     <xsl:choose>
2277       <xsl:when test="local-name(.) = 'section'">
2278         <xsl:value-of select="count(ancestor::section) + 1"/>
2279       </xsl:when>
2280       <xsl:when test="local-name(.) = 'sect1'">1</xsl:when>
2281       <xsl:when test="local-name(.) = 'sect2'">2</xsl:when>
2282       <xsl:when test="local-name(.) = 'sect3'">3</xsl:when>
2283       <xsl:when test="local-name(.) = 'sect4'">4</xsl:when>
2284       <xsl:when test="local-name(.) = 'sect5'">5</xsl:when>
2285       <xsl:when test="local-name(.) = 'refsect1'">1</xsl:when>
2286       <xsl:when test="local-name(.) = 'refsect2'">2</xsl:when>
2287       <xsl:when test="local-name(.) = 'refsect3'">3</xsl:when>
2288       <xsl:when test="local-name(.) = 'simplesect'">
2289         <!-- sigh... -->
2290         <xsl:choose>
2291           <xsl:when test="local-name(..) = 'section'">
2292             <xsl:value-of select="count(ancestor::section)"/>
2293           </xsl:when>
2294           <xsl:when test="local-name(..) = 'sect1'">2</xsl:when>
2295           <xsl:when test="local-name(..) = 'sect2'">3</xsl:when>
2296           <xsl:when test="local-name(..) = 'sect3'">4</xsl:when>
2297           <xsl:when test="local-name(..) = 'sect4'">5</xsl:when>
2298           <xsl:when test="local-name(..) = 'sect5'">6</xsl:when>
2299           <xsl:when test="local-name(..) = 'refsect1'">2</xsl:when>
2300           <xsl:when test="local-name(..) = 'refsect2'">3</xsl:when>
2301           <xsl:when test="local-name(..) = 'refsect3'">4</xsl:when>
2302           <xsl:otherwise>1</xsl:otherwise>
2303         </xsl:choose>
2304       </xsl:when>
2305       <xsl:otherwise>0</xsl:otherwise>
2306     </xsl:choose>
2307   </xsl:variable>
2308
2309   <xsl:variable name="depth.from.context" select="count(ancestor::*)-count($toc-context/ancestor::*)"/>
2310
2311   <xsl:variable name="subtoc">
2312     <xsl:element name="{$toc.list.type}">
2313       <xsl:choose>
2314         <xsl:when test="$toc.listitem.type = 'li'
2315                   and $toc.section.depth > $depth and 
2316                   ( ($qanda.in.toc = 0 and count($nodes)&gt;0) or
2317                     ($qanda.in.toc != 0 and count($nodes.plus)&gt;0) )
2318                   and $toc.max.depth > $depth.from.context">
2319           <!-- No @hidden attribute -->
2320         </xsl:when>
2321         <xsl:otherwise>
2322           <!-- Add empty @hidden attribute if too deep -->
2323           <xsl:attribute name="hidden"></xsl:attribute>
2324         </xsl:otherwise>
2325       </xsl:choose>
2326
2327       <xsl:choose>
2328         <xsl:when test="$qanda.in.toc != 0">
2329           <xsl:apply-templates mode="toc" select="$nodes.plus">
2330             <xsl:with-param name="toc-context" select="$toc-context"/>
2331           </xsl:apply-templates>
2332         </xsl:when>
2333         <xsl:otherwise>
2334           <xsl:apply-templates mode="toc" select="$nodes">
2335             <xsl:with-param name="toc-context" select="$toc-context"/>
2336           </xsl:apply-templates>
2337         </xsl:otherwise>
2338       </xsl:choose>
2339     </xsl:element>
2340   </xsl:variable>
2341
2342   <xsl:variable name="subtoc.list">
2343     <xsl:choose>
2344       <xsl:when test="$toc.dd.type = ''">
2345         <xsl:copy-of select="$subtoc"/>
2346       </xsl:when>
2347       <xsl:otherwise>
2348         <xsl:element name="{$toc.dd.type}">
2349           <xsl:copy-of select="$subtoc"/>
2350         </xsl:element>
2351       </xsl:otherwise>
2352     </xsl:choose>
2353   </xsl:variable>
2354
2355   <xsl:element name="{$toc.listitem.type}">
2356     <xsl:call-template name="toc.line">
2357       <xsl:with-param name="toc-context" select="$toc-context"/>
2358     </xsl:call-template>
2359     <xsl:if test="$toc.listitem.type = 'li'
2360                   and 
2361                   ( ($qanda.in.toc = 0 and count($nodes)&gt;0) or
2362                     ($qanda.in.toc != 0 and count($nodes.plus)&gt;0) ) ">
2363       <xsl:copy-of select="$subtoc.list"/>
2364     </xsl:if>
2365   </xsl:element>
2366 </xsl:template>
2367
2368 <!-- Inserted when a title is blank to avoid epubcheck error -->
2369 <xsl:param name="toc.entry.default.text">&#xA0;</xsl:param>
2370
2371 <!-- EPUB3: either <a> or <span>, but not both  -->
2372 <xsl:template name="toc.line">
2373   <xsl:param name="toc-context" select="."/>
2374   <xsl:param name="depth" select="1"/>
2375   <xsl:param name="depth.from.context" select="8"/>
2376
2377   <xsl:variable name="title">
2378     <xsl:apply-templates select="." mode="title.markup"/>
2379   </xsl:variable>
2380
2381   <a>
2382     <xsl:attribute name="href">
2383       <xsl:call-template name="href.target">
2384         <xsl:with-param name="context" select="$toc-context"/>
2385         <xsl:with-param name="toc-context" select="$toc-context"/>
2386       </xsl:call-template>
2387     </xsl:attribute>
2388     
2389     <!-- * if $autotoc.label.in.hyperlink is non-zero, then output the label -->
2390     <!-- * as part of the hyperlinked title -->
2391     <xsl:if test="not($autotoc.label.in.hyperlink = 0)">
2392       <xsl:variable name="label">
2393         <xsl:apply-templates select="." mode="label.markup"/>
2394       </xsl:variable>
2395       <xsl:copy-of select="$label"/>
2396       <xsl:if test="$label != ''">
2397         <xsl:value-of select="$autotoc.label.separator"/>
2398       </xsl:if>
2399     </xsl:if>
2400
2401     <xsl:choose>
2402       <xsl:when test="string-length(normalize-space($title)) != 0">
2403         <xsl:copy-of select="$title"/>
2404       </xsl:when>
2405       <xsl:otherwise>
2406         <xsl:value-of select="$toc.entry.default.text"/>
2407       </xsl:otherwise>
2408     </xsl:choose>
2409   </a>
2410 </xsl:template>
2411
2412 <!-- Make sure all text is inside the <a> element for epub3 -->
2413 <xsl:template match="figure|table|example|equation|procedure" mode="toc">
2414   <xsl:param name="toc-context" select="."/>
2415
2416   <xsl:element name="{$toc.listitem.type}">
2417     <a>
2418       <xsl:attribute name="href">
2419         <xsl:call-template name="href.target">
2420           <xsl:with-param name="toc-context" select="$toc-context"/>
2421         </xsl:call-template>
2422       </xsl:attribute>
2423       <xsl:variable name="label">
2424         <xsl:apply-templates select="." mode="label.markup"/>
2425       </xsl:variable>
2426       <xsl:copy-of select="$label"/>
2427       <xsl:if test="$label != ''">
2428         <xsl:value-of select="$autotoc.label.separator"/>
2429       </xsl:if>
2430       <xsl:apply-templates select="." mode="titleabbrev.markup"/>
2431     </a>
2432   </xsl:element>
2433 </xsl:template>
2434
2435 <!-- Remove spans from refentry TOC lines for epub3check -->
2436 <xsl:template match="refentry" mode="toc">
2437   <xsl:param name="toc-context" select="."/>
2438
2439   <xsl:variable name="refmeta" select=".//refmeta"/>
2440   <xsl:variable name="refentrytitle" select="$refmeta//refentrytitle"/>
2441   <xsl:variable name="refnamediv" select=".//refnamediv"/>
2442   <xsl:variable name="refname" select="$refnamediv//refname"/>
2443   <xsl:variable name="refdesc" select="$refnamediv//refdescriptor"/>
2444   <xsl:variable name="title">
2445     <xsl:choose>
2446       <xsl:when test="$refentrytitle">
2447         <xsl:apply-templates select="$refentrytitle[1]" mode="titleabbrev.markup"/>
2448       </xsl:when>
2449       <xsl:when test="$refdesc">
2450         <xsl:apply-templates select="$refdesc" mode="titleabbrev.markup"/>
2451       </xsl:when>
2452       <xsl:when test="$refname">
2453         <xsl:apply-templates select="$refname[1]" mode="titleabbrev.markup"/>
2454       </xsl:when>
2455     </xsl:choose>
2456   </xsl:variable>
2457
2458   <xsl:element name="{$toc.listitem.type}" namespace="http://www.w3.org/1999/xhtml">
2459     <a>
2460       <xsl:attribute name="href">
2461         <xsl:call-template name="href.target">
2462           <xsl:with-param name="toc-context" select="$toc-context"/>
2463         </xsl:call-template>
2464       </xsl:attribute>
2465       <xsl:copy-of select="$title"/>
2466       <xsl:if test="$annotate.toc != 0">
2467         <!-- * DocBook 5 says inlinemediaobject (among other things) -->
2468         <!-- * is allowed in refpurpose; so we need to run -->
2469         <!-- * apply-templates on refpurpose here, instead of value-of  -->
2470         <!-- Set allow-anchors=0 to avoid indexterms and other links -->
2471         <xsl:text> - </xsl:text>
2472         <xsl:apply-templates select="refnamediv/refpurpose" mode="no.anchor.mode"/>
2473       </xsl:if>
2474     </a>
2475   </xsl:element>
2476 </xsl:template>
2477
2478 <!-- Copy these here so relative document() open gets the correct css source -->
2479 <xsl:template name="generate.default.css.file">
2480   <xsl:if test="$make.clean.html != 0 and 
2481                 $generate.css.header = 0 and
2482                 $docbook.css.source != ''">
2483     <!-- Select default file relative to stylesheet -->
2484     <xsl:variable name="css.node" select="document($docbook.css.source)/*[1]"/>
2485
2486     <xsl:call-template name="generate.css.file">
2487       <xsl:with-param name="src" select="$docbook.css.source"/>
2488       <xsl:with-param name="css.node" select="$css.node"/>
2489     </xsl:call-template>
2490   </xsl:if>
2491 </xsl:template>
2492
2493 <xsl:template name="generate.custom.css.file">
2494   <xsl:if test="$custom.css.source != '' and
2495                 $generate.css.header = 0">
2496     <!-- Select custom file relative to document -->
2497     <xsl:variable name="css.node" select="document($custom.css.source,.)/*[1]"/>
2498
2499     <xsl:call-template name="generate.css.file">
2500       <xsl:with-param name="src" select="$custom.css.source"/>
2501       <xsl:with-param name="css.node" select="$css.node"/>
2502     </xsl:call-template>
2503   </xsl:if>
2504 </xsl:template>
2505
2506 </xsl:stylesheet>