1 <?xml version="1.0" encoding="utf-8"?>
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:exsl="http://exslt.org/common"
5 xmlns:xlink="http://www.w3.org/1999/xlink"
6 xmlns="http://docbook.org/ns/docbook"
7 exclude-result-prefixes="exsl d xlink"
10 <!-- $Id: assemble.xsl,v 1.10 2012-04-10 07:56:58 bobs Exp $ -->
12 <xsl:preserve-space elements="*"/>
13 <xsl:strip-space elements="d:assembly d:structure d:module d:resources d:resource"/>
15 <xsl:key name="id" match="*" use="@id|@xml:id"/>
18 <xsl:param name="docbook.version">5.0</xsl:param>
19 <xsl:param name="root.default.renderas">book</xsl:param>
20 <xsl:param name="topic.default.renderas">section</xsl:param>
22 <xsl:param name="output.type" select="''"/>
23 <xsl:param name="output.format" select="''"/>
24 <!-- May be used to select one structure among several to process -->
25 <xsl:param name="structure.id" select="''"/>
28 <!-- default mode is to copy all content nodes -->
29 <xsl:template match="node()|@*" priority="-5" mode="copycontent">
30 <xsl:param name="omittitles"/>
32 <xsl:apply-templates select="@*" mode="copycontent"/>
33 <xsl:apply-templates mode="copycontent">
34 <xsl:with-param name="omittitles" select="$omittitles"/>
35 </xsl:apply-templates>
39 <xsl:template match="processing-instruction('oxygen')"/>
41 <!-- skip assembly info elements -->
42 <xsl:template match="d:info"/>
44 <!-- including for structure info element -->
45 <xsl:template match="d:structure/d:info"/>
47 <!-- handle omittitles, but only top level title of resource -->
48 <xsl:template match="/*/d:title
51 | /*/d:info/d:subtitle
53 | /*/d:info/d:titleabbrev"
55 <xsl:param name="omittitles"/>
58 <xsl:when test="$omittitles = 'yes' or $omittitles = 'true' or $omittitles = '1'">
63 <xsl:apply-templates select="@*" mode="copycontent"/>
64 <xsl:apply-templates mode="copycontent"/>
70 <!-- handled in a mode -->
71 <xsl:template match="d:resources"/>
72 <xsl:template match="d:output|d:filterin|d:filterout|d:merge|d:revhistory"/>
73 <xsl:template match="d:output|d:filterin|d:filterout|d:merge|d:revhistory"
76 <xsl:template match="d:assembly">
78 <xsl:when test="$structure.id != ''">
79 <xsl:variable name="id.structure" select="key('id', $structure.id)"/>
81 <xsl:when test="count($id.structure) = 0">
82 <xsl:message terminate="yes">
83 <xsl:text>ERROR: structure.id param set to '</xsl:text>
84 <xsl:value-of select="$structure.id"/>
85 <xsl:text>' but no element with that xml:id exists in assembly.</xsl:text>
88 <xsl:when test="local-name($id.structure) != 'structure'">
89 <xsl:message terminate="yes">
90 <xsl:text>ERROR: structure.id param set to '</xsl:text>
91 <xsl:value-of select="$structure.id"/>
92 <xsl:text>' but no structure with that xml:id exists in assembly.</xsl:text>
96 <xsl:apply-templates select="key('id', $structure.id)"/>
100 <xsl:when test="$output.type != '' and not(d:structure[@type = $output.type])">
101 <xsl:message terminate="yes">
102 <xsl:text>ERROR: output.type param set to '</xsl:text>
103 <xsl:value-of select="$output.type"/>
104 <xsl:text> but no structure element has that type attribute. Exiting.</xsl:text>
107 <xsl:when test="$output.type != '' and d:structure[@type = $output.type]">
108 <xsl:apply-templates select="d:structure[@type = $output.type][1]"/>
111 <!-- otherwise process the first structure -->
112 <xsl:apply-templates select="d:structure[1]"/>
117 <xsl:template match="d:structure[not(@resourceref)]">
119 <xsl:variable name="output.root.element">
120 <xsl:apply-templates select="." mode="compute.element.name"/>
123 <xsl:element name="{$output.root.element}" namespace="http://docbook.org/ns/docbook">
124 <xsl:attribute name="version">
125 <xsl:value-of select="$docbook.version"/>
127 <xsl:copy-of select="@xml:id"/>
129 <xsl:apply-templates>
130 <xsl:with-param name="parent" select="$output.root.element"/>
131 </xsl:apply-templates>
136 <xsl:template match="d:glossary|d:bibliography|d:index|d:toc">
137 <xsl:param name="parent" select="''"/>
138 <xsl:apply-templates select="." mode="copycontent"/>
141 <xsl:template match="d:title|d:titleabbrev|d:subtitle">
142 <xsl:param name="parent" select="''"/>
143 <xsl:apply-templates select="." mode="copycontent"/>
146 <!-- module without a resourceref creates an element -->
147 <xsl:template match="d:module[not(@resourceref)]">
148 <xsl:param name="parent" select="''"/>
150 <xsl:variable name="module" select="."/>
152 <xsl:variable name="element.name">
153 <xsl:apply-templates select="." mode="compute.element.name"/>
156 <xsl:element name="{$element.name}" namespace="http://docbook.org/ns/docbook">
158 <!-- Use the module's xml:id if it has one -->
159 <xsl:when test="@xml:id">
160 <xsl:attribute name="xml:id">
161 <xsl:value-of select="@xml:id"/>
166 <xsl:call-template name="merge.info">
167 <xsl:with-param name="merge.element" select="$module/d:merge"/>
170 <xsl:apply-templates>
171 <xsl:with-param name="parent" select="$element.name"/>
172 </xsl:apply-templates>
177 <xsl:template name="compute.renderas">
178 <xsl:variable name="output.value">
179 <xsl:call-template name="compute.output.value">
180 <xsl:with-param name="property">renderas</xsl:with-param>
184 <xsl:value-of select="$output.value"/>
188 <!-- This utility template is passed a value for output format
189 and the name of a property and computes the value
190 of the property from output children of context element -->
191 <xsl:template name="compute.output.value">
192 <xsl:param name="property" select="''"/>
194 <xsl:variable name="default.format"
195 select="ancestor::d:structure/@defaultformat"/>
197 <xsl:variable name="property.value">
199 <!-- if a child output element has a format that matches the param value
200 and it has that property -->
201 <!-- The format attribute can be multivalued -->
202 <xsl:when test="$output.format != '' and
203 d:output[contains(concat(' ', normalize-space(@format), ' '),
205 [@*[local-name() = $property]]">
207 select="d:output[contains(concat(' ', normalize-space(@format), ' '),
209 [@*[local-name() = $property]][1]
210 /@*[local-name() = $property]"/>
212 <!-- try with the structure's @defaultformat -->
213 <xsl:when test="$default.format != '' and
214 d:output[contains(concat(' ', normalize-space(@format), ' '),
216 [@*[local-name() = $property]]">
218 select="d:output[contains(concat(' ', normalize-space(@format), ' '),
220 [@*[local-name() = $property]][1]
221 /@*[local-name() = $property]"/>
223 <!-- try the first output with the property-->
224 <xsl:when test="d:output[@*[local-name() = $property]]">
226 select="d:output[@*[local-name() = $property]][1]
227 /@*[local-name() = $property]"/>
229 <!-- and try the module element itself -->
230 <xsl:when test="@*[local-name() = $property]">
232 select="@*[local-name() = $property]"/>
237 <xsl:value-of select="$property.value"/>
240 <xsl:template match="d:module[not(@resourceref)]" mode="compute.element.name">
242 <xsl:variable name="renderas">
243 <xsl:call-template name="compute.renderas"/>
247 <xsl:when test="string-length($renderas) != 0">
248 <xsl:value-of select="$renderas"/>
251 <xsl:message terminate="yes">
252 <xsl:text>ERROR: cannot determine output element name for </xsl:text>
253 <xsl:text>module with no @resourceref and no @renderas. Exiting.</xsl:text>
259 <xsl:template match="d:module[@resourceref]" mode="compute.element.name">
260 <xsl:param name="ref.name" select="''"/>
262 <xsl:variable name="renderas">
263 <xsl:call-template name="compute.renderas"/>
267 <xsl:when test="string-length($renderas) != 0">
268 <xsl:value-of select="$renderas"/>
270 <xsl:when test="$ref.name = 'topic' and
271 string-length($topic.default.renderas) != 0">
272 <xsl:value-of select="$topic.default.renderas"/>
274 <xsl:when test="string-length($ref.name) != 0">
275 <xsl:value-of select="$ref.name"/>
278 <xsl:message terminate="yes">
279 <xsl:text>ERROR: cannot determine output element name for </xsl:text>
280 <xsl:text>@resourceref="</xsl:text>
281 <xsl:value-of select="@resourceref"/>
282 <xsl:text>". Exiting.</xsl:text>
288 <xsl:template match="d:structure" mode="compute.element.name">
289 <xsl:param name="ref.name" select="''"/>
291 <xsl:variable name="renderas">
292 <xsl:call-template name="compute.renderas"/>
296 <xsl:when test="string-length($renderas) != 0">
297 <xsl:value-of select="$renderas"/>
299 <xsl:when test="string-length($ref.name) != 0">
300 <xsl:value-of select="$ref.name"/>
302 <xsl:when test="string-length($root.default.renderas) != 0">
303 <xsl:value-of select="$root.default.renderas"/>
306 <xsl:message terminate="yes">
307 <xsl:text>ERROR: cannot determine output element name for </xsl:text>
308 <xsl:text>structure with no @renderas and no $root.default.renderas. </xsl:text>
309 <xsl:text>Exiting.</xsl:text>
315 <xsl:template match="d:module[@resourceref] | d:structure[@resourceref]">
316 <xsl:param name="parent" select="''"/>
318 <xsl:variable name="module" select="."/>
319 <xsl:variable name="resourceref" select="@resourceref"/>
320 <xsl:variable name="resource" select="key('id', $resourceref)"/>
323 <xsl:when test="not($resource)">
324 <xsl:message terminate="yes">
325 <xsl:text>ERROR: no xml:id matches @resourceref = '</xsl:text>
326 <xsl:value-of select="$resourceref"/>
327 <xsl:text>'.</xsl:text>
330 <xsl:when test="not($resource/self::d:resource)">
331 <xsl:message terminate="yes">
332 <xsl:text>ERROR: xml:id matching @resourceref = '</xsl:text>
333 <xsl:value-of select="$resourceref"/>
334 <xsl:text> is not a resource element'.</xsl:text>
339 <xsl:variable name="fileref.att" select="$resource/@fileref"/>
340 <xsl:variable name="fileref">
342 <xsl:when test="$resource/ancestor::d:resources/@xml:base">
344 select="concat($resource/ancestor::d:resources[@xml:base][1]/@xml:base,
345 '/', $fileref.att)"/>
348 <xsl:value-of select="$fileref.att"/>
354 <xsl:when test="string-length($fileref) = 0">
355 <!-- A resource without @fileref gets its content copied -->
356 <xsl:apply-templates select="$resource/node()" mode="copycontent"/>
360 <xsl:variable name="ref.content" select="document($fileref,/)"/>
362 <xsl:variable name="ref.root" select="$ref.content/*[1]"/>
364 <xsl:if test="count($ref.root) = 0">
365 <xsl:message terminate="yes">
366 <xsl:text>ERROR: @fileref = '</xsl:text>
367 <xsl:value-of select="$fileref"/>
368 <xsl:text>' has no content or is unresolved.</xsl:text>
372 <xsl:variable name="element.name">
373 <xsl:apply-templates select="." mode="compute.element.name">
374 <xsl:with-param name="ref.name" select="local-name($ref.content/*[1])"/>
375 </xsl:apply-templates>
378 <xsl:variable name="omittitles.property">
379 <xsl:call-template name="compute.output.value">
380 <xsl:with-param name="property">omittitles</xsl:with-param>
384 <xsl:variable name="contentonly.property">
385 <xsl:call-template name="compute.output.value">
386 <xsl:with-param name="property">contentonly</xsl:with-param>
391 <xsl:when test="$contentonly.property = 'true' or
392 $contentonly.property = 'yes' or
393 $contentonly.property = '1'">
394 <xsl:apply-templates select="$ref.content/*[1]/node()" mode="copycontent">
395 <xsl:with-param name="omittitles" select="$omittitles.property"/>
396 </xsl:apply-templates>
398 <!-- use xsl:copy if using the ref element to get its namespaces -->
399 <xsl:when test="$element.name = local-name($ref.root)">
400 <!-- must use for-each to set context node for xsl:copy -->
401 <xsl:for-each select="$ref.root">
403 <xsl:copy-of select="@*[not(name = 'xml:id')]"/>
405 <!-- Use the module's xml:id if it has one -->
406 <xsl:when test="$module/@xml:id">
407 <xsl:attribute name="xml:id">
408 <xsl:value-of select="$module/@xml:id"/>
411 <!-- otherwise use the resource's id -->
413 <xsl:copy-of select="@xml:id"/>
417 <xsl:call-template name="merge.info">
418 <xsl:with-param name="merge.element" select="$module/d:merge"/>
419 <xsl:with-param name="ref.content" select="$ref.content"/>
420 <xsl:with-param name="omittitles" select="$omittitles.property"/>
423 <!-- copy through all but titles, which moved to info -->
424 <xsl:apply-templates select="node()
425 [not(local-name() = 'title') and
426 not(local-name() = 'subtitle') and
427 not(local-name() = 'info') and
428 not(local-name() = 'titleabbrev')]" mode="copycontent"/>
430 <xsl:apply-templates select="$module/node()">
431 <xsl:with-param name="parent" select="$element.name"/>
432 </xsl:apply-templates>
437 <!-- create the element instead of copying it -->
438 <xsl:element name="{$element.name}" namespace="http://docbook.org/ns/docbook">
439 <xsl:copy-of select="$ref.content/*[1]/@*[not(name = 'xml:id')]"/>
441 <!-- Use the module's xml:id if it has one -->
442 <xsl:when test="@xml:id">
443 <xsl:attribute name="xml:id">
444 <xsl:value-of select="@xml:id"/>
447 <!-- otherwise use the resource's id -->
448 <xsl:when test="$ref.content/*[1]/@xml:id">
449 <xsl:attribute name="xml:id">
450 <xsl:value-of select="$ref.content/*[1]/@xml:id"/>
455 <xsl:call-template name="merge.info">
456 <xsl:with-param name="merge.element" select="d:merge"/>
457 <xsl:with-param name="ref.content" select="$ref.content"/>
458 <xsl:with-param name="omittitles" select="$omittitles.property"/>
461 <!-- copy through all but titles, which moved to info -->
462 <xsl:apply-templates select="$ref.content/*[1]/node()
463 [not(local-name() = 'title') and
464 not(local-name() = 'subtitle') and
465 not(local-name() = 'info') and
466 not(local-name() = 'titleabbrev')]" mode="copycontent"/>
468 <xsl:apply-templates>
469 <xsl:with-param name="parent" select="$element.name"/>
470 </xsl:apply-templates>
478 <xsl:template name="merge.info">
479 <xsl:param name="merge.element" select="NOTANODE"/>
480 <xsl:param name="ref.content" select="NOTANODE"/>
481 <xsl:param name="omittitles"/>
483 <!-- a merge element may use resourceref as well as literal content -->
484 <!-- any literal content overrides the merge resourceref content -->
485 <xsl:variable name="merge.ref.content">
486 <xsl:if test="$merge.element/@resourceref">
487 <xsl:variable name="resourceref" select="$merge.element/@resourceref"/>
488 <xsl:variable name="resource" select="key('id', $resourceref)"/>
491 <xsl:when test="not($resource)">
492 <xsl:message terminate="yes">
493 <xsl:text>ERROR: no xml:id matches @resourceref = '</xsl:text>
494 <xsl:value-of select="$resourceref"/>
495 <xsl:text>'.</xsl:text>
498 <xsl:when test="not($resource/self::d:resource)">
499 <xsl:message terminate="yes">
500 <xsl:text>ERROR: xml:id matching @resourceref = '</xsl:text>
501 <xsl:value-of select="$resourceref"/>
502 <xsl:text> is not a resource element'.</xsl:text>
507 <xsl:variable name="fileref.att" select="$resource/@fileref"/>
509 <xsl:variable name="fileref">
511 <xsl:when test="$resource/ancestor::d:resources/@xml:base">
513 select="concat($resource/ancestor::d:resources[@xml:base][1]/@xml:base,
514 '/', $fileref.att)"/>
517 <xsl:value-of select="$fileref.att"/>
522 <xsl:if test="string-length($fileref) != 0">
523 <xsl:copy-of select="document($fileref,/)"/>
528 <xsl:variable name="merge.ref.info"
529 select="exsl:node-set($merge.ref.content)//d:info[1]"/>
531 <xsl:if test="$merge.element/@resourceref and not($merge.ref.info)">
532 <xsl:message terminate="yes">
533 <xsl:text>ERROR: merge element with resourceref '</xsl:text>
534 <xsl:value-of select="$merge.element/@resourceref"/>
535 <xsl:text>' must point to something with an info element.'</xsl:text>
539 <xsl:variable name="omittitles.boolean">
541 <xsl:when test="$omittitles = 'yes' or $omittitles = 'true' or omittitles = '1'">
542 <xsl:value-of select="1"/>
545 <xsl:value-of select="0"/>
549 <!-- output info if there is any -->
550 <xsl:if test="$merge.element/node() or
551 $merge.ref.info/node() or
552 $ref.content/*/d:info/node() or
553 $ref.content/*/d:title[$omittitles.boolean = 0] or
554 $ref.content/*/d:subtitle[$omittitles.boolean = 0] or
555 $ref.content/*/d:titleabbrev[$omittitles.boolean = 0]">
557 <xsl:variable name="ref.info" select="$ref.content/*/d:info"/>
558 <xsl:variable name="ref.title" select="$ref.content/*/d:title"/>
559 <xsl:variable name="ref.subtitle" select="$ref.content/*/d:subtitle"/>
560 <xsl:variable name="ref.titleabbrev" select="$ref.content/*/d:titleabbrev"/>
561 <xsl:variable name="ref.info.title" select="$ref.content/*/d:info/d:title"/>
562 <xsl:variable name="ref.info.subtitle" select="$ref.content/*/d:info/d:subtitle"/>
563 <xsl:variable name="ref.info.titleabbrev" select="$ref.content/*/d:info/d:titleabbrev"/>
566 <!-- First copy through any merge attributes and elements and comments -->
567 <xsl:copy-of select="$merge.element/@*[not(local-name(.) = 'resourceref')]"/>
569 <!-- And copy any resource info attributes not in merge-->
570 <xsl:for-each select="$ref.info/@*">
571 <xsl:variable name="resource.att" select="local-name(.)"/>
573 <xsl:when test="$merge.element/@*[local-name(.) = $resource.att]">
574 <!-- do nothing because overridden -->
577 <!-- copy through if not overridden -->
578 <xsl:copy-of select="."/>
583 <!-- Copy through the merge children as they have highest priority -->
584 <xsl:copy-of select="$merge.element/node()"/>
586 <!-- and copy through those merge resource elements not in merge element -->
587 <xsl:for-each select="$merge.ref.info/node()">
588 <xsl:variable name="resource.node" select="local-name(.)"/>
590 <xsl:when test="$merge.element/node()[local-name(.) = $resource.node]">
591 <!-- do nothing because overridden -->
594 <!-- copy through -->
595 <xsl:copy-of select="."/>
600 <!-- And copy any module's resource info node not in merge or merge.ref -->
601 <xsl:for-each select="$ref.info/node() |
602 $ref.title[$omittitles.boolean = 0] |
603 $ref.subtitle[$omittitles.boolean = 0] |
604 $ref.titleabbrev[$omittitles.boolean = 0] |
605 $ref.info.title[$omittitles.boolean = 0] |
606 $ref.info.subtitle[$omittitles.boolean = 0] |
607 $ref.info.titleabbrev[$omittitles.boolean = 0]">
608 <xsl:variable name="resource.node" select="local-name(.)"/>
610 <xsl:when test="$merge.element/node()[local-name(.) = $resource.node]">
611 <!-- do nothing because overridden -->
613 <xsl:when test="$merge.ref.info/node()[local-name(.) = $resource.node]">
614 <!-- do nothing because overridden -->
617 <!-- copy through -->
618 <xsl:copy-of select="."/>
627 <xsl:template match="d:relationships">
629 <xsl:text>WARNING: the <relationships> element is not currently </xsl:text>
630 <xsl:text>supported by this stylesheet.</xsl:text>
634 <xsl:template match="d:transforms">
636 <xsl:text>WARNING: the <transforms> element is not currently </xsl:text>
637 <xsl:text>supported by this stylesheet.</xsl:text>
641 <xsl:template match="d:filterin">
643 <xsl:text>WARNING: the <filterin> element is not currently </xsl:text>
644 <xsl:text>supported by this stylesheet.</xsl:text>
648 <xsl:template match="d:filterout">
650 <xsl:text>WARNING: the <filterin> element is not currently </xsl:text>
651 <xsl:text>supported by this stylesheet.</xsl:text>