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