Imported Upstream version 1.79.2
[platform/upstream/docbook-xsl-stylesheets.git] / fo / xref.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:d="http://docbook.org/ns/docbook"
4                 xmlns:fo="http://www.w3.org/1999/XSL/Format"
5                 xmlns:exsl="http://exslt.org/common"
6                 xmlns:xlink='http://www.w3.org/1999/xlink'
7                 exclude-result-prefixes="exsl xlink d"
8                 version='1.0'>
9
10 <!-- ********************************************************************
11
12      This file is part of the XSL DocBook Stylesheet distribution.
13      See ../README or http://cdn.docbook.org/release/xsl/current/ for
14      copyright and other information.
15
16      ******************************************************************** -->
17
18 <!-- Use internal variable for olink xlink role for consistency -->
19 <xsl:variable 
20       name="xolink.role">http://docbook.org/xlink/role/olink</xsl:variable>
21
22 <!-- ==================================================================== -->
23
24 <xsl:template match="d:anchor">
25   <xsl:variable name="id">
26     <xsl:call-template name="object.id"/>
27   </xsl:variable>
28
29   <xsl:variable name="wrapper.name">
30     <xsl:call-template name="inline.or.block"/>
31   </xsl:variable>
32
33   <xsl:element name="{$wrapper.name}">
34     <xsl:attribute name="id">
35       <xsl:value-of select="$id"/>
36     </xsl:attribute>
37   </xsl:element>
38 </xsl:template>
39
40 <!-- ==================================================================== -->
41
42 <xsl:template match="d:xref" name="xref">
43   <xsl:param name="xhref" select="@xlink:href"/>
44   <!-- is the @xlink:href a local idref link? -->
45   <xsl:param name="xlink.idref">
46     <xsl:if test="starts-with($xhref,'#')
47                   and (not(contains($xhref,'&#40;'))
48                   or starts-with($xhref, '#xpointer&#40;id&#40;'))">
49       <xsl:call-template name="xpointer.idref">
50         <xsl:with-param name="xpointer" select="$xhref"/>
51       </xsl:call-template>
52    </xsl:if>
53   </xsl:param>
54   <xsl:param name="xlink.targets" select="key('id',$xlink.idref)"/>
55   <xsl:param name="linkend.targets" select="key('id',@linkend)"/>
56   <xsl:param name="target" select="($xlink.targets | $linkend.targets)[1]"/>
57   <xsl:param name="refelem" select="local-name($target)"/>
58   <xsl:param name="referrer" select="."/>
59   <xsl:param name="xrefstyle">
60     <xsl:apply-templates select="." mode="xrefstyle">
61       <xsl:with-param name="target" select="$target"/>
62       <xsl:with-param name="referrer" select="$referrer"/>
63     </xsl:apply-templates>
64   </xsl:param>
65
66   <xsl:variable name="content">
67     <fo:inline xsl:use-attribute-sets="xref.properties">
68       <xsl:choose>
69         <xsl:when test="@endterm">
70           <xsl:variable name="etargets" select="key('id',@endterm)"/>
71           <xsl:variable name="etarget" select="$etargets[1]"/>
72           <xsl:choose>
73             <xsl:when test="count($etarget) = 0">
74               <xsl:message>
75                 <xsl:value-of select="count($etargets)"/>
76                 <xsl:text>Endterm points to nonexistent ID: </xsl:text>
77                 <xsl:value-of select="@endterm"/>
78               </xsl:message>
79               <xsl:text>???</xsl:text>
80             </xsl:when>
81             <xsl:otherwise>
82               <xsl:apply-templates select="$etarget" mode="endterm"/>
83             </xsl:otherwise>
84           </xsl:choose>
85         </xsl:when>
86   
87         <xsl:when test="$target/@xreflabel">
88           <xsl:call-template name="xref.xreflabel">
89             <xsl:with-param name="target" select="$target"/>
90           </xsl:call-template>
91         </xsl:when>
92   
93         <xsl:when test="$target">
94           <xsl:if test="not(parent::d:citation)">
95             <xsl:apply-templates select="$target" mode="xref-to-prefix">
96               <xsl:with-param name="referrer" select="."/>
97               <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
98             </xsl:apply-templates>
99           </xsl:if>
100   
101           <xsl:apply-templates select="$target" mode="xref-to">
102             <xsl:with-param name="referrer" select="."/>
103             <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
104           </xsl:apply-templates>
105   
106           <xsl:if test="not(parent::d:citation)">
107             <xsl:apply-templates select="$target" mode="xref-to-suffix">
108               <xsl:with-param name="referrer" select="."/>
109               <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
110             </xsl:apply-templates>
111           </xsl:if>
112         </xsl:when>
113         <xsl:otherwise>
114           <xsl:message>
115             <xsl:text>ERROR: xref linking to </xsl:text>
116             <xsl:value-of select="@linkend|@xlink:href"/>
117             <xsl:text> has no generated link text.</xsl:text>
118           </xsl:message>
119           <xsl:text>???</xsl:text>
120         </xsl:otherwise>
121       </xsl:choose>
122     </fo:inline>
123   </xsl:variable>
124
125   <!-- Convert it into an active link -->
126   <xsl:call-template name="simple.xlink">
127     <xsl:with-param name="content" select="$content"/>
128   </xsl:call-template>
129
130   <!-- Add standard page reference? -->
131   <xsl:choose>
132     <xsl:when test="not($target)">
133       <!-- page numbers only for local targets -->
134     </xsl:when>
135     <xsl:when test="starts-with(normalize-space($xrefstyle), 'select:') 
136                   and contains($xrefstyle, 'nopage')">
137       <!-- negative xrefstyle in instance turns it off -->
138     </xsl:when>
139     <xsl:when test="starts-with(normalize-space($xrefstyle), 'template:')">
140       <!-- if page citation were wanted, it would've been in the template as %p -->
141     </xsl:when>
142     <!-- positive xrefstyle already handles it -->
143     <xsl:when test="not(starts-with(normalize-space($xrefstyle), 'select:') 
144                   and (contains($xrefstyle, 'page')
145                        or contains($xrefstyle, 'Page')))
146                   and ( $insert.xref.page.number = 'yes' 
147                      or $insert.xref.page.number = '1')
148                   or (local-name($target) = 'para'
149                      and $xrefstyle = ''
150                      and $insert.xref.page.number.para = 'yes')">
151       <xsl:apply-templates select="$target" mode="page.citation">
152         <xsl:with-param name="id" select="$target/@id|$target/@xml:id"/>
153         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
154       </xsl:apply-templates>
155     </xsl:when>
156   </xsl:choose>
157 </xsl:template>
158
159 <!-- ==================================================================== -->
160
161 <!-- Handled largely like an xref -->
162 <!-- To be done: add support for begin, end, and units attributes -->
163 <xsl:template match="d:biblioref" name="biblioref">
164   <xsl:variable name="targets" select="key('id',@linkend)"/>
165   <xsl:variable name="target" select="$targets[1]"/>
166   <xsl:variable name="referrer" select="."/>
167   <xsl:variable name="refelem" select="local-name($target)"/>
168
169   <xsl:variable name="xrefstyle">
170     <xsl:apply-templates select="." mode="xrefstyle">
171       <xsl:with-param name="target" select="$target"/>
172       <xsl:with-param name="referrer" select="$referrer"/>
173     </xsl:apply-templates>
174   </xsl:variable>
175
176   <xsl:call-template name="check.id.unique">
177     <xsl:with-param name="linkend" select="@linkend"/>
178   </xsl:call-template>
179
180   <xsl:choose>
181     <xsl:when test="$refelem=''">
182       <xsl:message>
183         <xsl:text>XRef to nonexistent id: </xsl:text>
184         <xsl:value-of select="@linkend"/>
185       </xsl:message>
186       <xsl:text>???</xsl:text>
187     </xsl:when>
188
189     <xsl:when test="@endterm">
190       <fo:basic-link internal-destination="{@linkend}"
191                      xsl:use-attribute-sets="xref.properties">
192         <xsl:variable name="etargets" select="key('id',@endterm)"/>
193         <xsl:variable name="etarget" select="$etargets[1]"/>
194         <xsl:choose>
195           <xsl:when test="count($etarget) = 0">
196             <xsl:message>
197               <xsl:value-of select="count($etargets)"/>
198               <xsl:text>Endterm points to nonexistent ID: </xsl:text>
199               <xsl:value-of select="@endterm"/>
200             </xsl:message>
201             <xsl:text>???</xsl:text>
202           </xsl:when>
203           <xsl:otherwise>
204             <xsl:apply-templates select="$etarget" mode="endterm"/>
205           </xsl:otherwise>
206         </xsl:choose>
207       </fo:basic-link>
208     </xsl:when>
209
210     <xsl:when test="$target/@xreflabel">
211       <fo:basic-link internal-destination="{@linkend}"
212                      xsl:use-attribute-sets="xref.properties">
213         <xsl:call-template name="xref.xreflabel">
214           <xsl:with-param name="target" select="$target"/>
215         </xsl:call-template>
216       </fo:basic-link>
217     </xsl:when>
218
219     <xsl:otherwise>
220       <xsl:if test="not(parent::d:citation)">
221         <xsl:apply-templates select="$target" mode="xref-to-prefix">
222           <xsl:with-param name="referrer" select="."/>
223           <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
224         </xsl:apply-templates>
225       </xsl:if>
226
227       <fo:basic-link internal-destination="{@linkend}"
228                      xsl:use-attribute-sets="xref.properties">
229         <xsl:apply-templates select="$target" mode="xref-to">
230           <xsl:with-param name="referrer" select="."/>
231           <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
232         </xsl:apply-templates>
233       </fo:basic-link>
234
235       <xsl:if test="not(parent::d:citation)">
236         <xsl:apply-templates select="$target" mode="xref-to-suffix">
237           <xsl:with-param name="referrer" select="."/>
238           <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
239         </xsl:apply-templates>
240       </xsl:if>
241     </xsl:otherwise>
242   </xsl:choose>
243
244 </xsl:template>
245
246 <!-- ==================================================================== -->
247
248 <xsl:template match="*" mode="endterm">
249   <!-- Process the children of the endterm element -->
250   <xsl:variable name="endterm">
251     <xsl:apply-templates select="child::node()"/>
252   </xsl:variable>
253
254   <xsl:choose>
255     <xsl:when test="$exsl.node.set.available != 0">
256       <xsl:apply-templates select="exsl:node-set($endterm)" mode="remove-ids"/>
257     </xsl:when>
258     <xsl:otherwise>
259       <xsl:copy-of select="$endterm"/>
260     </xsl:otherwise>
261   </xsl:choose>
262 </xsl:template>
263
264 <xsl:template match="*" mode="remove-ids">
265   <xsl:copy>
266     <xsl:for-each select="@*">
267       <xsl:choose>
268         <xsl:when test="local-name(.) != 'id'">
269           <xsl:copy/>
270         </xsl:when>
271         <xsl:otherwise>
272           <xsl:message>removing <xsl:value-of select="name(.)"/></xsl:message>
273         </xsl:otherwise>
274       </xsl:choose>
275     </xsl:for-each>
276     <xsl:apply-templates mode="remove-ids"/>
277   </xsl:copy>
278 </xsl:template>
279
280 <!--- ==================================================================== -->
281
282 <xsl:template match="*" mode="xref-to-prefix">
283   <xsl:param name="referrer"/>
284   <xsl:param name="xrefstyle"/>
285   <xsl:param name="verbose" select="1"/>
286 </xsl:template>
287 <xsl:template match="*" mode="xref-to-suffix">
288   <xsl:param name="referrer"/>
289   <xsl:param name="xrefstyle"/>
290   <xsl:param name="verbose" select="1"/>
291 </xsl:template>
292
293 <xsl:template match="*" mode="xref-to">
294   <xsl:param name="referrer"/>
295   <xsl:param name="xrefstyle"/>
296   <xsl:param name="verbose" select="1"/>
297
298   <xsl:if test="$verbose != 0">
299     <xsl:message>
300       <xsl:text>Don't know what gentext to create for xref to: "</xsl:text>
301       <xsl:value-of select="name(.)"/>
302       <xsl:text>"</xsl:text>
303     </xsl:message>
304     <xsl:text>???</xsl:text>
305   </xsl:if>
306 </xsl:template>
307
308 <xsl:template match="d:title" mode="xref-to">
309   <xsl:param name="referrer"/>
310   <xsl:param name="xrefstyle"/>
311   <xsl:param name="verbose" select="1"/>
312
313   <!-- if you xref to a title, xref to the parent... -->
314   <xsl:choose>
315     <!-- FIXME: how reliable is this? -->
316     <xsl:when test="contains(local-name(parent::*), 'info')">
317       <xsl:apply-templates select="parent::*[2]" mode="xref-to">
318         <xsl:with-param name="referrer" select="$referrer"/>
319         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
320         <xsl:with-param name="verbose" select="$verbose"/>
321       </xsl:apply-templates>
322     </xsl:when>
323     <xsl:otherwise>
324       <xsl:apply-templates select="parent::*" mode="xref-to">
325         <xsl:with-param name="referrer" select="$referrer"/>
326         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
327         <xsl:with-param name="verbose" select="$verbose"/>
328       </xsl:apply-templates>
329     </xsl:otherwise>
330   </xsl:choose>
331 </xsl:template>
332
333 <xsl:template match="d:abstract|d:article|d:authorblurb|d:bibliodiv|d:bibliomset
334                      |d:biblioset|d:blockquote|d:calloutlist|d:caution|d:colophon
335                      |d:constraintdef|d:formalpara|d:glossdiv|d:important|d:indexdiv
336                      |d:itemizedlist|d:legalnotice|d:lot|d:msg|d:msgexplan|d:msgmain
337                      |d:msgrel|d:msgset|d:msgsub|d:note|d:orderedlist|d:partintro
338                      |d:productionset|d:qandadiv|d:refsynopsisdiv|d:screenshot|d:segmentedlist
339                      |d:set|d:setindex|d:sidebar|d:tip|d:toc|d:variablelist|d:warning"
340               mode="xref-to">
341   <xsl:param name="referrer"/>
342   <xsl:param name="xrefstyle"/>
343   <xsl:param name="verbose" select="1"/>
344
345   <!-- catch-all for things with (possibly optional) titles -->
346   <xsl:apply-templates select="." mode="object.xref.markup">
347     <xsl:with-param name="purpose" select="'xref'"/>
348     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
349     <xsl:with-param name="referrer" select="$referrer"/>
350     <xsl:with-param name="verbose" select="$verbose"/>
351   </xsl:apply-templates>
352 </xsl:template>
353
354 <xsl:template match="d:author|d:editor|d:othercredit|d:personname" mode="xref-to">
355   <xsl:param name="referrer"/>
356   <xsl:param name="xrefstyle"/>
357   <xsl:param name="verbose" select="1"/>
358
359   <xsl:call-template name="person.name"/>
360 </xsl:template>
361
362 <xsl:template match="d:authorgroup" mode="xref-to">
363   <xsl:param name="referrer"/>
364   <xsl:param name="xrefstyle"/>
365   <xsl:param name="verbose" select="1"/>
366
367   <xsl:call-template name="person.name.list"/>
368 </xsl:template>
369
370 <xsl:template match="d:figure|d:example|d:table|d:equation" mode="xref-to">
371   <xsl:param name="referrer"/>
372   <xsl:param name="xrefstyle"/>
373   <xsl:param name="verbose" select="1"/>
374
375   <xsl:apply-templates select="." mode="object.xref.markup">
376     <xsl:with-param name="purpose" select="'xref'"/>
377     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
378     <xsl:with-param name="referrer" select="$referrer"/>
379     <xsl:with-param name="verbose" select="$verbose"/>
380   </xsl:apply-templates>
381 </xsl:template>
382
383 <xsl:template match="d:procedure" mode="xref-to">
384   <xsl:param name="referrer"/>
385   <xsl:param name="xrefstyle"/>
386   <xsl:param name="verbose"/>
387
388   <xsl:apply-templates select="." mode="object.xref.markup">
389     <xsl:with-param name="purpose" select="'xref'"/>
390     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
391     <xsl:with-param name="referrer" select="$referrer"/>
392     <xsl:with-param name="verbose" select="$verbose"/>
393   </xsl:apply-templates>
394 </xsl:template>
395
396 <xsl:template match="d:task" mode="xref-to">
397   <xsl:param name="referrer"/>
398   <xsl:param name="xrefstyle"/>
399   <xsl:param name="verbose"/>
400
401   <xsl:apply-templates select="." mode="object.xref.markup">
402     <xsl:with-param name="purpose" select="'xref'"/>
403     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
404     <xsl:with-param name="referrer" select="$referrer"/>
405     <xsl:with-param name="verbose" select="$verbose"/>
406   </xsl:apply-templates>
407 </xsl:template>
408
409 <xsl:template match="d:cmdsynopsis" mode="xref-to">
410   <xsl:param name="referrer"/>
411   <xsl:param name="xrefstyle"/>
412   <xsl:param name="verbose" select="1"/>
413
414   <xsl:apply-templates select="(.//d:command)[1]" mode="xref"/>
415 </xsl:template>
416
417 <xsl:template match="d:funcsynopsis" mode="xref-to">
418   <xsl:param name="referrer"/>
419   <xsl:param name="xrefstyle"/>
420   <xsl:param name="verbose" select="1"/>
421
422   <xsl:apply-templates select="(.//d:function)[1]" mode="xref"/>
423 </xsl:template>
424
425 <xsl:template match="d:dedication|d:acknowledgements|d:preface|d:chapter|d:appendix" mode="xref-to">
426   <xsl:param name="referrer"/>
427   <xsl:param name="xrefstyle"/>
428   <xsl:param name="verbose" select="1"/>
429
430   <xsl:apply-templates select="." mode="object.xref.markup">
431     <xsl:with-param name="purpose" select="'xref'"/>
432     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
433     <xsl:with-param name="referrer" select="$referrer"/>
434     <xsl:with-param name="verbose" select="$verbose"/>
435   </xsl:apply-templates>
436 </xsl:template>
437
438 <xsl:template match="d:bibliography" mode="xref-to">
439   <xsl:param name="referrer"/>
440   <xsl:param name="xrefstyle"/>
441   <xsl:param name="verbose" select="1"/>
442
443   <xsl:apply-templates select="." mode="object.xref.markup">
444     <xsl:with-param name="purpose" select="'xref'"/>
445     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
446     <xsl:with-param name="referrer" select="$referrer"/>
447     <xsl:with-param name="verbose" select="$verbose"/>
448   </xsl:apply-templates>
449 </xsl:template>
450
451 <xsl:template match="d:biblioentry|d:bibliomixed" mode="xref-to-prefix">
452   <xsl:param name="referrer"/>
453   <xsl:param name="xrefstyle"/>
454   <xsl:text>[</xsl:text>
455 </xsl:template>
456
457 <xsl:template match="d:biblioentry|d:bibliomixed" mode="xref-to-suffix">
458   <xsl:param name="referrer"/>
459   <xsl:param name="xrefstyle"/>
460   <xsl:text>]</xsl:text>
461 </xsl:template>
462
463 <xsl:template match="d:biblioentry|d:bibliomixed" mode="xref-to">
464   <xsl:param name="referrer"/>
465   <xsl:param name="xrefstyle"/>
466   <xsl:param name="verbose" select="1"/>
467
468   <!-- handles both biblioentry and bibliomixed -->
469   <xsl:choose>
470     <xsl:when test="string(.) = ''">
471       <xsl:variable name="bib" select="document($bibliography.collection,.)"/>
472       <xsl:variable name="id" select="(@id|@xml:id)[1]"/>
473       <xsl:variable name="entry" select="$bib/d:bibliography/
474                                          *[@id=$id or @xml:id=$id][1]"/>
475       <xsl:choose>
476         <xsl:when test="$entry">
477           <xsl:choose>
478             <xsl:when test="$bibliography.numbered != 0">
479               <xsl:number from="d:bibliography" count="d:biblioentry|d:bibliomixed"
480                           level="any" format="1"/>
481             </xsl:when>
482             <xsl:when test="local-name($entry/*[1]) = 'abbrev'">
483               <xsl:apply-templates select="$entry/*[1]"/>
484             </xsl:when>
485             <xsl:otherwise>
486               <xsl:value-of select="(@id|@xml:id)[1]"/>
487             </xsl:otherwise>
488           </xsl:choose>
489         </xsl:when>
490         <xsl:otherwise>
491           <xsl:message>
492             <xsl:text>No bibliography entry: </xsl:text>
493             <xsl:value-of select="$id"/>
494             <xsl:text> found in </xsl:text>
495             <xsl:value-of select="$bibliography.collection"/>
496           </xsl:message>
497           <xsl:value-of select="(@id|@xml:id)[1]"/>
498         </xsl:otherwise>
499       </xsl:choose>
500     </xsl:when>
501     <xsl:otherwise>
502       <xsl:choose>
503         <xsl:when test="$bibliography.numbered != 0">
504           <xsl:number from="d:bibliography" count="d:biblioentry|d:bibliomixed"
505                       level="any" format="1"/>
506         </xsl:when>
507         <xsl:when test="local-name(*[1]) = 'abbrev'">
508           <xsl:apply-templates select="*[1]"/>
509         </xsl:when>
510         <xsl:otherwise>
511           <xsl:value-of select="(@id|@xml:id)[1]"/>
512         </xsl:otherwise>
513       </xsl:choose>
514     </xsl:otherwise>
515   </xsl:choose>
516 </xsl:template>
517
518 <xsl:template match="d:glossary" mode="xref-to">
519   <xsl:param name="referrer"/>
520   <xsl:param name="xrefstyle"/>
521   <xsl:param name="verbose" select="1"/>
522
523   <xsl:apply-templates select="." mode="object.xref.markup">
524     <xsl:with-param name="purpose" select="'xref'"/>
525     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
526     <xsl:with-param name="referrer" select="$referrer"/>
527     <xsl:with-param name="verbose" select="$verbose"/>
528   </xsl:apply-templates>
529 </xsl:template>
530
531 <xsl:template match="d:glossentry" mode="xref-to">
532   <xsl:choose>
533     <xsl:when test="$glossentry.show.acronym = 'primary'">
534       <xsl:choose>
535         <xsl:when test="d:acronym|d:abbrev">
536           <xsl:apply-templates select="(d:acronym|d:abbrev)[1]"/>
537         </xsl:when>
538         <xsl:otherwise>
539           <xsl:apply-templates select="d:glossterm[1]" mode="xref-to"/>
540         </xsl:otherwise>
541       </xsl:choose>
542     </xsl:when>
543     <xsl:otherwise>
544       <xsl:apply-templates select="d:glossterm[1]" mode="xref-to"/>
545     </xsl:otherwise>
546   </xsl:choose>
547 </xsl:template>
548
549 <xsl:template match="d:glossterm|d:firstterm" mode="xref-to">
550   <xsl:apply-templates mode="no.anchor.mode"/>
551 </xsl:template>
552
553 <xsl:template match="d:index" mode="xref-to">
554   <xsl:param name="referrer"/>
555   <xsl:param name="xrefstyle"/>
556   <xsl:param name="verbose" select="1"/>
557
558   <xsl:apply-templates select="." mode="object.xref.markup">
559     <xsl:with-param name="purpose" select="'xref'"/>
560     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
561     <xsl:with-param name="referrer" select="$referrer"/>
562     <xsl:with-param name="verbose" select="$verbose"/>
563   </xsl:apply-templates>
564 </xsl:template>
565
566 <xsl:template match="d:listitem" mode="xref-to">
567   <xsl:param name="referrer"/>
568   <xsl:param name="xrefstyle"/>
569   <xsl:param name="verbose" select="1"/>
570
571   <xsl:apply-templates select="." mode="object.xref.markup">
572     <xsl:with-param name="purpose" select="'xref'"/>
573     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
574     <xsl:with-param name="referrer" select="$referrer"/>
575     <xsl:with-param name="verbose" select="$verbose"/>
576   </xsl:apply-templates>
577 </xsl:template>
578
579 <xsl:template match="d:section|d:simplesect
580                      |d:sect1|d:sect2|d:sect3|d:sect4|d:sect5
581                      |d:refsect1|d:refsect2|d:refsect3|d:refsection" mode="xref-to">
582   <xsl:param name="referrer"/>
583   <xsl:param name="xrefstyle"/>
584   <xsl:param name="verbose" select="1"/>
585
586   <xsl:apply-templates select="." mode="object.xref.markup">
587     <xsl:with-param name="purpose" select="'xref'"/>
588     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
589     <xsl:with-param name="referrer" select="$referrer"/>
590     <xsl:with-param name="verbose" select="$verbose"/>
591   </xsl:apply-templates>
592   <!-- What about "in Chapter X"? -->
593 </xsl:template>
594
595 <xsl:template match="d:topic" mode="xref-to">
596   <xsl:param name="referrer"/>
597   <xsl:param name="xrefstyle"/>
598   <xsl:param name="verbose" select="1"/>
599
600   <xsl:apply-templates select="." mode="object.xref.markup">
601     <xsl:with-param name="purpose" select="'xref'"/>
602     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
603     <xsl:with-param name="referrer" select="$referrer"/>
604     <xsl:with-param name="verbose" select="$verbose"/>
605   </xsl:apply-templates>
606 </xsl:template>
607
608 <xsl:template match="d:bridgehead" mode="xref-to">
609   <xsl:param name="referrer"/>
610   <xsl:param name="xrefstyle"/>
611   <xsl:param name="verbose" select="1"/>
612
613   <xsl:apply-templates select="." mode="object.xref.markup">
614     <xsl:with-param name="purpose" select="'xref'"/>
615     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
616     <xsl:with-param name="referrer" select="$referrer"/>
617     <xsl:with-param name="verbose" select="$verbose"/>
618   </xsl:apply-templates>
619   <!-- What about "in Chapter X"? -->
620 </xsl:template>
621
622 <xsl:template match="d:qandaset" mode="xref-to">
623   <xsl:param name="referrer"/>
624   <xsl:param name="xrefstyle"/>
625   <xsl:param name="verbose" select="1"/>
626
627   <xsl:apply-templates select="." mode="object.xref.markup">
628     <xsl:with-param name="purpose" select="'xref'"/>
629     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
630     <xsl:with-param name="referrer" select="$referrer"/>
631     <xsl:with-param name="verbose" select="$verbose"/>
632   </xsl:apply-templates>
633 </xsl:template>
634
635 <xsl:template match="d:qandadiv" mode="xref-to">
636   <xsl:param name="referrer"/>
637   <xsl:param name="xrefstyle"/>
638   <xsl:param name="verbose" select="1"/>
639
640   <xsl:apply-templates select="." mode="object.xref.markup">
641     <xsl:with-param name="purpose" select="'xref'"/>
642     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
643     <xsl:with-param name="referrer" select="$referrer"/>
644     <xsl:with-param name="verbose" select="$verbose"/>
645   </xsl:apply-templates>
646 </xsl:template>
647
648 <xsl:template match="d:qandaentry" mode="xref-to">
649   <xsl:param name="referrer"/>
650   <xsl:param name="xrefstyle"/>
651   <xsl:param name="verbose" select="1"/>
652
653   <xsl:apply-templates select="d:question[1]" mode="xref-to">
654     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
655     <xsl:with-param name="referrer" select="$referrer"/>
656     <xsl:with-param name="verbose" select="$verbose"/>
657   </xsl:apply-templates>
658 </xsl:template>
659
660 <xsl:template match="d:question|d:answer" mode="xref-to">
661   <xsl:param name="referrer"/>
662   <xsl:param name="xrefstyle"/>
663   <xsl:param name="verbose" select="1"/>
664
665   <xsl:choose>
666     <xsl:when test="string-length(d:label) != 0">
667       <xsl:apply-templates select="." mode="label.markup"/>
668     </xsl:when>
669     <xsl:otherwise>
670       <xsl:apply-templates select="." mode="object.xref.markup">
671         <xsl:with-param name="purpose" select="'xref'"/>
672         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
673         <xsl:with-param name="referrer" select="$referrer"/>
674         <xsl:with-param name="verbose" select="$verbose"/>
675       </xsl:apply-templates>
676     </xsl:otherwise>
677   </xsl:choose>
678 </xsl:template>
679
680 <xsl:template match="d:part|d:reference" mode="xref-to">
681   <xsl:param name="referrer"/>
682   <xsl:param name="xrefstyle"/>
683   <xsl:param name="verbose" select="1"/>
684
685   <xsl:apply-templates select="." mode="object.xref.markup">
686     <xsl:with-param name="purpose" select="'xref'"/>
687     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
688     <xsl:with-param name="referrer" select="$referrer"/>
689     <xsl:with-param name="verbose" select="$verbose"/>
690   </xsl:apply-templates>
691 </xsl:template>
692
693 <xsl:template match="d:refentry" mode="xref-to">
694   <xsl:param name="referrer"/>
695   <xsl:param name="xrefstyle"/>
696   <xsl:param name="verbose" select="1"/>
697
698   <xsl:choose>
699     <xsl:when test="d:refmeta/d:refentrytitle">
700       <xsl:apply-templates select="d:refmeta/d:refentrytitle"/>
701     </xsl:when>
702     <xsl:otherwise>
703       <xsl:apply-templates select="d:refnamediv/d:refname[1]"/>
704     </xsl:otherwise>
705   </xsl:choose>
706   <xsl:apply-templates select="d:refmeta/d:manvolnum"/>
707 </xsl:template>
708
709 <xsl:template match="d:refnamediv" mode="xref-to">
710   <xsl:param name="referrer"/>
711   <xsl:param name="xrefstyle"/>
712   <xsl:param name="verbose" select="1"/>
713
714   <xsl:apply-templates select="d:refname[1]" mode="xref-to">
715     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
716     <xsl:with-param name="referrer" select="$referrer"/>
717     <xsl:with-param name="verbose" select="$verbose"/>
718   </xsl:apply-templates>
719 </xsl:template>
720
721 <xsl:template match="d:refname" mode="xref-to">
722   <xsl:param name="referrer"/>
723   <xsl:param name="xrefstyle"/>
724   <xsl:param name="verbose" select="1"/>
725
726   <xsl:apply-templates mode="xref-to">
727     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
728     <xsl:with-param name="referrer" select="$referrer"/>
729     <xsl:with-param name="verbose" select="$verbose"/>
730   </xsl:apply-templates>
731 </xsl:template>
732
733 <xsl:template match="d:step" mode="xref-to">
734   <xsl:param name="referrer"/>
735   <xsl:param name="xrefstyle"/>
736   <xsl:param name="verbose" select="1"/>
737
738   <xsl:call-template name="gentext">
739     <xsl:with-param name="key" select="'Step'"/>
740   </xsl:call-template>
741   <xsl:text> </xsl:text>
742   <xsl:apply-templates select="." mode="number"/>
743 </xsl:template>
744
745 <xsl:template match="d:varlistentry" mode="xref-to">
746   <xsl:param name="referrer"/>
747   <xsl:param name="xrefstyle"/>
748   <xsl:param name="verbose" select="1"/>
749
750   <xsl:apply-templates select="d:term[1]" mode="xref-to">
751     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
752     <xsl:with-param name="referrer" select="$referrer"/>
753     <xsl:with-param name="verbose" select="$verbose"/>
754   </xsl:apply-templates>
755 </xsl:template>
756
757 <xsl:template match="d:varlistentry/d:term" mode="xref-to">
758   <xsl:param name="verbose" select="1"/>
759   <!-- avoids the comma that will be generated if there are several terms -->
760   <!-- Use no.anchor.mode to turn off nested xrefs and indexterms
761        in link text -->
762   <xsl:apply-templates mode="no.anchor.mode"/>
763 </xsl:template>
764
765 <xsl:template match="d:co" mode="xref-to">
766   <xsl:param name="referrer"/>
767   <xsl:param name="xrefstyle"/>
768   <xsl:param name="verbose" select="1"/>
769
770   <xsl:apply-templates select="." mode="callout-bug"/>
771 </xsl:template>
772
773 <xsl:template match="d:area|d:areaset" mode="xref-to">
774   <xsl:param name="referrer"/>
775   <xsl:param name="xrefstyle"/>
776
777   <xsl:call-template name="callout-bug">
778     <xsl:with-param name="conum">
779       <xsl:apply-templates select="." mode="conumber"/>
780     </xsl:with-param>
781   </xsl:call-template>
782 </xsl:template>
783
784 <xsl:template match="d:book" mode="xref-to">
785   <xsl:param name="referrer"/>
786   <xsl:param name="xrefstyle"/>
787   <xsl:param name="verbose" select="1"/>
788
789   <xsl:apply-templates select="." mode="object.xref.markup">
790     <xsl:with-param name="purpose" select="'xref'"/>
791     <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
792     <xsl:with-param name="referrer" select="$referrer"/>
793     <xsl:with-param name="verbose" select="$verbose"/>
794   </xsl:apply-templates>
795 </xsl:template>
796
797 <!-- These are elements for which no link text exists, so an xref to one
798      uses the xrefstyle attribute if specified, or if not it falls back
799      to the container element's link text -->
800 <xsl:template match="d:para|d:phrase|d:simpara|d:anchor|d:quote" mode="xref-to">
801   <xsl:param name="referrer"/>
802   <xsl:param name="xrefstyle"/>
803   <xsl:param name="verbose"/>
804
805   <xsl:variable name="context" select="(ancestor::d:simplesect
806                                        |ancestor::d:section
807                                        |ancestor::d:sect1
808                                        |ancestor::d:sect2
809                                        |ancestor::d:sect3
810                                        |ancestor::d:sect4
811                                        |ancestor::d:sect5
812                                        |ancestor::d:topic
813                                        |ancestor::d:refsection
814                                        |ancestor::d:refsect1
815                                        |ancestor::d:refsect2
816                                        |ancestor::d:refsect3
817                                        |ancestor::d:chapter
818                                        |ancestor::d:appendix
819                                        |ancestor::d:preface
820                                        |ancestor::d:partintro
821                                        |ancestor::d:dedication
822                                        |ancestor::d:acknowledgements
823                                        |ancestor::d:colophon
824                                        |ancestor::d:bibliography
825                                        |ancestor::d:index
826                                        |ancestor::d:glossary
827                                        |ancestor::d:glossentry
828                                        |ancestor::d:listitem
829                                        |ancestor::d:varlistentry)[last()]"/>
830
831   <xsl:choose>
832     <xsl:when test="$xrefstyle != ''">
833       <xsl:apply-templates select="." mode="object.xref.markup">
834         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
835         <xsl:with-param name="referrer" select="$referrer"/>
836         <xsl:with-param name="verbose" select="$verbose"/>
837       </xsl:apply-templates>
838     </xsl:when>
839     <xsl:otherwise>
840       <xsl:if test="$verbose != 0">
841         <xsl:message>
842           <xsl:text>WARNING: xref to &lt;</xsl:text>
843           <xsl:value-of select="local-name()"/>
844           <xsl:text> id="</xsl:text>
845           <xsl:value-of select="@id|@xml:id"/>
846           <xsl:text>"&gt; has no generated text. Trying its ancestor elements.</xsl:text>
847         </xsl:message>
848       </xsl:if>
849       <xsl:apply-templates select="$context" mode="xref-to">
850         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
851         <xsl:with-param name="referrer" select="$referrer"/>
852         <xsl:with-param name="verbose" select="$verbose"/>
853       </xsl:apply-templates>
854     </xsl:otherwise>
855   </xsl:choose>
856 </xsl:template>
857
858 <xsl:template match="d:indexterm" mode="xref-to">
859   <xsl:value-of select="d:primary"/>
860 </xsl:template>
861
862 <xsl:template match="d:primary|d:secondary|d:tertiary" mode="xref-to">
863   <xsl:value-of select="."/>
864 </xsl:template>
865
866 <!-- ==================================================================== -->
867
868 <xsl:template match="d:link" name="link">
869   <xsl:param name="referrer" select="."/>
870   <xsl:param name="linkend" select="@linkend"/>
871   <xsl:param name="targets" select="key('id',$linkend)"/>
872   <xsl:param name="target" select="$targets[1]"/>
873   <xsl:param name="xrefstyle">
874     <xsl:apply-templates select="." mode="xrefstyle">
875       <xsl:with-param name="target" select="$target"/>
876       <xsl:with-param name="referrer" select="$referrer"/>
877     </xsl:apply-templates>
878   </xsl:param>
879
880   <xsl:variable name="content">
881     <xsl:choose>
882       <xsl:when test="count(child::node()) &gt; 0">
883         <!-- If it has content, use it -->
884         <xsl:apply-templates/>
885       </xsl:when>
886       <!-- look for an endterm -->
887       <xsl:when test="@endterm">
888         <xsl:variable name="etargets" select="key('id',@endterm)"/>
889         <xsl:variable name="etarget" select="$etargets[1]"/>
890         <xsl:choose>
891           <xsl:when test="count($etarget) = 0">
892             <xsl:message>
893               <xsl:value-of select="count($etargets)"/>
894               <xsl:text>Endterm points to nonexistent ID: </xsl:text>
895               <xsl:value-of select="@endterm"/>
896             </xsl:message>
897             <xsl:text>???</xsl:text>
898           </xsl:when>
899           <xsl:otherwise>
900               <xsl:apply-templates select="$etarget" mode="endterm"/>
901           </xsl:otherwise>
902         </xsl:choose>
903       </xsl:when>
904       <!-- Use the xlink:href if no other text -->
905       <xsl:when test="@xlink:href">
906           <fo:inline hyphenate="false">
907             <xsl:call-template name="hyphenate-url">
908               <xsl:with-param name="url" select="@xlink:href"/>
909             </xsl:call-template>
910           </fo:inline>
911       </xsl:when>
912       <xsl:otherwise>
913         <xsl:message>
914           <xsl:text>Link element has no content and no Endterm. </xsl:text>
915           <xsl:text>Nothing to show in the link to </xsl:text>
916           <xsl:value-of select="$target"/>
917         </xsl:message>
918         <xsl:text>???</xsl:text>
919       </xsl:otherwise>
920     </xsl:choose>
921   </xsl:variable>
922
923   <xsl:variable name="styled.content">
924     <xsl:choose>
925       <xsl:when test="@xlink:role = $xolink.role">
926         <!-- olink styling handled by simple.xlink -->
927         <xsl:copy-of select="$content"/>
928       </xsl:when>
929       <xsl:otherwise>
930         <fo:inline xsl:use-attribute-sets="xref.properties">
931           <xsl:copy-of select="$content"/>
932         </fo:inline>
933       </xsl:otherwise>
934     </xsl:choose>
935   </xsl:variable>
936
937   <xsl:call-template name="simple.xlink">
938     <xsl:with-param name="node" select="."/>
939     <xsl:with-param name="linkend" select="$linkend"/>
940     <xsl:with-param name="content" select="$styled.content"/>
941   </xsl:call-template>
942
943   <!-- Add standard page reference? -->
944   <xsl:choose>
945     <!-- page numbering on link only enabled for @linkend -->
946     <xsl:when test="not($linkend)">
947     </xsl:when>
948     <!-- negative xrefstyle in instance turns it off -->
949     <xsl:when test="starts-with(normalize-space($xrefstyle), 'select:') 
950                   and contains($xrefstyle, 'nopage')">
951     </xsl:when>
952     <xsl:when test="(starts-with(normalize-space($xrefstyle), 'select:') 
953                   and $insert.link.page.number = 'maybe'  
954                   and (contains($xrefstyle, 'page')
955                        or contains($xrefstyle, 'Page')))
956                   or ( $insert.link.page.number = 'yes' 
957                      or $insert.link.page.number = '1')">
958       <xsl:apply-templates select="$target" mode="page.citation">
959         <xsl:with-param name="id" select="$linkend"/>
960         <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
961       </xsl:apply-templates>
962     </xsl:when>
963   </xsl:choose>
964 </xsl:template>
965
966 <xsl:template match="d:ulink" name="ulink">
967   <xsl:param name="url" select="@url"/>
968
969   <xsl:variable name ="ulink.url">
970     <xsl:call-template name="fo-external-image">
971       <xsl:with-param name="filename" select="$url"/>
972     </xsl:call-template>
973   </xsl:variable>
974
975   <fo:basic-link xsl:use-attribute-sets="xref.properties"
976                  external-destination="{$ulink.url}">
977     <xsl:choose>
978       <xsl:when test="count(child::node())=0 or (string(.) = $url)">
979         <fo:inline hyphenate="false">
980           <xsl:call-template name="hyphenate-url">
981             <xsl:with-param name="url" select="$url"/>
982           </xsl:call-template>
983         </fo:inline>
984       </xsl:when>
985       <xsl:otherwise>
986         <xsl:apply-templates/>
987       </xsl:otherwise>
988     </xsl:choose>
989   </fo:basic-link>
990   <!-- * Call the template for determining whether the URL for this -->
991   <!-- * hyperlink is displayed, and how to display it (either inline or -->
992   <!-- * as a numbered footnote). -->
993   <xsl:call-template name="hyperlink.url.display">
994     <xsl:with-param name="url" select="$url"/>
995     <xsl:with-param name="ulink.url" select="$ulink.url"/>
996   </xsl:call-template>
997 </xsl:template>
998
999 <xsl:template name="hyperlink.url.display">
1000   <!-- * This template is called for all external hyperlinks (ulinks and -->
1001   <!-- * for all simple xlinks); it determines whether the URL for the -->
1002   <!-- * hyperlink is displayed, and how to display it (either inline or -->
1003   <!-- * as a numbered footnote). -->
1004   <xsl:param name="url"/>
1005   <xsl:param name="ulink.url">
1006     <!-- * ulink.url is just the value of the URL wrapped in 'url(...)' -->
1007     <xsl:call-template name="fo-external-image">
1008       <xsl:with-param name="filename" select="$url"/>
1009     </xsl:call-template>
1010   </xsl:param>
1011
1012   <xsl:if test="count(child::node()) != 0
1013                 and string(.) != $url
1014                 and $ulink.show != 0">
1015     <!-- * Display the URL for this hyperlink only if it is non-empty, -->
1016     <!-- * and the value of its content is not a URL that is the same as -->
1017     <!-- * URL it links to, and if ulink.show is non-zero. -->
1018     <xsl:choose>
1019       <xsl:when test="$ulink.footnotes != 0 and not(ancestor::d:footnote) and not(ancestor::*[@floatstyle='before'])">
1020         <!-- * ulink.show and ulink.footnote are both non-zero; that -->
1021         <!-- * means we display the URL as a footnote (instead of inline) -->
1022         <fo:footnote>
1023           <xsl:call-template name="ulink.footnote.number"/>
1024           <fo:footnote-body xsl:use-attribute-sets="footnote.properties">
1025             <fo:block>
1026               <xsl:call-template name="ulink.footnote.number"/>
1027               <xsl:text> </xsl:text>
1028               <fo:basic-link external-destination="{$ulink.url}">
1029                 <xsl:value-of select="$url"/>
1030               </fo:basic-link>
1031             </fo:block>
1032           </fo:footnote-body>
1033         </fo:footnote>
1034       </xsl:when>
1035       <xsl:otherwise>
1036         <!-- * ulink.show is non-zero, but ulink.footnote is not; that -->
1037         <!-- * means we display the URL inline -->
1038         <fo:inline hyphenate="false">
1039           <!-- * put square brackets around the URL -->
1040           <xsl:text> [</xsl:text>
1041           <fo:basic-link external-destination="{$ulink.url}">
1042             <xsl:call-template name="hyphenate-url">
1043               <xsl:with-param name="url" select="$url"/>
1044             </xsl:call-template>
1045           </fo:basic-link>
1046           <xsl:text>]</xsl:text>
1047         </fo:inline>
1048       </xsl:otherwise>
1049     </xsl:choose>
1050   </xsl:if>
1051
1052 </xsl:template>
1053
1054 <xsl:template name="ulink.footnote.number">
1055   <fo:inline xsl:use-attribute-sets="footnote.mark.properties">
1056     <xsl:choose>
1057       <xsl:when test="$fop.extensions != 0">
1058         <xsl:attribute name="vertical-align">super</xsl:attribute>
1059       </xsl:when>
1060       <xsl:otherwise>
1061         <xsl:attribute name="baseline-shift">super</xsl:attribute>
1062       </xsl:otherwise>
1063     </xsl:choose>
1064     <xsl:variable name="fnum">
1065       <!-- * Determine the footnote number to display for this hyperlink, -->
1066       <!-- * by counting all foonotes, ulinks, and any elements that have -->
1067       <!-- * an xlink:href attribute that meets the following criteria: -->
1068       <!-- * -->
1069       <!-- * - the content of the element is not a URI that is the same -->
1070       <!-- *   URI as the value of the href attribute -->
1071       <!-- * - the href attribute is not an internal ID reference (does -->
1072       <!-- *   not start with a hash sign) -->
1073       <!-- * - the href is not part of an olink reference (the element -->
1074       <!-- * - does not have an xlink:role attribute that indicates it is -->
1075       <!-- *   an olink, and the href does not contain a hash sign) -->
1076       <!-- * - the element either has no xlink:type attribute or has -->
1077       <!-- *   an xlink:type attribute whose value is 'simple' -->
1078       <!-- FIXME: list in @from is probably not complete -->
1079       <xsl:number level="any" 
1080                   from="d:chapter|d:appendix|d:preface|d:article|d:refentry|d:bibliography[not(parent::d:article)]"
1081                   count="d:footnote[not(@label)][not(ancestor::d:tgroup)]
1082                   |d:ulink[node()][@url != .][not(ancestor::d:footnote)]
1083                   |*[node()][@xlink:href][not(@xlink:href = .)][not(starts-with(@xlink:href,'#'))]
1084                     [not(contains(@xlink:href,'#') and @xlink:role = $xolink.role)]
1085                     [not(@xlink:type) or @xlink:type='simple']
1086                     [not(ancestor::d:footnote)]"
1087                   format="1"/>
1088     </xsl:variable>
1089     <xsl:choose>
1090       <xsl:when test="string-length($footnote.number.symbols) &gt;= $fnum">
1091         <xsl:value-of select="substring($footnote.number.symbols, $fnum, 1)"/>
1092       </xsl:when>
1093       <xsl:otherwise>
1094         <xsl:number value="$fnum" format="{$footnote.number.format}"/>
1095       </xsl:otherwise>
1096     </xsl:choose>
1097   </fo:inline>
1098 </xsl:template>
1099
1100 <xsl:template name="hyphenate-url">
1101   <xsl:param name="url" select="''"/>
1102   <xsl:choose>
1103     <xsl:when test="$ulink.hyphenate = ''">
1104       <xsl:value-of select="$url"/>
1105     </xsl:when>
1106     <xsl:when test="string-length($url) &gt; 1">
1107       <xsl:variable name="char" select="substring($url, 1, 1)"/>
1108       <xsl:value-of select="$char"/>
1109       <xsl:if test="contains($ulink.hyphenate.chars, $char)">
1110         <!-- Do not hyphen in-between // -->
1111         <xsl:if test="not($char = '/' and substring($url,2,1) = '/')">
1112           <xsl:copy-of select="$ulink.hyphenate"/>
1113         </xsl:if>
1114       </xsl:if>
1115       <!-- recurse to the next character -->
1116       <xsl:call-template name="hyphenate-url">
1117         <xsl:with-param name="url" select="substring($url, 2)"/>
1118       </xsl:call-template>
1119     </xsl:when>
1120     <xsl:otherwise>
1121       <xsl:value-of select="$url"/>
1122     </xsl:otherwise>
1123   </xsl:choose>
1124 </xsl:template>
1125
1126 <xsl:template match="d:olink" name="olink">
1127   <!-- olink content may be passed in from xlink olink -->
1128   <xsl:param name="content" select="NOTANELEMENT"/>
1129
1130   <xsl:choose>
1131     <!-- olinks resolved by stylesheet and target database -->
1132     <xsl:when test="@targetdoc or @targetptr or
1133                     (@xlink:role=$xolink.role and
1134                      contains(@xlink:href, '#') )" >
1135
1136       <xsl:variable name="targetdoc.att">
1137         <xsl:choose>
1138           <xsl:when test="@targetdoc != ''">
1139             <xsl:value-of select="@targetdoc"/>
1140           </xsl:when>
1141           <xsl:when test="@xlink:role=$xolink.role and
1142                        contains(@xlink:href, '#')" >
1143             <xsl:value-of select="substring-before(@xlink:href, '#')"/>
1144           </xsl:when>
1145         </xsl:choose>
1146       </xsl:variable>
1147
1148       <xsl:variable name="targetptr.att">
1149         <xsl:choose>
1150           <xsl:when test="@targetptr != ''">
1151             <xsl:value-of select="@targetptr"/>
1152           </xsl:when>
1153           <xsl:when test="@xlink:role=$xolink.role and
1154                        contains(@xlink:href, '#')" >
1155             <xsl:value-of select="substring-after(@xlink:href, '#')"/>
1156           </xsl:when>
1157         </xsl:choose>
1158       </xsl:variable>
1159
1160       <xsl:variable name="olink.lang">
1161         <xsl:call-template name="l10n.language">
1162           <xsl:with-param name="xref-context" select="true()"/>
1163         </xsl:call-template>
1164       </xsl:variable>
1165     
1166       <xsl:variable name="target.database.filename">
1167         <xsl:call-template name="select.target.database">
1168           <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
1169           <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
1170           <xsl:with-param name="olink.lang" select="$olink.lang"/>
1171         </xsl:call-template>
1172       </xsl:variable>
1173     
1174       <xsl:variable name="target.database" 
1175           select="document($target.database.filename, /)"/>
1176     
1177       <xsl:if test="$olink.debug != 0">
1178         <xsl:message>
1179           <xsl:text>Olink debug: root element of target.database is '</xsl:text>
1180           <xsl:value-of select="local-name($target.database/*[1])"/>
1181           <xsl:text>'.</xsl:text>
1182         </xsl:message>
1183       </xsl:if>
1184     
1185       <xsl:variable name="olink.key">
1186         <xsl:call-template name="select.olink.key">
1187           <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
1188           <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
1189           <xsl:with-param name="olink.lang" select="$olink.lang"/>
1190           <xsl:with-param name="target.database" select="$target.database"/>
1191         </xsl:call-template>
1192       </xsl:variable>
1193     
1194       <xsl:if test="string-length($olink.key) = 0">
1195         <xsl:call-template name="olink.unresolved">
1196           <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
1197           <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
1198         </xsl:call-template>
1199       </xsl:if>
1200
1201       <xsl:variable name="href">
1202         <xsl:call-template name="make.olink.href">
1203           <xsl:with-param name="olink.key" select="$olink.key"/>
1204           <xsl:with-param name="target.database" select="$target.database"/>
1205         </xsl:call-template>
1206       </xsl:variable>
1207
1208       <!-- Olink that points to internal id can be a link -->
1209       <xsl:variable name="linkend">
1210         <xsl:call-template name="olink.as.linkend">
1211           <xsl:with-param name="olink.key" select="$olink.key"/>
1212           <xsl:with-param name="olink.lang" select="$olink.lang"/>
1213           <xsl:with-param name="target.database" select="$target.database"/>
1214         </xsl:call-template>
1215       </xsl:variable>
1216
1217       <xsl:variable name="hottext">
1218         <xsl:choose>
1219           <xsl:when test="string-length($content) != 0">
1220             <xsl:copy-of select="$content"/>
1221           </xsl:when>
1222           <xsl:otherwise>
1223             <xsl:call-template name="olink.hottext">
1224               <xsl:with-param name="olink.key" select="$olink.key"/>
1225               <xsl:with-param name="olink.lang" select="$olink.lang"/>
1226               <xsl:with-param name="target.database" select="$target.database"/>
1227             </xsl:call-template>
1228           </xsl:otherwise>
1229         </xsl:choose>
1230       </xsl:variable>
1231
1232       <xsl:variable name="olink.docname.citation">
1233         <xsl:call-template name="olink.document.citation">
1234           <xsl:with-param name="olink.key" select="$olink.key"/>
1235           <xsl:with-param name="target.database" select="$target.database"/>
1236           <xsl:with-param name="olink.lang" select="$olink.lang"/>
1237         </xsl:call-template>
1238       </xsl:variable>
1239
1240       <xsl:variable name="olink.page.citation">
1241         <xsl:call-template name="olink.page.citation">
1242           <xsl:with-param name="olink.key" select="$olink.key"/>
1243           <xsl:with-param name="target.database" select="$target.database"/>
1244           <xsl:with-param name="olink.lang" select="$olink.lang"/>
1245           <xsl:with-param name="linkend" select="$linkend"/>
1246         </xsl:call-template>
1247       </xsl:variable>
1248
1249       <xsl:choose>
1250         <xsl:when test="$linkend != ''">
1251           <fo:basic-link internal-destination="{$linkend}"
1252                        xsl:use-attribute-sets="xref.properties">
1253             <xsl:call-template name="anchor"/>
1254             <xsl:copy-of select="$hottext"/>
1255             <xsl:copy-of select="$olink.page.citation"/>
1256           </fo:basic-link>
1257         </xsl:when>
1258         <xsl:when test="$href != ''">
1259           <xsl:choose>
1260             <xsl:when test="$fop1.extensions != 0">
1261               <xsl:variable name="href.mangled">
1262                 <xsl:choose>
1263                   <xsl:when test="contains($href, '#')">
1264                     <xsl:value-of select="concat(substring-before($href,'#'), '#dest=', substring-after($href,'#'))"/>
1265                   </xsl:when>
1266                   <xsl:otherwise>
1267                     <xsl:value-of select="$href"/>
1268                   </xsl:otherwise>
1269                 </xsl:choose>
1270               </xsl:variable>
1271               <fo:basic-link external-destination="{$href.mangled}"
1272                              xsl:use-attribute-sets="olink.properties">
1273                 <xsl:copy-of select="$hottext"/>
1274               </fo:basic-link>
1275               <xsl:copy-of select="$olink.page.citation"/>
1276               <xsl:copy-of select="$olink.docname.citation"/>
1277             </xsl:when>
1278             <xsl:when test="$xep.extensions != 0">
1279               <fo:basic-link external-destination="url({$href})"
1280                              xsl:use-attribute-sets="olink.properties">
1281                 <xsl:call-template name="anchor"/>
1282                 <xsl:copy-of select="$hottext"/>
1283               </fo:basic-link>
1284               <xsl:copy-of select="$olink.page.citation"/>
1285               <xsl:copy-of select="$olink.docname.citation"/>
1286             </xsl:when>
1287             <xsl:when test="$axf.extensions != 0">
1288               <fo:basic-link external-destination="{$href}"
1289                              xsl:use-attribute-sets="olink.properties">
1290                 <xsl:copy-of select="$hottext"/>
1291               </fo:basic-link>
1292               <xsl:copy-of select="$olink.page.citation"/>
1293               <xsl:copy-of select="$olink.docname.citation"/>
1294             </xsl:when>
1295             <xsl:otherwise>
1296               <fo:basic-link external-destination="{$href}"
1297                              xsl:use-attribute-sets="olink.properties">
1298                 <xsl:copy-of select="$hottext"/>
1299               </fo:basic-link>
1300               <xsl:copy-of select="$olink.page.citation"/>
1301               <xsl:copy-of select="$olink.docname.citation"/>
1302             </xsl:otherwise>
1303           </xsl:choose>
1304         </xsl:when>
1305         <xsl:otherwise>
1306           <xsl:copy-of select="$hottext"/>
1307           <xsl:copy-of select="$olink.page.citation"/>
1308           <xsl:copy-of select="$olink.docname.citation"/>
1309         </xsl:otherwise>
1310       </xsl:choose>
1311     </xsl:when>
1312
1313     <!-- olink never implemented in FO for old olink entity syntax -->
1314     <xsl:otherwise>
1315       <xsl:apply-templates/>
1316     </xsl:otherwise>
1317   </xsl:choose>
1318 </xsl:template>
1319
1320 <xsl:template match="*" mode="insert.olink.docname.markup">
1321   <xsl:param name="docname" select="''"/>
1322   
1323   <fo:inline font-style="italic">
1324     <xsl:value-of select="$docname"/>
1325   </fo:inline>
1326
1327 </xsl:template>
1328
1329 <!-- This prevents error message when processing olinks with xrefstyle -->
1330 <xsl:template match="d:olink" mode="object.xref.template"/>
1331
1332
1333 <xsl:template name="olink.as.linkend">
1334   <xsl:param name="olink.key" select="''"/>
1335   <xsl:param name="olink.lang" select="''"/>
1336   <xsl:param name="target.database" select="NotANode"/>
1337
1338   <xsl:variable name="targetdoc">
1339     <xsl:value-of select="substring-before($olink.key, '/')"/>
1340   </xsl:variable>
1341
1342   <xsl:variable name="targetptr">
1343     <xsl:value-of 
1344        select="substring-before(substring-after($olink.key, '/'), '/')"/>
1345   </xsl:variable>
1346
1347   <xsl:variable name="target.lang">
1348     <xsl:variable name="candidate">
1349       <xsl:for-each select="$target.database" >
1350         <xsl:value-of 
1351                   select="key('targetptr-key', $olink.key)[1]/@lang" />
1352       </xsl:for-each>
1353     </xsl:variable>
1354     <xsl:choose>
1355       <xsl:when test="$candidate != ''">
1356         <xsl:value-of select="$candidate"/>
1357       </xsl:when>
1358       <xsl:otherwise>
1359         <xsl:value-of select="$olink.lang"/>
1360       </xsl:otherwise>
1361     </xsl:choose>
1362   </xsl:variable>
1363
1364   <xsl:if test="$current.docid = $targetdoc and 
1365                 $olink.lang = $target.lang">
1366     <xsl:variable name="targets" select="key('id',$targetptr)"/>
1367     <xsl:variable name="target" select="$targets[1]"/>
1368     <xsl:if test="$target">
1369       <xsl:value-of select="$targetptr"/>
1370     </xsl:if>
1371   </xsl:if>
1372
1373 </xsl:template>
1374
1375
1376 <!-- ==================================================================== -->
1377
1378 <xsl:template name="title.xref">
1379   <xsl:param name="target" select="."/>
1380   <xsl:choose>
1381     <xsl:when test="local-name($target) = 'figure'
1382                     or local-name($target) = 'example'
1383                     or local-name($target) = 'equation'
1384                     or local-name($target) = 'table'
1385                     or local-name($target) = 'dedication'
1386                     or local-name($target) = 'acknowledgements'
1387                     or local-name($target) = 'preface'
1388                     or local-name($target) = 'bibliography'
1389                     or local-name($target) = 'glossary'
1390                     or local-name($target) = 'index'
1391                     or local-name($target) = 'setindex'
1392                     or local-name($target) = 'colophon'">
1393       <xsl:call-template name="gentext.startquote"/>
1394       <xsl:apply-templates select="$target" mode="title.markup"/>
1395       <xsl:call-template name="gentext.endquote"/>
1396     </xsl:when>
1397     <xsl:otherwise>
1398       <fo:inline font-style="italic">
1399         <xsl:apply-templates select="$target" mode="title.markup"/>
1400       </fo:inline>
1401     </xsl:otherwise>
1402   </xsl:choose>
1403 </xsl:template>
1404
1405 <xsl:template name="number.xref">
1406   <xsl:param name="target" select="."/>
1407   <xsl:apply-templates select="$target" mode="label.markup"/>
1408 </xsl:template>
1409
1410 <!-- ==================================================================== -->
1411
1412 <xsl:template name="xref.xreflabel">
1413   <!-- called to process an xreflabel...you might use this to make  -->
1414   <!-- xreflabels come out in the right font for different targets, -->
1415   <!-- for example. -->
1416   <xsl:param name="target" select="."/>
1417   <xsl:value-of select="$target/@xreflabel"/>
1418 </xsl:template>
1419
1420 <!-- ==================================================================== -->
1421
1422 <xsl:template match="d:title" mode="xref">
1423   <xsl:apply-templates/>
1424 </xsl:template>
1425
1426 <xsl:template match="d:command" mode="xref">
1427   <xsl:call-template name="inline.boldseq"/>
1428 </xsl:template>
1429
1430 <xsl:template match="d:function" mode="xref">
1431   <xsl:call-template name="inline.monoseq"/>
1432 </xsl:template>
1433
1434 <xsl:template match="*" mode="page.citation">
1435   <xsl:param name="id" select="'???'"/>
1436   <xsl:param name="xrefstyle" select="''"/>
1437
1438   <fo:basic-link internal-destination="{$id}"
1439                  xsl:use-attribute-sets="xref.properties">
1440     <fo:inline keep-together.within-line="always">
1441       <xsl:call-template name="substitute-markup">
1442         <xsl:with-param name="template">
1443           <xsl:call-template name="gentext.template">
1444             <xsl:with-param name="name" select="'page.citation'"/>
1445             <xsl:with-param name="context" select="'xref'"/>
1446             <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
1447           </xsl:call-template>
1448         </xsl:with-param>
1449       </xsl:call-template>
1450     </fo:inline>
1451   </fo:basic-link>
1452 </xsl:template>
1453
1454 <xsl:template match="*" mode="pagenumber.markup">
1455   <xsl:variable name="id">
1456     <xsl:call-template name="object.id"/>
1457   </xsl:variable>
1458   <fo:page-number-citation ref-id="{$id}"/>
1459 </xsl:template>
1460
1461 <!-- ==================================================================== -->
1462
1463 <xsl:template match="*" mode="insert.title.markup">
1464   <xsl:param name="purpose"/>
1465   <xsl:param name="xrefstyle"/>
1466   <xsl:param name="title"/>
1467
1468   <xsl:choose>
1469     <xsl:when test="$purpose = 'xref'">
1470       <xsl:copy-of select="$title"/>
1471     </xsl:when>
1472     <xsl:otherwise>
1473       <xsl:copy-of select="$title"/>
1474     </xsl:otherwise>
1475   </xsl:choose>
1476 </xsl:template>
1477
1478 <xsl:template match="d:chapter|d:appendix" mode="insert.title.markup">
1479   <xsl:param name="purpose"/>
1480   <xsl:param name="xrefstyle"/>
1481   <xsl:param name="title"/>
1482
1483   <xsl:choose>
1484     <xsl:when test="$purpose = 'xref'">
1485       <fo:inline font-style="italic">
1486         <xsl:copy-of select="$title"/>
1487       </fo:inline>
1488     </xsl:when>
1489     <xsl:otherwise>
1490       <xsl:copy-of select="$title"/>
1491     </xsl:otherwise>
1492   </xsl:choose>
1493 </xsl:template>
1494
1495 <xsl:template match="*" mode="insert.subtitle.markup">
1496   <xsl:param name="purpose"/>
1497   <xsl:param name="xrefstyle"/>
1498   <xsl:param name="subtitle"/>
1499
1500   <xsl:copy-of select="$subtitle"/>
1501 </xsl:template>
1502
1503 <xsl:template match="*" mode="insert.label.markup">
1504   <xsl:param name="purpose"/>
1505   <xsl:param name="xrefstyle"/>
1506   <xsl:param name="label"/>
1507
1508   <xsl:copy-of select="$label"/>
1509 </xsl:template>
1510
1511 <xsl:template match="*" mode="insert.pagenumber.markup">
1512   <xsl:param name="purpose"/>
1513   <xsl:param name="xrefstyle"/>
1514   <xsl:param name="pagenumber"/>
1515
1516   <xsl:copy-of select="$pagenumber"/>
1517 </xsl:template>
1518
1519 <xsl:template match="*" mode="insert.direction.markup">
1520   <xsl:param name="purpose"/>
1521   <xsl:param name="xrefstyle"/>
1522   <xsl:param name="direction"/>
1523
1524   <xsl:copy-of select="$direction"/>
1525 </xsl:template>
1526
1527 <xsl:template match="d:olink" mode="pagenumber.markup">
1528   <!-- Local olinks can use page-citation -->
1529   <xsl:variable name="targetdoc.att" select="@targetdoc"/>
1530   <xsl:variable name="targetptr.att" select="@targetptr"/>
1531
1532   <xsl:variable name="olink.lang">
1533     <xsl:call-template name="l10n.language">
1534       <xsl:with-param name="xref-context" select="true()"/>
1535     </xsl:call-template>
1536   </xsl:variable>
1537
1538   <xsl:variable name="target.database.filename">
1539     <xsl:call-template name="select.target.database">
1540       <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
1541       <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
1542       <xsl:with-param name="olink.lang" select="$olink.lang"/>
1543     </xsl:call-template>
1544   </xsl:variable>
1545
1546   <xsl:variable name="target.database" 
1547       select="document($target.database.filename, /)"/>
1548
1549   <xsl:if test="$olink.debug != 0">
1550     <xsl:message>
1551       <xsl:text>Olink debug: root element of target.database is '</xsl:text>
1552       <xsl:value-of select="local-name($target.database/*[1])"/>
1553       <xsl:text>'.</xsl:text>
1554     </xsl:message>
1555   </xsl:if>
1556
1557   <xsl:variable name="olink.key">
1558     <xsl:call-template name="select.olink.key">
1559       <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
1560       <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
1561       <xsl:with-param name="olink.lang" select="$olink.lang"/>
1562       <xsl:with-param name="target.database" select="$target.database"/>
1563     </xsl:call-template>
1564   </xsl:variable>
1565
1566   <!-- Olink that points to internal id can be a link -->
1567   <xsl:variable name="linkend">
1568     <xsl:call-template name="olink.as.linkend">
1569       <xsl:with-param name="olink.key" select="$olink.key"/>
1570       <xsl:with-param name="olink.lang" select="$olink.lang"/>
1571       <xsl:with-param name="target.database" select="$target.database"/>
1572     </xsl:call-template>
1573   </xsl:variable>
1574
1575   <xsl:choose>
1576     <xsl:when test="$linkend != ''">
1577       <fo:page-number-citation ref-id="{$linkend}"/>
1578     </xsl:when>
1579     <xsl:otherwise>
1580       <xsl:call-template name="olink.error">
1581         <xsl:with-param name="message">
1582           <xsl:text>no page number linkend for local olink '</xsl:text>
1583           <xsl:value-of select="$olink.key"/>
1584           <xsl:text>'</xsl:text>
1585         </xsl:with-param>
1586       </xsl:call-template>
1587     </xsl:otherwise>
1588   </xsl:choose>
1589 </xsl:template>
1590
1591 </xsl:stylesheet>