Project

General

Profile

1
<?xml version="1.0"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
                version="1.0">
4

    
5
<!-- Create keys for quickly looking up olink targets -->
6
<xsl:key name="targetdoc-key" match="document" use="@targetdoc" />
7
<xsl:key name="targetptr-key"  match="div|obj"
8
         use="concat(ancestor::document/@targetdoc, '/',
9
                     @targetptr, '/', ancestor::document/@lang)" />
10

    
11
<!-- Return filename of database -->
12
<xsl:template name="select.target.database">
13
  <xsl:param name="targetdoc.att" select="''"/>
14
  <xsl:param name="targetptr.att" select="''"/>
15
  <xsl:param name="olink.lang" select="''"/>
16

    
17
  <!-- use root's xml:base if exists -->
18
  <xsl:variable name="xml.base" select="/*/@xml:base"/>
19

    
20
  <!-- This selection can be customized if needed -->
21
  <xsl:variable name="target.database.filename">
22
    <xsl:choose>
23
      <xsl:when test="$xml.base != '' and
24
                   not(starts-with($target.database.document, 'file:/')) and
25
                   not(starts-with($target.database.document, '/'))">
26
        <xsl:call-template name="systemIdToBaseURI">
27
          <xsl:with-param name="systemId" select="$xml.base"/>
28
        </xsl:call-template>
29
        <xsl:value-of select="$target.database.document"/>
30
      </xsl:when>
31
      <xsl:otherwise>
32
        <xsl:value-of select="$target.database.document"/>
33
      </xsl:otherwise>
34
    </xsl:choose>
35
  </xsl:variable>
36

    
37
  <xsl:variable name="target.database" 
38
      select="document($target.database.filename,/)"/>
39

    
40
  <xsl:choose>
41
    <!-- Was the database document parameter not set? -->
42
    <xsl:when test="$target.database.document = ''">
43
      <xsl:message>
44
        <xsl:text>Olinks not processed: must specify a </xsl:text>
45
        <xsl:text>$target.database.document parameter&#10;</xsl:text>
46
        <xsl:text>when using olinks with targetdoc </xsl:text>
47
        <xsl:text>and targetptr attributes.</xsl:text>
48
      </xsl:message>
49
    </xsl:when>
50
    <!-- Did it not open? Should be a targetset element -->
51
    <xsl:when test="not($target.database/*)">
52
      <xsl:message>
53
        <xsl:text>Olink error: could not open target database '</xsl:text>
54
        <xsl:value-of select="$target.database.filename"/>
55
        <xsl:text>'.</xsl:text>
56
      </xsl:message>
57
    </xsl:when>
58
    <xsl:otherwise>
59
      <xsl:value-of select="$target.database.filename"/>
60
    </xsl:otherwise>
61
  </xsl:choose>
62
</xsl:template>
63

    
64
<xsl:template name="select.olink.key">
65
  <xsl:param name="targetdoc.att" select="''"/>
66
  <xsl:param name="targetptr.att" select="''"/>
67
  <xsl:param name="olink.lang" select="''"/>
68
  <xsl:param name="target.database"/>
69

    
70
  <xsl:if test="$target.database/*">
71
    <xsl:variable name="olink.fallback.sequence">
72
      <xsl:call-template name="select.olink.lang.fallback">
73
        <xsl:with-param name="olink.lang" select="$olink.lang"/>
74
      </xsl:call-template>
75
    </xsl:variable>
76
  
77
    <!-- Recurse through the languages until you find a match -->
78
    <xsl:call-template name="select.olink.key.in.lang">
79
      <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
80
      <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
81
      <xsl:with-param name="olink.lang" select="$olink.lang"/>
82
      <xsl:with-param name="target.database" select="$target.database"/>
83
      <xsl:with-param name="fallback.index" select="1"/>
84
      <xsl:with-param name="olink.fallback.sequence"
85
                      select="$olink.fallback.sequence"/>
86
    </xsl:call-template>
87
  </xsl:if>
88
  
89
</xsl:template>
90

    
91
<!-- Locate olink key in a particular language -->
92
<xsl:template name="select.olink.key.in.lang">
93
  <xsl:param name="targetdoc.att" select="''"/>
94
  <xsl:param name="targetptr.att" select="''"/>
95
  <xsl:param name="olink.lang" select="''"/>
96
  <xsl:param name="target.database"/>
97
  <xsl:param name="fallback.index" select="1"/>
98
  <xsl:param name="olink.fallback.sequence" select="''"/>
99
  
100
  <xsl:variable name="target.lang">
101
    <xsl:call-template name="select.target.lang">
102
      <xsl:with-param name="fallback.index" select="$fallback.index"/>
103
      <xsl:with-param name="olink.fallback.sequence"
104
                      select="$olink.fallback.sequence"/>
105
    </xsl:call-template>
106
  </xsl:variable>
107

    
108
  <xsl:if test="$olink.debug != 0">
109
    <xsl:message><xsl:text>Olink debug: cases for targetdoc='</xsl:text>
110
      <xsl:value-of select="$targetdoc.att"/>
111
      <xsl:text>' and targetptr='</xsl:text>
112
      <xsl:value-of select="$targetptr.att"/>
113
      <xsl:text>' in language '</xsl:text>
114
      <xsl:value-of select="$target.lang"/>
115
      <xsl:text>'.</xsl:text>
116
    </xsl:message>
117
  </xsl:if>
118

    
119
  <!-- Customize these cases if you want different selection logic -->
120
  <xsl:variable name="CaseA">
121
    <!-- targetdoc.att = not blank
122
         targetptr.att = not blank
123
    -->
124
    <xsl:if test="$targetdoc.att != '' and
125
                  $targetptr.att != ''">
126
      <xsl:for-each select="$target.database">
127
        <xsl:variable name="key" 
128
                      select="concat($targetdoc.att, '/', 
129
                                     $targetptr.att, '/',
130
                                     $target.lang)"/>
131
        <xsl:choose>
132
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
133
            <xsl:value-of select="$key"/>
134
            <xsl:if test="$olink.debug != 0">
135
              <xsl:message>Olink debug: CaseA matched.</xsl:message>
136
            </xsl:if>
137
          </xsl:when>
138
          <xsl:when test="$olink.debug != 0">
139
            <xsl:message>Olink debug: CaseA NOT matched</xsl:message>
140
          </xsl:when>
141
        </xsl:choose>
142
      </xsl:for-each>
143
    </xsl:if>
144
  </xsl:variable>
145

    
146
  <xsl:variable name="CaseB">
147
    <!-- targetdoc.att = not blank
148
         targetptr.att = not blank
149
         prefer.internal.olink = not zero
150
         current.docid = not blank 
151
    -->
152
    <xsl:if test="$targetdoc.att != '' and
153
                  $targetptr.att != '' and
154
                  $current.docid != '' and
155
                  $prefer.internal.olink != 0">
156
      <xsl:for-each select="$target.database">
157
        <xsl:variable name="key" 
158
                      select="concat($current.docid, '/', 
159
                                     $targetptr.att, '/',
160
                                     $target.lang)"/>
161
        <xsl:choose>
162
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
163
            <xsl:value-of select="$key"/>
164
            <xsl:if test="$olink.debug != 0">
165
              <xsl:message>Olink debug: CaseB matched.</xsl:message>
166
            </xsl:if>
167
          </xsl:when>
168
          <xsl:when test="$olink.debug != 0">
169
            <xsl:message>Olink debug: CaseB NOT matched</xsl:message>
170
          </xsl:when>
171
        </xsl:choose>
172
      </xsl:for-each>
173
    </xsl:if>
174
  </xsl:variable>
175

    
176
  <xsl:variable name="CaseC">
177
    <!-- targetdoc.att = blank
178
         targetptr.att = not blank
179
         current.docid = not blank 
180
    -->
181
    <xsl:if test="string-length($targetdoc.att) = 0 and
182
                  $targetptr.att != '' and
183
                  $current.docid != ''">
184
      <!-- Must use a for-each to change context for keys to work -->
185
      <xsl:for-each select="$target.database">
186
        <xsl:variable name="key" 
187
                      select="concat($current.docid, '/', 
188
                                     $targetptr.att, '/',
189
                                     $target.lang)"/>
190
        <xsl:choose>
191
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
192
            <xsl:value-of select="$key"/>
193
            <xsl:if test="$olink.debug != 0">
194
              <xsl:message>Olink debug: CaseC matched.</xsl:message>
195
            </xsl:if>
196
          </xsl:when>
197
          <xsl:when test="$olink.debug != 0">
198
            <xsl:message>Olink debug: CaseC NOT matched.</xsl:message>
199
          </xsl:when>
200
        </xsl:choose>
201
      </xsl:for-each>
202
    </xsl:if>
203
  </xsl:variable>
204

    
205
  <xsl:variable name="CaseD">
206
    <!-- targetdoc.att = blank
207
         targetptr.att = not blank
208
         current.docid = blank 
209
    -->
210
    <!-- This is possible if only one document in the database -->
211
    <xsl:if test="string-length($targetdoc.att) = 0 and
212
                  $targetptr.att != '' and
213
                  string-length($current.docid) = 0 and
214
                  count($target.database//document) = 1">
215
      <xsl:for-each select="$target.database">
216
        <xsl:variable name="key" 
217
                      select="concat(.//document/@targetdoc, '/', 
218
                                     $targetptr.att, '/',
219
                                     $target.lang)"/>
220
        <xsl:choose>
221
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
222
            <xsl:value-of select="$key"/>
223
            <xsl:if test="$olink.debug != 0">
224
              <xsl:message>Olink debug: CaseD matched.</xsl:message>
225
            </xsl:if>
226
          </xsl:when>
227
          <xsl:when test="$olink.debug != 0">
228
            <xsl:message>Olink debug: CaseD NOT matched</xsl:message>
229
          </xsl:when>
230
        </xsl:choose>
231
      </xsl:for-each>
232
    </xsl:if>
233
  </xsl:variable>
234

    
235
  <xsl:variable name="CaseE">
236
    <!-- targetdoc.att = not blank
237
         targetptr.att = blank
238
    -->
239
    <xsl:if test="$targetdoc.att != '' and
240
                  string-length($targetptr.att) = 0">
241

    
242
      <!-- Try the document's root element id -->
243
      <xsl:variable name="rootid">
244
        <xsl:choose>
245
          <xsl:when test="$target.lang != ''">
246
            <xsl:value-of select="$target.database//document[@targetdoc = $targetdoc.att and @lang = $target.lang]/*[1]/@targetptr"/>
247
          </xsl:when>
248
          <xsl:otherwise>
249
            <xsl:value-of select="$target.database//document[@targetdoc = $targetdoc.att and not(@lang)]/*[1]/@targetptr"/>
250
          </xsl:otherwise>
251
        </xsl:choose>
252
      </xsl:variable>
253

    
254
      <xsl:for-each select="$target.database">
255
        <xsl:variable name="key" 
256
                      select="concat($targetdoc.att, '/', 
257
                                     $rootid, '/',
258
                                     $target.lang)"/>
259
        <xsl:choose>
260
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
261
            <xsl:value-of select="$key"/>
262
            <xsl:if test="$olink.debug != 0">
263
              <xsl:message>Olink debug: CaseE matched.</xsl:message>
264
            </xsl:if>
265
          </xsl:when>
266
          <xsl:when test="$olink.debug != 0">
267
            <xsl:message>Olink debug: CaseE NOT matched.</xsl:message>
268
          </xsl:when>
269
        </xsl:choose>
270
      </xsl:for-each>
271
    </xsl:if>
272
  </xsl:variable>
273

    
274
  <xsl:variable name="CaseF">
275
    <!-- targetdoc.att = not blank
276
         targetptr.att = blank
277
         prefer.internal.olink = not zero
278
         current.docid = not blank 
279
    -->
280
    <xsl:if test="$targetdoc.att != '' and
281
                  string-length($targetptr.att) = 0 and
282
                  $current.docid != '' and
283
                  $prefer.internal.olink != 0">
284
      <!-- Try the document's root element id -->
285
      <xsl:variable name="rootid">
286
        <xsl:choose>
287
          <xsl:when test="$target.lang != ''">
288
            <xsl:value-of select="$target.database//document[@targetdoc = $current.docid and @lang = $target.lang]/*[1]/@targetptr"/>
289
          </xsl:when>
290
          <xsl:otherwise>
291
            <xsl:value-of select="$target.database//document[@targetdoc = $current.docid and not(@lang)]/*[1]/@targetptr"/>
292
          </xsl:otherwise>
293
        </xsl:choose>
294
      </xsl:variable>
295

    
296
      <xsl:for-each select="$target.database">
297
        <xsl:variable name="key" 
298
                      select="concat($current.docid, '/', 
299
                                     $rootid, '/',
300
                                     $target.lang)"/>
301
        <xsl:choose>
302
          <xsl:when test="key('targetptr-key', $key)[1]/@href != ''">
303
            <xsl:value-of select="$key"/>
304
            <xsl:if test="$olink.debug != 0">
305
              <xsl:message>Olink debug: CaseF matched.</xsl:message>
306
            </xsl:if>
307
          </xsl:when>
308
          <xsl:when test="$olink.debug != 0">
309
            <xsl:message>Olink debug: CaseF NOT matched.</xsl:message>
310
          </xsl:when>
311
        </xsl:choose>
312
      </xsl:for-each>
313
    </xsl:if>
314
  </xsl:variable>
315

    
316
  <!-- Now select the best match. Customize the order if needed -->
317
  <xsl:variable name="selected.key">
318
    <xsl:choose>
319
      <xsl:when test="$CaseB != ''">
320
        <xsl:value-of select="$CaseB"/>
321
        <xsl:if test="$olink.debug != 0">
322
          <xsl:message>
323
            <xsl:text>Olink debug: CaseB key is the final selection: </xsl:text>
324
            <xsl:value-of select="$CaseB"/>
325
          </xsl:message>
326
        </xsl:if>
327
      </xsl:when>
328
      <xsl:when test="$CaseA != ''">
329
        <xsl:value-of select="$CaseA"/>
330
        <xsl:if test="$olink.debug != 0">
331
          <xsl:message>
332
            <xsl:text>Olink debug: CaseA key is the final selection: </xsl:text>
333
            <xsl:value-of select="$CaseA"/>
334
          </xsl:message>
335
        </xsl:if>
336
      </xsl:when>
337
      <xsl:when test="$CaseC != ''">
338
        <xsl:value-of select="$CaseC"/>
339
        <xsl:if test="$olink.debug != 0">
340
          <xsl:message>
341
            <xsl:text>Olink debug: CaseC key is the final selection: </xsl:text>
342
            <xsl:value-of select="$CaseC"/>
343
          </xsl:message>
344
        </xsl:if>
345
      </xsl:when>
346
      <xsl:when test="$CaseD != ''">
347
        <xsl:value-of select="$CaseD"/>
348
        <xsl:if test="$olink.debug != 0">
349
          <xsl:message>
350
            <xsl:text>Olink debug: CaseD key is the final selection: </xsl:text>
351
            <xsl:value-of select="$CaseD"/>
352
          </xsl:message>
353
        </xsl:if>
354
      </xsl:when>
355
      <xsl:when test="$CaseF != ''">
356
        <xsl:value-of select="$CaseF"/>
357
        <xsl:if test="$olink.debug != 0">
358
          <xsl:message>
359
            <xsl:text>Olink debug: CaseF key is the final selection: </xsl:text>
360
            <xsl:value-of select="$CaseF"/>
361
          </xsl:message>
362
        </xsl:if>
363
      </xsl:when>
364
      <xsl:when test="$CaseE != ''">
365
        <xsl:value-of select="$CaseE"/>
366
        <xsl:if test="$olink.debug != 0">
367
          <xsl:message>
368
            <xsl:text>Olink debug: CaseE key is the final selection: </xsl:text>
369
            <xsl:value-of select="$CaseE"/>
370
          </xsl:message>
371
        </xsl:if>
372
      </xsl:when>
373
      <xsl:otherwise>
374
        <xsl:if test="$olink.debug != 0">
375
          <xsl:message>
376
            <xsl:text>Olink debug: No case matched for lang '</xsl:text>
377
            <xsl:value-of select="$target.lang"/>
378
            <xsl:text>'.</xsl:text>
379
          </xsl:message>
380
        </xsl:if>
381
      </xsl:otherwise>
382
    </xsl:choose>
383
  </xsl:variable>
384

    
385
  <xsl:choose>
386
    <xsl:when test="$selected.key != ''">
387
      <xsl:value-of select="$selected.key"/>
388
    </xsl:when>
389
    <xsl:when test="string-length($selected.key) = 0 and 
390
                    string-length($target.lang) = 0">
391
      <!-- No match on last try, and we are done -->
392
    </xsl:when>
393
    <xsl:otherwise>
394
      <!-- Recurse through next language -->
395
      <xsl:call-template name="select.olink.key.in.lang">
396
        <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
397
        <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
398
        <xsl:with-param name="olink.lang" select="$olink.lang"/>
399
        <xsl:with-param name="target.database" select="$target.database"/>
400
        <xsl:with-param name="fallback.index" select="$fallback.index + 1"/>
401
        <xsl:with-param name="olink.fallback.sequence"
402
                        select="$olink.fallback.sequence"/>
403
      </xsl:call-template>
404
    </xsl:otherwise>
405
  </xsl:choose>
406

    
407
</xsl:template>
408

    
409
<xsl:template name="select.target.lang">
410
  <xsl:param name="fallback.index" select="1"/>
411
  <xsl:param name="olink.fallback.sequence" select="''"/>
412

    
413
  <!-- recurse backwards to find the lang matching the index -->
414
  <xsl:variable name="firstlang" 
415
                select="substring-before($olink.fallback.sequence, ' ')"/>
416
  <xsl:variable name="rest" 
417
                select="substring-after($olink.fallback.sequence, ' ')"/>
418
  <xsl:choose>
419
    <xsl:when test="$fallback.index = 1">
420
      <xsl:value-of select="$firstlang"/>
421
    </xsl:when>
422
    <xsl:when test="$fallback.index &gt; 1">
423
      <xsl:call-template name="select.target.lang">
424
        <xsl:with-param name="fallback.index" select="$fallback.index - 1"/>
425
        <xsl:with-param name="olink.fallback.sequence"
426
                        select="$rest"/>
427
      </xsl:call-template>
428
    </xsl:when>
429
    <xsl:otherwise>
430
    </xsl:otherwise>
431
  </xsl:choose>
432
</xsl:template>
433

    
434
<xsl:template name="select.olink.lang.fallback">
435
  <xsl:param name="olink.lang" select="''"/>
436

    
437
  <!-- Prefer language of the olink element -->
438
  <xsl:value-of select="concat(normalize-space(concat($olink.lang, ' ', 
439
                        $olink.lang.fallback.sequence)), ' ')"/>
440
</xsl:template>
441

    
442
<!-- Returns the complete olink href value if found -->
443
<xsl:template name="make.olink.href">
444
  <xsl:param name="olink.key" select="''"/>
445
  <xsl:param name="target.database"/>
446

    
447
  <xsl:if test="$olink.key != ''">
448
    <xsl:variable name="target.href" >
449
      <xsl:for-each select="$target.database" >
450
        <xsl:value-of select="key('targetptr-key', $olink.key)[1]/@href" />
451
      </xsl:for-each>
452
    </xsl:variable>
453
  
454
    <xsl:variable name="targetdoc">
455
      <xsl:value-of select="substring-before($olink.key, '/')"/>
456
    </xsl:variable>
457
  
458
    <!-- Does the target database use a sitemap? -->
459
    <xsl:variable name="use.sitemap">
460
      <xsl:choose>
461
        <xsl:when test="$target.database//sitemap">1</xsl:when>
462
        <xsl:otherwise>0</xsl:otherwise>
463
      </xsl:choose>
464
    </xsl:variable>
465
  
466
  
467
    <!-- Get the baseuri for this targetptr -->
468
    <xsl:variable name="baseuri" >
469
      <xsl:choose>
470
        <!-- Does the database use a sitemap? -->
471
        <xsl:when test="$use.sitemap != 0" >
472
          <xsl:choose>
473
            <!-- Was current.docid parameter set? -->
474
            <xsl:when test="$current.docid != ''">
475
              <!-- Was it found in the database? -->
476
              <xsl:variable name="currentdoc.key" >
477
                <xsl:for-each select="$target.database" >
478
                  <xsl:value-of select="key('targetdoc-key',
479
                                        $current.docid)[1]/@targetdoc" />
480
                </xsl:for-each>
481
              </xsl:variable>
482
              <xsl:choose>
483
                <xsl:when test="$currentdoc.key != ''">
484
                  <xsl:for-each select="$target.database" >
485
                    <xsl:call-template name="targetpath" >
486
                      <xsl:with-param name="dirnode" 
487
                          select="key('targetdoc-key', $current.docid)[1]/parent::dir"/>
488
                      <xsl:with-param name="targetdoc" select="$targetdoc"/>
489
                    </xsl:call-template>
490
                  </xsl:for-each >
491
                </xsl:when>
492
                <xsl:otherwise>
493
                  <xsl:message>
494
                    <xsl:text>Olink error: cannot compute relative </xsl:text>
495
                    <xsl:text>sitemap path because $current.docid '</xsl:text>
496
                    <xsl:value-of select="$current.docid"/>
497
                    <xsl:text>' not found in target database.</xsl:text>
498
                  </xsl:message>
499
                </xsl:otherwise>
500
              </xsl:choose>
501
            </xsl:when>
502
            <xsl:otherwise>
503
              <xsl:message>
504
                <xsl:text>Olink warning: cannot compute relative </xsl:text>
505
                <xsl:text>sitemap path without $current.docid parameter</xsl:text>
506
              </xsl:message>
507
            </xsl:otherwise>
508
          </xsl:choose> 
509
          <!-- In either case, add baseuri from its document entry-->
510
          <xsl:variable name="docbaseuri">
511
            <xsl:for-each select="$target.database" >
512
              <xsl:value-of select="key('targetdoc-key', $targetdoc)[1]/@baseuri" />
513
            </xsl:for-each>
514
          </xsl:variable>
515
          <xsl:if test="$docbaseuri != ''" >
516
            <xsl:value-of select="$docbaseuri"/>
517
          </xsl:if>
518
        </xsl:when>
519
        <!-- No database sitemap in use -->
520
        <xsl:otherwise>
521
          <!-- Just use any baseuri from its document entry -->
522
          <xsl:variable name="docbaseuri">
523
            <xsl:for-each select="$target.database" >
524
              <xsl:value-of select="key('targetdoc-key', $targetdoc)[1]/@baseuri" />
525
            </xsl:for-each>
526
          </xsl:variable>
527
          <xsl:if test="$docbaseuri != ''" >
528
            <xsl:value-of select="$docbaseuri"/>
529
          </xsl:if>
530
        </xsl:otherwise>
531
      </xsl:choose>
532
    </xsl:variable>
533
  
534
    <!-- Form the href information -->
535
    <xsl:if test="$baseuri != ''">
536
      <xsl:value-of select="$baseuri"/>
537
      <xsl:if test="substring($target.href,1,1) != '#'">
538
        <!--xsl:text>/</xsl:text-->
539
      </xsl:if>
540
    </xsl:if>
541
    <!-- optionally turn off frag for PDF references -->
542
    <xsl:if test="not($insert.olink.pdf.frag = 0 and
543
          translate(substring($baseuri, string-length($baseuri) - 3),
544
                    'PDF', 'pdf') = '.pdf'
545
          and starts-with($target.href, '#') )">
546
      <xsl:value-of select="$target.href"/>
547
    </xsl:if>
548
  </xsl:if>
549
</xsl:template>
550

    
551
<!-- Computes the href of the object containing the olink element -->
552
<xsl:template name="olink.from.uri">
553
  <xsl:param name="target.database"/>
554
  <xsl:param name="object" select="NotAnElement"/>
555
  <xsl:param name="object.targetdoc" select="$current.docid"/>
556
  <xsl:param name="object.lang" 
557
           select="concat($object/ancestor::*[last()]/@lang,
558
                          $object/ancestor::*[last()]/@xml:lang)"/>
559

    
560
  <xsl:variable name="parent.id">
561
    <xsl:call-template name="object.id">
562
      <xsl:with-param name="object" select="$object"/>
563
    </xsl:call-template>
564
  </xsl:variable>
565

    
566
  <!-- Get the olink key for the parent of olink element -->
567
  <xsl:variable name="from.key">
568
    <xsl:call-template name="select.olink.key">
569
      <xsl:with-param name="targetdoc.att" select="$object.targetdoc"/>
570
      <xsl:with-param name="targetptr.att" select="$parent.id"/>
571
      <xsl:with-param name="olink.lang" select="$object.lang"/>
572
      <xsl:with-param name="target.database" select="$target.database"/>
573
    </xsl:call-template>
574
  </xsl:variable>
575

    
576
  <xsl:variable name="from.olink.href">
577
    <xsl:for-each select="$target.database" >
578
      <xsl:value-of select="key('targetptr-key', $from.key)[1]/@href" />
579
    </xsl:for-each>
580
  </xsl:variable>
581

    
582
  <xsl:choose>
583
    <!-- we found the olink object -->
584
    <xsl:when test="$from.olink.href != ''">
585
      <xsl:value-of select="$from.olink.href"/>
586
    </xsl:when>
587
    <xsl:when test="not($object/parent::*)">
588
      <xsl:value-of select="$from.olink.href"/>
589
    </xsl:when>
590
    <xsl:otherwise>
591
      <!-- recurse upward in current document -->
592
      <xsl:call-template name="olink.from.uri">
593
        <xsl:with-param name="target.database" select="$target.database"/>
594
        <xsl:with-param name="object" select="$object/parent::*"/>
595
        <xsl:with-param name="object.targetdoc" select="$object.targetdoc"/>
596
        <xsl:with-param name="object.lang" select="$object.lang"/>
597
      </xsl:call-template>
598
    </xsl:otherwise>
599
  </xsl:choose>
600

    
601
</xsl:template>
602

    
603
<xsl:template name="olink.hottext">
604
  <xsl:param name="target.database"/>
605
  <xsl:param name="olink.lang" select="''"/>
606
  <xsl:param name="olink.key" select="''"/>
607
  <xsl:param name="referrer" select="."/>
608
  <xsl:param name="xrefstyle">
609
    <xsl:choose>
610
      <xsl:when test="@role and not(@xrefstyle) 
611
                      and $use.role.as.xrefstyle != 0">
612
        <xsl:value-of select="@role"/>
613
      </xsl:when>
614
      <xsl:otherwise>
615
        <xsl:value-of select="@xrefstyle"/>
616
      </xsl:otherwise>
617
    </xsl:choose>
618
  </xsl:param>
619

    
620
  <xsl:choose>
621
    <!-- If it has elements or text (not just PI or comment) -->
622
    <xsl:when test="child::text() or child::*">
623
      <xsl:apply-templates/>
624
    </xsl:when>
625
    <xsl:when test="$olink.key != ''">
626
      <!-- Get the xref text for this record -->
627
      <xsl:variable name="xref.text" >
628
        <xsl:for-each select="$target.database" >
629
          <xsl:call-template name="insert.targetdb.data">
630
            <xsl:with-param name="data"
631
                  select="key('targetptr-key', $olink.key)[1]/xreftext/node()" />
632
          </xsl:call-template>
633
        </xsl:for-each>
634
      </xsl:variable>
635

    
636
      <xsl:variable name="xref.number" >
637
        <xsl:for-each select="$target.database" >
638
          <xsl:value-of select="key('targetptr-key', $olink.key)[1]/@number" />
639
        </xsl:for-each>
640
      </xsl:variable>
641

    
642
      <xsl:variable name="target.elem" >
643
        <xsl:for-each select="$target.database" >
644
          <xsl:value-of select="key('targetptr-key', $olink.key)[1]/@element" />
645
        </xsl:for-each>
646
      </xsl:variable>
647

    
648
      <xsl:variable name="lang">
649
        <xsl:variable name="candidate">
650
          <xsl:for-each select="$target.database" >
651
            <xsl:value-of 
652
                      select="key('targetptr-key', $olink.key)[1]/@lang" />
653
          </xsl:for-each>
654
        </xsl:variable>
655
        <xsl:choose>
656
          <xsl:when test="$candidate != ''">
657
            <xsl:value-of select="$candidate"/>
658
          </xsl:when>
659
          <xsl:otherwise>
660
            <xsl:value-of select="$olink.lang"/>
661
          </xsl:otherwise>
662
        </xsl:choose>
663
      </xsl:variable>
664

    
665
      <xsl:variable name="targetdoc">
666
        <xsl:value-of select="substring-before($olink.key, '/')"/>
667
      </xsl:variable>
668

    
669
      <xsl:choose>
670
        <xsl:when test="$xrefstyle != '' and
671
                        starts-with(normalize-space($xrefstyle), 'select:') and
672
                        (contains($xrefstyle, 'nodocname') or
673
                        contains($xrefstyle, 'nopage')) and
674
                        not(contains($xrefstyle, 'title')) and
675
                        not(contains($xrefstyle, 'label'))"> 
676
          <xsl:copy-of select="$xref.text"/>
677
        </xsl:when>
678
        <xsl:when test="$xrefstyle != ''">
679
          <xsl:if test="$olink.debug != 0">
680
            <xsl:message>
681
              <xsl:text>xrefstyle is '</xsl:text>
682
              <xsl:value-of select="$xrefstyle"/>
683
              <xsl:text>'.</xsl:text>
684
            </xsl:message>
685
          </xsl:if>
686
          <xsl:variable name="template">
687
            <xsl:choose>
688
              <xsl:when test="starts-with(normalize-space($xrefstyle),
689
                                          'select:')">
690
                <xsl:call-template name="make.gentext.template">
691
                  <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
692
                  <xsl:with-param name="purpose" select="'olink'"/>
693
                  <xsl:with-param name="referrer" select="."/>
694
                  <xsl:with-param name="target.elem" select="$target.elem"/>
695
                </xsl:call-template>
696
              </xsl:when>
697
              <xsl:when test="starts-with(normalize-space($xrefstyle),
698
                                          'template:')">
699
                <xsl:value-of select="substring-after(
700
                                 normalize-space($xrefstyle), 'template:')"/>
701
              </xsl:when>
702
              <xsl:otherwise>
703
                <!-- Look for Gentext template with @style attribute -->
704
                <!-- Must compare to no style value because gentext.template
705
                     falls back to no style -->
706

    
707
                <xsl:variable name="xref-context">
708
                  <xsl:call-template name="gentext.template">
709
                    <xsl:with-param name="context" select="'xref'"/>
710
                    <xsl:with-param name="name" select="$target.elem"/>
711
                    <xsl:with-param name="lang" select="$lang"/>
712
                  </xsl:call-template>
713
                </xsl:variable>
714

    
715
                <xsl:variable name="styled-xref-context">
716
                  <xsl:call-template name="gentext.template">
717
                    <xsl:with-param name="context" select="'xref'"/>
718
                    <xsl:with-param name="name" select="$target.elem"/>
719
                    <xsl:with-param name="lang" select="$lang"/>
720
                    <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
721
                  </xsl:call-template>
722
                </xsl:variable>
723

    
724
                <xsl:variable name="xref-number-context">
725
                  <xsl:call-template name="gentext.template">
726
                    <xsl:with-param name="context" select="'xref-number'"/>
727
                    <xsl:with-param name="name" select="$target.elem"/>
728
                    <xsl:with-param name="lang" select="$lang"/>
729
                  </xsl:call-template>
730
                </xsl:variable>
731

    
732
                <xsl:variable name="styled-xref-number-context">
733
                  <xsl:call-template name="gentext.template">
734
                    <xsl:with-param name="context" select="'xref-number'"/>
735
                    <xsl:with-param name="name" select="$target.elem"/>
736
                    <xsl:with-param name="lang" select="$lang"/>
737
                    <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
738
                  </xsl:call-template>
739
                </xsl:variable>
740

    
741
                <xsl:variable name="xref-number-and-title-context">
742
                  <xsl:call-template name="gentext.template">
743
                    <xsl:with-param name="context" 
744
                                    select="'xref-number-and-title'"/>
745
                    <xsl:with-param name="name" select="$target.elem"/>
746
                    <xsl:with-param name="lang" select="$lang"/>
747
                  </xsl:call-template>
748
                </xsl:variable>
749

    
750
                <xsl:variable name="styled-xref-number-and-title-context">
751
                  <xsl:call-template name="gentext.template">
752
                    <xsl:with-param name="context" 
753
                                    select="'xref-number-and-title'"/>
754
                    <xsl:with-param name="name" select="$target.elem"/>
755
                    <xsl:with-param name="lang" select="$lang"/>
756
                    <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
757
                  </xsl:call-template>
758
                </xsl:variable>
759

    
760
                <xsl:choose>
761
                  <xsl:when test="$xref-number-and-title-context != 
762
                                 $styled-xref-number-and-title-context and
763
                                 $xref.number != '' and
764
                                 $xref.with.number.and.title != 0">
765
                    <xsl:value-of 
766
                            select="$styled-xref-number-and-title-context"/>
767
                  </xsl:when>
768
                  <xsl:when test="$xref-number-context != 
769
                                 $styled-xref-number-context and
770
                                 $xref.number != ''">
771
                    <xsl:value-of select="$styled-xref-number-context"/>
772
                  </xsl:when>
773
                  <xsl:when test="$xref-context != $styled-xref-context">
774
                    <xsl:value-of select="$styled-xref-context"/>
775
                  </xsl:when>
776
                  <xsl:when test="$xref-number-and-title-context != '' and
777
                                 $xref.number != '' and
778
                                 $xref.with.number.and.title != 0">
779
                    <xsl:value-of 
780
                            select="$xref-number-and-title-context"/>
781
                    <xsl:if test="$olink.debug">
782
                      <xsl:message>
783
                        <xsl:text>Olink error: no gentext template</xsl:text>
784
                        <xsl:text> exists for xrefstyle '</xsl:text>
785
                        <xsl:value-of select="$xrefstyle"/>
786
                        <xsl:text>' for element '</xsl:text>
787
                        <xsl:value-of select="$target.elem"/>
788
                        <xsl:text>' in language '</xsl:text>
789
                        <xsl:value-of select="$lang"/>
790
                        <xsl:text>' in context 'xref-number-and-title</xsl:text>
791
                        <xsl:text>'. Using template without @style.</xsl:text>
792
                      </xsl:message>
793
                    </xsl:if>
794
                  </xsl:when>
795
                  <xsl:when test="$xref-number-context != '' and
796
                                 $xref.number != ''">
797
                    <xsl:value-of select="$xref-number-context"/>
798
                    <xsl:if test="$olink.debug">
799
                      <xsl:message>
800
                        <xsl:text>Olink error: no gentext template</xsl:text>
801
                        <xsl:text> exists for xrefstyle '</xsl:text>
802
                        <xsl:value-of select="$xrefstyle"/>
803
                        <xsl:text>' for element '</xsl:text>
804
                        <xsl:value-of select="$target.elem"/>
805
                        <xsl:text>' in language '</xsl:text>
806
                        <xsl:value-of select="$lang"/>
807
                        <xsl:text>' in context 'xref-number</xsl:text>
808
                        <xsl:text>'. Using template without @style.</xsl:text>
809
                      </xsl:message>
810
                    </xsl:if>
811
                  </xsl:when>
812
                  <xsl:when test="$xref-context != ''">
813
                    <xsl:value-of select="$xref-context"/>
814
                    <xsl:if test="$olink.debug">
815
                      <xsl:message>
816
                        <xsl:text>Olink error: no gentext template</xsl:text>
817
                        <xsl:text> exists for xrefstyle '</xsl:text>
818
                        <xsl:value-of select="$xrefstyle"/>
819
                        <xsl:text>' for element '</xsl:text>
820
                        <xsl:value-of select="$target.elem"/>
821
                        <xsl:text>' in language '</xsl:text>
822
                        <xsl:value-of select="$lang"/>
823
                        <xsl:text>' in context 'xref</xsl:text>
824
                        <xsl:text>'. Using template without @style.</xsl:text>
825
                      </xsl:message>
826
                    </xsl:if>
827
                  </xsl:when>
828
                  <xsl:otherwise>
829
                    <xsl:message>
830
                      <xsl:text>Olink error: no gentext template</xsl:text>
831
                      <xsl:text> exists for xrefstyle '</xsl:text>
832
                      <xsl:value-of select="$xrefstyle"/>
833
                      <xsl:text>' for element '</xsl:text>
834
                      <xsl:value-of select="$target.elem"/>
835
                      <xsl:text>' in language '</xsl:text>
836
                      <xsl:value-of select="$lang"/>
837
                      <xsl:text>'. Trying '%t'.</xsl:text>
838
                    </xsl:message>
839
                    <xsl:value-of select="'%t'"/>
840
                  </xsl:otherwise>
841
                </xsl:choose>
842
              </xsl:otherwise>
843
            </xsl:choose>
844
          </xsl:variable>
845

    
846
          <xsl:if test="$olink.debug != 0">
847
            <xsl:message>
848
              <xsl:text>Olink debug: xrefstyle template is '</xsl:text>
849
              <xsl:value-of select="$template"/>
850
              <xsl:text>'.</xsl:text>
851
            </xsl:message>
852
          </xsl:if>
853

    
854
          <xsl:call-template name="substitute-markup">
855
            <xsl:with-param name="template" select="$template"/>
856
            <xsl:with-param name="title">
857
              <xsl:for-each select="$target.database" >
858
                <xsl:call-template name="insert.targetdb.data">
859
                  <xsl:with-param name="data"
860
                                  select="key('targetptr-key', $olink.key)/ttl" />
861
                </xsl:call-template>
862
              </xsl:for-each>
863
            </xsl:with-param>
864
            <xsl:with-param name="label">
865
              <xsl:for-each select="$target.database" >
866
                <xsl:value-of 
867
                        select="key('targetptr-key', $olink.key)[1]/@number" />
868
              </xsl:for-each>
869
            </xsl:with-param>
870
            <xsl:with-param name="pagenumber">
871
              <xsl:for-each select="$target.database" >
872
                <xsl:value-of 
873
                        select="key('targetptr-key', $olink.key)[1]/@page" />
874
              </xsl:for-each>
875
            </xsl:with-param>
876
            <xsl:with-param name="docname">
877
              <xsl:for-each select="$target.database" >
878
                <xsl:call-template name="insert.targetdb.data">
879
                  <xsl:with-param name="data"
880
                       select="key('targetdoc-key', $targetdoc)[1]/div[1]/ttl" />
881
                </xsl:call-template>
882
              </xsl:for-each>
883
            </xsl:with-param>
884
          </xsl:call-template>
885
        </xsl:when>
886

    
887
        <xsl:when test="$use.local.olink.style != 0">
888

    
889
          <!-- Is autonumbering on? -->
890
          <xsl:variable name="target.number">
891
            <xsl:for-each select="$target.database" >
892
              <xsl:value-of 
893
                      select="key('targetptr-key', $olink.key)[1]/@number" />
894
            </xsl:for-each>
895
          </xsl:variable>
896

    
897
          <xsl:variable name="autonumber">
898
            <xsl:choose>
899
              <xsl:when test="$target.number != ''">1</xsl:when>
900
              <xsl:otherwise>0</xsl:otherwise>
901
            </xsl:choose>
902
          </xsl:variable>
903
        
904
          <xsl:variable name="number-and-title-template">
905
            <xsl:call-template name="gentext.template.exists">
906
              <xsl:with-param name="context" select="'xref-number-and-title'"/>
907
              <xsl:with-param name="name" select="$target.elem"/>
908
            </xsl:call-template>
909
          </xsl:variable>
910
        
911
          <xsl:variable name="number-template">
912
            <xsl:call-template name="gentext.template.exists">
913
              <xsl:with-param name="context" select="'xref-number'"/>
914
              <xsl:with-param name="name" select="$target.elem"/>
915
            </xsl:call-template>
916
          </xsl:variable>
917
        
918
          <xsl:variable name="context">
919
            <xsl:choose>
920
              <xsl:when test="string($autonumber) != 0 
921
                              and $number-and-title-template != 0
922
                              and $xref.with.number.and.title != 0">
923
                 <xsl:value-of select="'xref-number-and-title'"/>
924
              </xsl:when>
925
              <xsl:when test="string($autonumber) != 0 
926
                              and $number-template != 0">
927
                 <xsl:value-of select="'xref-number'"/>
928
              </xsl:when>
929
              <xsl:otherwise>
930
                 <xsl:value-of select="'xref'"/>
931
              </xsl:otherwise>
932
            </xsl:choose>
933
          </xsl:variable>
934
        
935
          <xsl:variable name="template">
936
            <xsl:call-template name="gentext.template">
937
              <xsl:with-param name="context" select="$context"/>
938
              <xsl:with-param name="name" select="$target.elem"/>
939
              <xsl:with-param name="lang" select="$lang"/>
940
            </xsl:call-template>
941
          </xsl:variable>
942

    
943
          <xsl:call-template name="substitute-markup">
944
            <xsl:with-param name="template" select="$template"/>
945
            <xsl:with-param name="title">
946
              <xsl:for-each select="$target.database" >
947
                <xsl:call-template name="insert.targetdb.data">
948
                  <xsl:with-param name="data"
949
                                  select="key('targetptr-key', $olink.key)[1]/ttl" />
950
                </xsl:call-template>
951
              </xsl:for-each>
952
            </xsl:with-param>
953
            <xsl:with-param name="label">
954
              <xsl:for-each select="$target.database" >
955
                <xsl:call-template name="insert.targetdb.data">
956
                  <xsl:with-param name="data"
957
                          select="key('targetptr-key', $olink.key)[1]/@number" />
958
                </xsl:call-template>
959
              </xsl:for-each>
960
            </xsl:with-param>
961
          </xsl:call-template>
962
        </xsl:when>
963
        <xsl:when test="$xref.text !=''">
964
          <xsl:copy-of select="$xref.text"/>
965
        </xsl:when>
966
        <xsl:otherwise>
967
          <xsl:message>
968
            <xsl:text>Olink error: no generated text for </xsl:text>
969
            <xsl:text>targetdoc/targetptr/lang = '</xsl:text>
970
            <xsl:value-of select="$olink.key"/>
971
            <xsl:text>'.</xsl:text>
972
          </xsl:message>
973
          <xsl:text>????</xsl:text>
974
        </xsl:otherwise>
975
      </xsl:choose>
976
    </xsl:when>
977
    <xsl:when test="@targetdoc != '' or @targetptr != ''">
978
      <xsl:if test="$olink.key != ''">
979
        <xsl:message>
980
          <xsl:text>Olink error: no generated text for </xsl:text>
981
          <xsl:text>targetdoc/targetptr/lang = '</xsl:text>
982
          <xsl:value-of select="$olink.key"/>
983
          <xsl:text>'.</xsl:text>
984
        </xsl:message>
985
      </xsl:if>
986
      <xsl:text>????</xsl:text>
987
    </xsl:when>
988
    <xsl:otherwise>
989
      <!-- old style olink -->
990
      <xsl:call-template name="olink.outline">
991
        <xsl:with-param name="outline.base.uri"
992
                        select="unparsed-entity-uri(@targetdocent)"/>
993
        <xsl:with-param name="localinfo" select="@localinfo"/>
994
        <xsl:with-param name="return" select="'xreftext'"/>
995
      </xsl:call-template>
996
    </xsl:otherwise>
997
  </xsl:choose>
998
</xsl:template>
999

    
1000
<xsl:template name="insert.targetdb.data">
1001
  <xsl:param name="data"/>
1002
  <!-- Customize this to massage data further -->
1003
  <xsl:copy-of select="$data"/>
1004
</xsl:template>
1005

    
1006
<xsl:template match="*" mode="olink.docname.markup">
1007
  <!-- No-op for now -->
1008
</xsl:template>
1009

    
1010
<xsl:template name="targetpath">
1011
  <xsl:param name="dirnode" />
1012
  <xsl:param name="targetdoc" select="''"/>
1013

    
1014
<!-- 
1015
<xsl:message>dirnode is <xsl:value-of select="$dirnode/@name"/></xsl:message>
1016
<xsl:message>targetdoc is <xsl:value-of select="$targetdoc"/></xsl:message>
1017
-->
1018
  <!-- recursive template generates path to olink target directory -->
1019
  <xsl:choose>
1020
    <!-- Have we arrived at the final path step? -->
1021
    <xsl:when test="$dirnode/child::document[@targetdoc = $targetdoc]">
1022
      <!-- We are done -->
1023
    </xsl:when>
1024
    <!-- Have we reached the top without a match? -->
1025
    <xsl:when test="local-name($dirnode) != 'dir'" >
1026
        <xsl:message>Olink error: cannot locate targetdoc <xsl:value-of select="$targetdoc"/> in sitemap</xsl:message>
1027
    </xsl:when>
1028
    <!-- Is the target in a descendant? -->
1029
    <xsl:when test="$dirnode/descendant::document/@targetdoc = $targetdoc">
1030
      <xsl:variable name="step" select="$dirnode/child::dir[descendant::document/@targetdoc = $targetdoc]"/>
1031
      <xsl:if test = "$step">
1032
        <xsl:value-of select="$step/@name"/>
1033
        <xsl:text>/</xsl:text>
1034
      </xsl:if>
1035
      <!-- Now recurse with the child -->
1036
      <xsl:call-template name="targetpath" >
1037
        <xsl:with-param name="dirnode" select="$step"/>
1038
        <xsl:with-param name="targetdoc" select="$targetdoc"/>
1039
      </xsl:call-template>
1040
    </xsl:when>
1041
    <!-- Otherwise we need to move up a step -->
1042
    <xsl:otherwise>
1043
      <xsl:if test="$dirnode/parent::dir">
1044
        <xsl:text>../</xsl:text>
1045
      </xsl:if>
1046
      <xsl:call-template name="targetpath" >
1047
        <xsl:with-param name="dirnode" select="$dirnode/parent::*"/>
1048
        <xsl:with-param name="targetdoc" select="$targetdoc"/>
1049
      </xsl:call-template>
1050
    </xsl:otherwise>
1051
  </xsl:choose>
1052
</xsl:template>
1053

    
1054
<xsl:template name="olink.page.citation">
1055
  <xsl:param name="olink.key" select="''"/>
1056
  <xsl:param name="olink.lang" select="'en'"/>
1057
  <xsl:param name="target.database"/>
1058
  <xsl:param name="linkend" select="''"/>
1059
  <xsl:param name="xrefstyle">
1060
    <xsl:choose>
1061
      <xsl:when test="@role and not(@xrefstyle) 
1062
                      and $use.role.as.xrefstyle != 0">
1063
        <xsl:value-of select="@role"/>
1064
      </xsl:when>
1065
      <xsl:otherwise>
1066
        <xsl:value-of select="@xrefstyle"/>
1067
      </xsl:otherwise>
1068
    </xsl:choose>
1069
  </xsl:param>
1070

    
1071
  <xsl:variable name="targetdoc">
1072
    <xsl:value-of select="substring-before($olink.key, '/')"/>
1073
  </xsl:variable>
1074

    
1075
  <xsl:choose>
1076
    <xsl:when test="$linkend != ''">
1077
      <xsl:call-template name="xref.page.citation">
1078
        <xsl:with-param name="linkend" select="$linkend"/>
1079
        <xsl:with-param name="target" select="key('id', $linkend)"/>
1080
        <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
1081
      </xsl:call-template>
1082
    </xsl:when>
1083
    <xsl:when test="not(starts-with(normalize-space($xrefstyle),
1084
                        'select:') 
1085
                and (contains($xrefstyle, 'page')
1086
                     or contains($xrefstyle, 'Page')))
1087
                and $current.docid != '' 
1088
                and $current.docid != $targetdoc
1089
                and $insert.olink.page.number = 'yes' ">
1090
  
1091
      <xsl:variable name="page-number">
1092
        <xsl:for-each select="$target.database" >
1093
          <xsl:value-of 
1094
                 select="key('targetptr-key', $olink.key)[1]/@page" />
1095
        </xsl:for-each>
1096
      </xsl:variable>
1097
  
1098
      <xsl:if test="$page-number != ''">
1099
        <xsl:call-template name="substitute-markup">
1100
          <xsl:with-param name="template">
1101
            <xsl:call-template name="gentext.template">
1102
              <xsl:with-param name="name" select="'olink.page.citation'"/>
1103
              <xsl:with-param name="context" select="'xref'"/>
1104
              <xsl:with-param name="lang" select="$olink.lang"/>
1105
            </xsl:call-template>
1106
          </xsl:with-param>
1107
          <xsl:with-param name="pagenumber" select="$page-number"/>
1108
        </xsl:call-template>
1109
      </xsl:if>
1110
  
1111
    </xsl:when>
1112
  </xsl:choose>
1113
</xsl:template>
1114

    
1115
<xsl:template name="olink.document.citation">
1116
  <xsl:param name="olink.key" select="''"/>
1117
  <xsl:param name="olink.lang" select="'en'"/>
1118
  <xsl:param name="target.database"/>
1119
  <xsl:param name="xrefstyle">
1120
    <xsl:choose>
1121
      <xsl:when test="@role and not(@xrefstyle) 
1122
                      and $use.role.as.xrefstyle != 0">
1123
        <xsl:value-of select="@role"/>
1124
      </xsl:when>
1125
      <xsl:otherwise>
1126
        <xsl:value-of select="@xrefstyle"/>
1127
      </xsl:otherwise>
1128
    </xsl:choose>
1129
  </xsl:param>
1130

    
1131
  <xsl:variable name="page">
1132
    <xsl:for-each select="$target.database" >
1133
      <xsl:value-of 
1134
             select="key('targetptr-key', $olink.key)[1]/@page" />
1135
    </xsl:for-each>
1136
  </xsl:variable>
1137

    
1138
  <xsl:variable name="targetdoc">
1139
    <xsl:value-of select="substring-before($olink.key, '/')"/>
1140
  </xsl:variable>
1141

    
1142
  <xsl:variable name="targetptr">
1143
    <xsl:value-of 
1144
          select="substring-before(substring-after($olink.key, '/'), '/')"/>
1145
  </xsl:variable>
1146

    
1147
  <!-- Don't add docname if pointing to root element -->
1148
  <xsl:variable name="rootptr">
1149
    <xsl:for-each select="$target.database" >
1150
      <xsl:value-of 
1151
             select="key('targetdoc-key', $targetdoc)[1]/div[1]/@targetptr" />
1152
    </xsl:for-each>
1153
  </xsl:variable>
1154

    
1155
  <xsl:variable name="docname">
1156
    <xsl:for-each select="$target.database" >
1157
      <xsl:call-template name="insert.targetdb.data">
1158
        <xsl:with-param name="data"
1159
             select="key('targetdoc-key', $targetdoc)[1]/div[1]/ttl" />
1160
      </xsl:call-template>
1161
    </xsl:for-each>
1162
  </xsl:variable>
1163

    
1164
  <xsl:if test="not(starts-with(normalize-space($xrefstyle), 'select:') 
1165
              and (contains($xrefstyle, 'docname')))
1166
              and ($olink.doctitle = 'yes' or $olink.doctitle = '1')
1167
              and $current.docid != '' 
1168
              and $rootptr != $targetptr
1169
              and $current.docid != $targetdoc
1170
              and $docname != ''">
1171
    <xsl:call-template name="substitute-markup">
1172
      <xsl:with-param name="template">
1173
        <xsl:call-template name="gentext.template">
1174
          <xsl:with-param name="name" select="'olink.document.citation'"/>
1175
          <xsl:with-param name="context" select="'xref'"/>
1176
          <xsl:with-param name="lang" select="$olink.lang"/>
1177
        </xsl:call-template>
1178
      </xsl:with-param>
1179
      <xsl:with-param name="docname" select="$docname"/>
1180
      <xsl:with-param name="pagenumber" select="$page"/>
1181
    </xsl:call-template>
1182
  </xsl:if>
1183
</xsl:template>
1184

    
1185
<xsl:template name="xref.page.citation">
1186
  <!-- Determine if this xref should have a page citation.
1187
       Context node is the xref or local olink element -->
1188
  <xsl:param name="linkend" select="@linkend"/>
1189
  <xsl:param name="target" select="key('id', $linkend)"/>
1190
  <xsl:param name="xrefstyle">
1191
    <xsl:choose>
1192
      <xsl:when test="@role and not(@xrefstyle) 
1193
                      and $use.role.as.xrefstyle != 0">
1194
        <xsl:value-of select="@role"/>
1195
      </xsl:when>
1196
      <xsl:otherwise>
1197
        <xsl:value-of select="@xrefstyle"/>
1198
      </xsl:otherwise>
1199
    </xsl:choose>
1200
  </xsl:param>
1201

    
1202
  <xsl:if test="not(starts-with(normalize-space($xrefstyle),'select:')
1203
                    and (contains($xrefstyle, 'page')
1204
                         or contains($xrefstyle, 'Page')))
1205
                and ( $insert.xref.page.number = 'yes' 
1206
                   or $insert.xref.page.number = '1')
1207
                or local-name($target) = 'para'">
1208
    <xsl:apply-templates select="$target" mode="page.citation">
1209
      <xsl:with-param name="id" select="$linkend"/>
1210
    </xsl:apply-templates>
1211
  </xsl:if>
1212
</xsl:template>
1213

    
1214
</xsl:stylesheet>
1215

    
(48-48/78)