1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY lowercase "'abcdefghijklmnopqrstuvwxyz'">
<!ENTITY uppercase "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'">
<!ENTITY primary 'normalize-space(concat(primary/@sortas, primary[not(@sortas)]))'>
<!ENTITY secondary 'normalize-space(concat(secondary/@sortas, secondary[not(@sortas)]))'>
<!ENTITY tertiary 'normalize-space(concat(tertiary/@sortas, tertiary[not(@sortas)]))'>
<!ENTITY section '(ancestor-or-self::set
|ancestor-or-self::book
|ancestor-or-self::part
|ancestor-or-self::reference
|ancestor-or-self::partintro
|ancestor-or-self::chapter
|ancestor-or-self::appendix
|ancestor-or-self::preface
|ancestor-or-self::article
|ancestor-or-self::section
|ancestor-or-self::sect1
|ancestor-or-self::sect2
|ancestor-or-self::sect3
|ancestor-or-self::sect4
|ancestor-or-self::sect5
|ancestor-or-self::refentry
|ancestor-or-self::refsect1
|ancestor-or-self::refsect2
|ancestor-or-self::refsect3
|ancestor-or-self::simplesect
|ancestor-or-self::bibliography
|ancestor-or-self::glossary
|ancestor-or-self::index
|ancestor-or-self::webpage)[last()]'>
<!ENTITY section.id 'generate-id(§ion;)'>
<!ENTITY sep '" "'>
<!ENTITY scope 'count(ancestor::node()|$scope) = count(ancestor::node())
and ($role = @role or $type = @type or
(string-length($role) = 0 and string-length($type) = 0))'>
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- ********************************************************************
$Id$
********************************************************************
This file is part of the XSL DocBook Stylesheet distribution.
See ../README or http://nwalsh.com/docbook/xsl/ for copyright
and other information.
******************************************************************** -->
<!-- ==================================================================== -->
<!-- Jeni Tennison gets all the credit for what follows.
I think I understand it :-) Anyway, I've hacked it a bit, so the
bugs are mine. -->
<xsl:key name="letter"
match="indexterm"
use="translate(substring(&primary;, 1, 1),&lowercase;,&uppercase;)"/>
<xsl:key name="primary"
match="indexterm"
use="&primary;"/>
<xsl:key name="secondary"
match="indexterm"
use="concat(&primary;, &sep;, &secondary;)"/>
<xsl:key name="tertiary"
match="indexterm"
use="concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;)"/>
<xsl:key name="endofrange"
match="indexterm[@class='endofrange']"
use="@startref"/>
<xsl:key name="primary-section"
match="indexterm[not(secondary) and not(see)]"
use="concat(&primary;, &sep;, §ion.id;)"/>
<xsl:key name="secondary-section"
match="indexterm[not(tertiary) and not(see)]"
use="concat(&primary;, &sep;, &secondary;, &sep;, §ion.id;)"/>
<xsl:key name="tertiary-section"
match="indexterm[not(see)]"
use="concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;, &sep;, §ion.id;)"/>
<xsl:key name="see-also"
match="indexterm[seealso]"
use="concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;, &sep;, seealso)"/>
<xsl:key name="see"
match="indexterm[see]"
use="concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;, &sep;, see)"/>
<xsl:key name="sections" match="*[@id]" use="@id"/>
<xsl:template name="generate-index">
<xsl:param name="scope" select="(ancestor::book|/)[last()]"/>
<xsl:variable name="role">
<xsl:if test="$index.on.role != 0">
<xsl:value-of select="@role"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="type">
<xsl:if test="$index.on.type != 0">
<xsl:value-of select="@type"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="terms"
select="//indexterm[count(.|key('letter',
translate(substring(&primary;, 1, 1),
&lowercase;,
&uppercase;))[&scope;][1]) = 1
and not(@class = 'endofrange')]"/>
<xsl:variable name="alphabetical"
select="$terms[tqcontains(concat(&lowercase;, &uppercase;),
substring(&primary;, 1, 1))]"/>
<xsl:variable name="others" select="$terms[not(tqcontains(concat(&lowercase;,
&uppercase;),
substring(&primary;, 1, 1)))]"/>
<div class="index">
<xsl:if test="$others">
<div class="indexdiv">
<h3>
<xsl:call-template name="gentext">
<xsl:with-param name="key" select="'index symbols'"/>
</xsl:call-template>
</h3>
<dl>
<xsl:apply-templates select="$others[count(.|key('primary',
&primary;)[&scope;][1]) = 1]"
mode="index-symbol-div">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&primary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</dl>
</div>
</xsl:if>
<xsl:apply-templates select="$alphabetical[count(.|key('letter',
translate(substring(&primary;, 1, 1),
&lowercase;,&uppercase;))[&scope;][1]) = 1]"
mode="index-div">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&primary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</div>
</xsl:template>
<xsl:template match="indexterm" mode="index-div">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:variable name="key"
select="translate(substring(&primary;, 1, 1),&lowercase;,&uppercase;)"/>
<!-- Make sure that we don't generate a div if there are no terms in scope -->
<xsl:if test="key('letter', $key)[&scope;]
[count(.|key('primary', &primary;)[&scope;][1]) = 1]">
<div class="indexdiv">
<xsl:if test="tqcontains(concat(&lowercase;, &uppercase;), $key)">
<h3>
<xsl:value-of select="translate($key, &lowercase;, &uppercase;)"/>
</h3>
</xsl:if>
<dl>
<xsl:apply-templates select="key('letter', $key)[&scope;]
[count(.|key('primary', &primary;)[&scope;][1])=1]"
mode="index-primary">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&primary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</dl>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="indexterm" mode="index-symbol-div">
<xsl:param name="scope" select="/"/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:variable name="key" select="translate(substring(&primary;, 1, 1),
&lowercase;,&uppercase;)"/>
<xsl:apply-templates select="key('letter', $key)
[&scope;][count(.|key('primary', &primary;)[1]) = 1]"
mode="index-primary">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&primary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="indexterm" mode="index-primary">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:variable name="key" select="&primary;"/>
<xsl:variable name="refs" select="key('primary', $key)[&scope;]"/>
<dt>
<xsl:value-of select="primary"/>
<xsl:for-each select="$refs[generate-id() = generate-id(key('primary-section', concat($key, &sep;, §ion.id;))[&scope;][1])]">
<xsl:apply-templates select="." mode="reference">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:if test="$refs[not(secondary)]/*[self::see]">
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(&primary;, &sep;, &sep;, &sep;, see))[&scope;][1])]"
mode="index-see">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(see, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</xsl:if>
</dt>
<xsl:if test="$refs/secondary or $refs[not(secondary)]/*[self::seealso]">
<dd>
<dl>
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(&primary;, &sep;, &sep;, &sep;, seealso))[&scope;][1])]"
mode="index-seealso">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(seealso, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
<xsl:apply-templates select="$refs[secondary and count(.|key('secondary', concat($key, &sep;, &secondary;))[&scope;][1]) = 1]"
mode="index-secondary">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&secondary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</dl>
</dd>
</xsl:if>
</xsl:template>
<xsl:template match="indexterm" mode="index-secondary">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:variable name="key" select="concat(&primary;, &sep;, &secondary;)"/>
<xsl:variable name="refs" select="key('secondary', $key)[&scope;]"/>
<dt>
<xsl:value-of select="secondary"/>
<xsl:for-each select="$refs[generate-id() = generate-id(key('secondary-section', concat($key, &sep;, §ion.id;))[&scope;][1])]">
<xsl:apply-templates select="." mode="reference">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:if test="$refs[not(tertiary)]/*[self::see]">
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(&primary;, &sep;, &secondary;, &sep;, &sep;, see))[&scope;][1])]"
mode="index-see">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(see, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</xsl:if>
</dt>
<xsl:if test="$refs/tertiary or $refs[not(tertiary)]/*[self::seealso]">
<dd>
<dl>
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(&primary;, &sep;, &secondary;, &sep;, &sep;, seealso))[&scope;][1])]"
mode="index-seealso">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(seealso, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
<xsl:apply-templates select="$refs[tertiary and count(.|key('tertiary', concat($key, &sep;, &tertiary;))[&scope;][1]) = 1]"
mode="index-tertiary">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(&tertiary;, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</dl>
</dd>
</xsl:if>
</xsl:template>
<xsl:template match="indexterm" mode="index-tertiary">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:variable name="key" select="concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;)"/>
<xsl:variable name="refs" select="key('tertiary', $key)[&scope;]"/>
<dt>
<xsl:value-of select="tertiary"/>
<xsl:for-each select="$refs[generate-id() = generate-id(key('tertiary-section', concat($key, &sep;, §ion.id;))[&scope;][1])]">
<xsl:apply-templates select="." mode="reference">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:if test="$refs/see">
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see', concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;, &sep;, see))[&scope;][1])]"
mode="index-see">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(see, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</xsl:if>
</dt>
<xsl:if test="$refs/seealso">
<dd>
<dl>
<xsl:apply-templates select="$refs[generate-id() = generate-id(key('see-also', concat(&primary;, &sep;, &secondary;, &sep;, &tertiary;, &sep;, seealso))[&scope;][1])]"
mode="index-seealso">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:sort select="translate(seealso, &lowercase;, &uppercase;)"/>
</xsl:apply-templates>
</dl>
</dd>
</xsl:if>
</xsl:template>
<xsl:template match="indexterm" mode="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="separator" select="', '"/>
<xsl:value-of select="$separator"/>
<xsl:choose>
<xsl:when test="@zone and string(@zone)">
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="normalize-space(@zone)"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="§ion;/titleabbrev and $index.prefer.titleabbrev != 0">
<xsl:apply-templates select="§ion;" mode="titleabbrev.markup"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="§ion;" mode="title.markup"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="href">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="§ion;"/>
</xsl:call-template>
</xsl:attribute>
<xsl:value-of select="$title"/> <!-- text only -->
</a>
<xsl:if test="key('endofrange', @id)[&scope;]">
<xsl:apply-templates select="key('endofrange', @id)[&scope;][last()]"
mode="reference">
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
<xsl:with-param name="separator" select="'-'"/>
</xsl:apply-templates>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="reference">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:param name="zones"/>
<xsl:choose>
<xsl:when test="tqcontains($zones, ' ')">
<xsl:variable name="zone" select="substring-before($zones, ' ')"/>
<xsl:variable name="target" select="key('sections', $zone)[&scope;]"/>
<a>
<xsl:attribute name="href">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="$target[1]"/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates select="$target[1]" mode="index-title-content"/>
</a>
<xsl:text>, </xsl:text>
<xsl:call-template name="reference">
<xsl:with-param name="zones" select="substring-after($zones, ' ')"/>
<xsl:with-param name="scope" select="$scope"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="zone" select="$zones"/>
<xsl:variable name="target" select="key('sections', $zone)[&scope;]"/>
<a>
<xsl:attribute name="href">
<xsl:call-template name="href.target">
<xsl:with-param name="object" select="$target[1]"/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates select="$target[1]" mode="index-title-content"/>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="indexterm" mode="index-see">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:text> (</xsl:text>
<xsl:call-template name="gentext">
<xsl:with-param name="key" select="'see'"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:value-of select="see"/>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="indexterm" mode="index-seealso">
<xsl:param name="scope" select="."/>
<xsl:param name="role" select="''"/>
<xsl:param name="type" select="''"/>
<xsl:for-each select="seealso">
<xsl:sort select="translate(., &lowercase;, &uppercase;)"/>
<dt>
<xsl:text>(</xsl:text>
<xsl:call-template name="gentext">
<xsl:with-param name="key" select="'seealso'"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>)</xsl:text>
</dt>
</xsl:for-each>
</xsl:template>
<xsl:template match="*" mode="index-title-content">
<xsl:variable name="title">
<xsl:apply-templates select="§ion;" mode="title.markup"/>
</xsl:variable>
<xsl:value-of select="$title"/>
</xsl:template>
</xsl:stylesheet>
|