summaryrefslogtreecommitdiff
path: root/xslt/mallard/common/mal-link.xsl
blob: 128e248f791253a9de5b0ad7595aee2311e7fbea (plain)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
<!--
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License
along with this program; see the file COPYING.LGPL.  If not, see <http://www.gnu.org/licenses/>.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:mal="http://projectmallard.org/1.0/"
                xmlns:cache="http://projectmallard.org/cache/1.0/"
                xmlns:exsl="http://exslt.org/common"
                xmlns:str="http://exslt.org/strings"
                exclude-result-prefixes="mal cache exsl str"
                version="1.0">

<!--!!==========================================================================
Mallard Links
Common linking utilities for Mallard documents.
@revision[version=3.4 date=2012-01-17 status=final]

This stylesheet contains various utilities for handling links in Mallard
documents.  The templates in this stylesheet make it easier to handle the
different linking mechanisms in Mallard, including the dynamic automatic
linking systems.
-->


<!--@@==========================================================================
mal.cache.file
The location of the cache file.
@revision[version=3.0 date=2010-01-02 status=final]

In order to locate and process links between pages, this stylesheet requires
a Mallard cache file.  Use this parameter to pass the path to a valid cache
file.
-->
<xsl:param name="mal.cache.file"/>


<!--@@==========================================================================
mal.cache
The cache document as a node set.
@revision[version=3.0 date=2010-01-02 status=final]

This parameter points to the root `cache:cache` element of a Mallard cache
document.  By default, it selects the root element from the file provided in
{mal.cache.file}.

Some processing tools may create a Mallard cache document without outputting
it to a file.  Those tools can use this parameter directly.
-->
<xsl:param name="mal.cache" select="document($mal.cache.file, /)/cache:cache"/>


<!--++==========================================================================
mal.cache.key
Get a page or section from the `id` attribute.
@revision[version=3.4 date=2012-01-25 status=final]

This key returns a `page` or `section` element from a Mallard cache file from
the `id` attribute. In cache files, the `id` attribute of `section` elements
is set to a qualified ID by prefixing it with the containing page ID and the
hash character.

The context node must be in the document {mal.cache} when this key is called.
-->
<xsl:key name="mal.cache.key" match="cache:cache//*" use="@id"/>


<!--++==========================================================================
mal.cache.link.key
Get `link` elements from a link type and `xref` attribute.
@revision[version=3.4 date=2012-01-25 status=final]

This key returns all `link` elements from a Mallard cache file from the `type`
and `xref` attributes. They key is the concatenation of the `type` attribute,
the colon character, and the `xref` attribute. Only `link` elements with both
a `type` and `xref` attribute are supported.

The context node must be in the document {mal.cache} when this key is called.
-->
<xsl:key name="mal.cache.link.key"
         match="mal:info/mal:link[@type][@xref]"
         use="concat(@type, ':', @xref)"/>


<!--@@==========================================================================
mal.link.prefix
A prefix for link targets.
@revision[version=3.4 date=2012-01-17 status=final]

When link targets are constructed by {mal.link.target} from `xref` attributes,
this string is prepended. This can be used, for example, to specify absolute
directories or URLs.
-->
<xsl:param name="mal.link.prefix"/>


<!--@@==========================================================================
mal.link.extension
The filename extension for output files.
@revision[version=3.4 date=2012-01-17 status=final]

When link targets are constructed by {mal.link.target} from `xref` attributes,
this string is appended. This is used to specify the file extension when creating
output files from Mallard pages.
-->
<xsl:param name="mal.link.extension"/>


<!--@@==========================================================================
mal.link.default_root
The default root ID.
@revision[version=3.4 date=2012-01-17 status=final]

This parameter provides the default ID for the page that is taken to be the
root of the document.  By default, `'index'` is used.  This should not be
changed for normal Mallard documents.  It may be necessary to change it for
some Mallard extension formats.
-->
<xsl:param name="mal.link.default_root" select="'index'"/>


<!--**==========================================================================
mal.link.linkid
Output the fully qualified link ID for a page or section.
@revision[version=3.0 date=2010-01-02 status=final]

[xsl:params]
$node: The `page` or `section` element to generate a link ID for.

This template outputs the fully qualified link ID for a page or section.  For
`page` elements, the link ID is identical to the ID.  For `section` elements,
however, the link ID is the containing page ID and the section ID, joined with
the `#` character.

The link ID is used in Mallard cache files to ensure all `id` attributes are
unique.  All of the templates in this stylesheet that use a link ID use this
template or {mal.link.xref.linkid}.
-->
<xsl:template name="mal.link.linkid">
  <xsl:param name="node" select="."/>
  <xsl:choose>
    <xsl:when test="contains($node/@id, '#')">
      <xsl:value-of select="$node/@id"/>
    </xsl:when>
    <xsl:when test="$node/self::mal:section">
      <xsl:value-of select="concat($node/ancestor::mal:page[1]/@id, '#', $node/@id)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$node/@id"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.xref.linkid
Output the fully qualified link ID for an `xref` attribute.
@revision[version=3.0 date=2010-01-02 status=final]

[xsl:params]
$node: The element containing an `xref` attribute.
$xref: The `xref` value to generate a link ID from.

This template outputs the fully qualified link ID for an `xref` attribute.
This may simply be $xref, but if $xref starts with the `#` character, it
is prefixed with the ID of the page that contains $node.

See {mal.link.linkid} for more on link IDs.
-->
<xsl:template name="mal.link.xref.linkid">
  <xsl:param name="node" select="."/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:if test="starts-with($xref, '#')">
    <xsl:value-of select="$node/ancestor-or-self::mal:page/@id"/>
  </xsl:if>
  <xsl:value-of select="$xref"/>
</xsl:template>


<!--**==========================================================================
mal.link.content
Output the content for a `link` element.
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate title.
$info: An `info` element that overrides the info found in a target node.

This template outputs the automatic text content for a link.  It should only
be used for links that do not have specified content.  If $xref points to a
valid page or section, the appropriate link title from that page or section
will be selected, based on the list of roles in $role. The first role for
which a matching link title is found will be used. Otherwise, the link title
without a role is used, or the primary title. The {mal.link.content.mode}
mode is applied to the contents of that title.  Stylesheets using this template
should map that mode to inline processing.

For inline links, $node should be the `link` element. For links from a
`links` element, $node should be that `links` element, or the containing
element when the `links` element is implicit.

This template first calls {mal.link.content.custom} with the same arguments.
If that template returns a non-empty result, it is used as the return value,
overriding any other behavior of this template.

If only $href is provided, that URL is used as the text content.  If a target
page or section cannot be found, $xref is used as the text content.

Normally, this template automatically looks up information from a targret node
according to the $xref parameter. However, if the $info parameter is given,
information in that node set is used instead. This is useful for external info
links, where the target information is provided as child elements to the `link`
element.
-->
<xsl:template name="mal.link.content">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
  <xsl:variable name="custom">
    <xsl:call-template name="mal.link.content.custom">
      <xsl:with-param name="node" select="$node"/>
      <xsl:with-param name="action" select="$action"/>
      <xsl:with-param name="xref" select="$xref"/>
      <xsl:with-param name="href" select="$href"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="info" select="$info"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="exsl:node-set($custom)/node()">
      <xsl:copy-of select="$custom"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="linkid">
        <xsl:call-template name="mal.link.xref.linkid">
          <xsl:with-param name="node" select="$node"/>
          <xsl:with-param name="xref" select="$xref"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:for-each select="$mal.cache">
        <xsl:variable name="target" select="key('mal.cache.key', $linkid)"/>
        <xsl:choose>
          <xsl:when test="$target or $info">
            <xsl:variable name="infos" select="$target/mal:info | $info"/>
            <xsl:variable name="titles" select="$infos/mal:title[@type = 'link']"/>
            <xsl:variable name="realrole">
              <xsl:for-each select="str:split($role)">
                <xsl:variable name="thisrole" select="string(.)"/>
                <xsl:if test="$titles[@role=$thisrole]">
                  <xsl:value-of select="concat($thisrole, ' ')"/>
                </xsl:if>
              </xsl:for-each>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="$realrole != ''">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$titles[@role = substring-before($realrole, ' ')][1]/node()"/>
              </xsl:when>
              <xsl:when test="$titles[not(@role)]">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$titles[not(@role)][1]/node()"/>
              </xsl:when>
              <xsl:when test="$info/mal:title[not(@type)]">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$info/mal:title[not(@type)][1]/node()"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$target/mal:title[1]/node()"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="$href">
                <xsl:value-of select="$href"/>
              </xsl:when>
              <xsl:when test="$xref">
                <xsl:value-of select="$xref"/>
              </xsl:when>
              <xsl:when test="$action">
                <xsl:value-of select="$action"/>
              </xsl:when>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.content.custom
Output the content for a custom `link` element.
:Stub: true
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate title.
$info: An `info` element that overrides the info found in a target node.

This template is called by {mal.link.content} to create content for custom
links. Use this template to support the `action` attribute or extended `xref`
attributes containing slash or colon characters.
-->
<xsl:template name="mal.link.content.custom">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
 <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
</xsl:template>


<!--%%==========================================================================
mal.link.content.mode
Output the content for a link from the contents of a `title` element.
@revision[version=3.0 date=2010-01-02 status=final]

This mode is applied to the contents of a `title` element by {mal.link.content}.
By default, it returns the string value of its input.  Stylesheets that use
{mal.link.content} should map this mode to inline processing.
-->
<xsl:template mode="mal.link.content.mode" match="* | text()">
  <xsl:value-of select="."/>
</xsl:template>


<!--**==========================================================================
mal.link.desc
Output the desc content for a `link` element.
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate desc.
$info: An `info` element that overrides the info found in a target node.

This template outputs the secondary desc text content for a link. If $xref
points to a valid page or section, the desc from that page or section will be
used. The {mal.link.content.mode} mode is applied to the contents of that
desc. Stylesheets using this template should map that mode to inline processing.

For inline links, $node should be the `link` element. For links from a
`links` element, $node should be that `links` element, or the containing
element when the `links` element is implicit.

This template first calls {mal.link.desc.custom} with the same arguments.
If that template returns a non-empty result, it is used as the return value,
overriding any other behavior of this template.

Normally, this template automatically looks up information from a targret node
according to the $xref parameter. However, if the $info parameter is given,
information in that node set is used instead. This is useful for external info
links, where the target information is provided as child elements to the `link`
element.
-->
<xsl:template name="mal.link.desc">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
  <xsl:variable name="custom">
    <xsl:call-template name="mal.link.desc.custom">
      <xsl:with-param name="node" select="$node"/>
      <xsl:with-param name="action" select="$action"/>
      <xsl:with-param name="xref" select="$xref"/>
      <xsl:with-param name="href" select="$href"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="info" select="$info"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="exsl:node-set($custom)/node()">
      <xsl:copy-of select="$custom"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="linkid">
        <xsl:call-template name="mal.link.xref.linkid">
          <xsl:with-param name="node" select="$node"/>
          <xsl:with-param name="xref" select="$xref"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:for-each select="$mal.cache">
        <xsl:variable name="target" select="key('mal.cache.key', $linkid)"/>
        <xsl:choose>
          <xsl:when test="$target or $info">
            <xsl:variable name="infos" select="$target/mal:info | $info"/>
            <xsl:variable name="typedescs" select="$infos/mal:desc[@type = 'link']"/>
            <xsl:variable name="notypedescs" select="$infos/mal:desc[not(@type)]"/>
            <xsl:variable name="typerole">
              <xsl:for-each select="str:split($role)">
                <xsl:variable name="thisrole" select="string(.)"/>
                <xsl:if test="$typedescs[@role=$thisrole]">
                  <xsl:value-of select="concat($thisrole, ' ')"/>
                </xsl:if>
              </xsl:for-each>
            </xsl:variable>
            <xsl:variable name="notyperole">
              <xsl:for-each select="str:split($role)">
                <xsl:variable name="thisrole" select="string(.)"/>
                <xsl:if test="$notypedescs[@role=$thisrole]">
                  <xsl:value-of select="concat($thisrole, ' ')"/>
                </xsl:if>
              </xsl:for-each>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="$typerole != ''">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$typedescs[@role = substring-before($typerole, ' ')][1]/node()"/>
              </xsl:when>
              <xsl:when test="$notyperole != ''">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$notypedescs[@role = substring-before($notyperole, ' ')][1]/node()"/>
              </xsl:when>
              <xsl:when test="$typedescs[not(@role)]">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$typedescs[not(@role)][1]/node()"/>
              </xsl:when>
              <xsl:when test="$notypedescs[not(@role)]">
                <xsl:apply-templates mode="mal.link.content.mode"
                                     select="$notypedescs[not(@role)][1]/node()"/>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.desc.custom
Output the desc content for a custom `link` element.
:Stub: true
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate title.
$info: An `info` element that overrides the info found in a target node.

This template is called by {mal.link.desc} to create content for custom links.
Use this template to support the `action` attribute or extended `xref`
attributes containing slash or colon characters.
-->
<xsl:template name="mal.link.desc.custom">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
</xsl:template>


<!--**==========================================================================
mal.link.tooltip
Output a tooltip for a `link` element.
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate title.
$info: An `info` element that overrides the info found in a target node.

This template outputs a text-only tooltip for a link. If $xref points to a
valid page or section, the text title from that page or section will be used.
If the target does not specify a text title, the primary title is used.

For inline links, $node should be the `link` element. For links from a
`links` element, $node should be that `links` element, or the containing
element when the `links` element is implicit.

This template first calls {mal.link.tooltip.custom} with the same arguments.
If that template returns a non-empty string, it is used as the return value,
overriding any other behavior of this template.

If only $href is provided, that URL is used as the tooltip. If a target
page or section cannot be found, $xref is used as the text content. Special
tooltips may be provided for certain URI schemes.

Normally, this template automatically looks up information from a targret node
according to the $xref parameter. However, if the $info parameter is given,
information in that node set is used instead. This is useful for external info
links, where the target information is provided as child elements to the `link`
element.
-->
<xsl:template name="mal.link.tooltip">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
  <xsl:variable name="custom">
    <xsl:call-template name="mal.link.tooltip.custom">
      <xsl:with-param name="node" select="$node"/>
      <xsl:with-param name="action" select="$action"/>
      <xsl:with-param name="xref" select="$xref"/>
      <xsl:with-param name="href" select="$href"/>
      <xsl:with-param name="role" select="$role"/>
      <xsl:with-param name="info" select="$info"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$custom != ''">
      <xsl:value-of select="$custom"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="linkid">
        <xsl:call-template name="mal.link.xref.linkid">
          <xsl:with-param name="node" select="$node"/>
          <xsl:with-param name="xref" select="$xref"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:for-each select="$mal.cache">
        <xsl:variable name="target" select="key('mal.cache.key', $linkid)"/>
        <xsl:variable name="infos" select="$target/mal:info | $info"/>
        <xsl:choose>
          <xsl:when test="$infos/mal:title[@type = 'text']">
            <xsl:value-of select="normalize-space($infos/mal:title[@type = 'text'][1])"/>
          </xsl:when>
          <xsl:when test="$info/mal:title[not(@type)]">
            <xsl:value-of select="normalize-space($info/mal:title[not(@type)])"/>
          </xsl:when>
          <xsl:when test="$target/mal:title">
            <xsl:value-of select="normalize-space($target/mal:title[1])"/>
          </xsl:when>
          <xsl:when test="starts-with($href, 'mailto:')">
            <xsl:call-template name="l10n.gettext">
              <xsl:with-param name="msgid" select="'email.tooltip'"/>
              <xsl:with-param name="string">
                <xsl:call-template name="utils.email_address">
                  <xsl:with-param name="url" select="$href"/>
                </xsl:call-template>
              </xsl:with-param>
              <xsl:with-param name="format" select="true()"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="$href">
            <xsl:value-of select="$href"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$xref"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.tooltip.custom
Output a tooltip for a custom `link` element.
:Stub: true
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.
$role: A space-separated list of link roles, used to select the appropriate title.
$info: An `info` element that overrides the info found in a target node.

This template is called by {mal.link.tooltip} to create tooltips for custom
links. Use this template to support the `action` attribute or extended `xref`
attributes containing slash or colon characters.
-->
<xsl:template name="mal.link.tooltip.custom">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:param name="role" select="''"/>
  <xsl:param name="info" select="/false"/>
</xsl:template>


<!--**==========================================================================
mal.link.target
Output the target URL for a `link` or other linking element.
@revision[version=3.28 date=2017-08-11 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.

This template outputs a URL for a `link` element or another element using
linking attributes.  If $xref points to a valid page or section, it uses
a file name based on the ID of the target page plus {mal.link.extension}.
Otherwise, the link will point to $href.

For inline links, $node should be the `link` element. For links from a
`links` element, $node should be that `links` element, or the containing
element when the `links` element is implicit.

This template first calls {mal.link.target.custom} with the same arguments.
If that template returns a non-empty string, it is used as the return value,
overriding any other behavior of this template.

If $xref contains a `/` or `:` character, this template calls
{mal.link.target.extended}, which by default just uses $href instead.
Override that template to provide extended xref behavior.
-->
<xsl:template name="mal.link.target">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:variable name="custom">
    <xsl:call-template name="mal.link.target.custom">
      <xsl:with-param name="node" select="$node"/>
      <xsl:with-param name="action" select="$action"/>
      <xsl:with-param name="xref" select="$xref"/>
      <xsl:with-param name="href" select="$href"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$custom != ''">
      <xsl:value-of select="$custom"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="_mal.link.target.default">
        <xsl:with-param name="node" select="$node"/>
        <xsl:with-param name="action" select="$action"/>
        <xsl:with-param name="xref" select="$xref"/>
        <xsl:with-param name="href" select="$href"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--#* _mal.link.target.default -->
<!--
Note sure yet if I want to make this public API. If so, we should
do it to all the mal.link.* templates. This allows you, if you
really want, to override mal.link.target completely, but still
fallback to the built-in behavior.
-->
<xsl:template name="_mal.link.target.default">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:choose>
    <xsl:when test="string($xref) = ''">
      <xsl:value-of select="$href"/>
    </xsl:when>
    <xsl:when test="contains($xref, '/') or contains($xref, ':')">
      <xsl:call-template name="mal.link.target.extended">
        <xsl:with-param name="node" select="$node"/>
        <xsl:with-param name="action" select="$action"/>
        <xsl:with-param name="xref" select="$xref"/>
        <xsl:with-param name="href" select="$href"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains($xref, '#')">
      <xsl:variable name="pageid" select="substring-before($xref, '#')"/>
      <xsl:variable name="sectionid" select="substring-after($xref, '#')"/>
      <xsl:if test="$pageid != ''">
        <xsl:value-of select="concat($mal.link.prefix, $pageid, $mal.link.extension)"/>
      </xsl:if>
      <xsl:value-of select="concat('#', $sectionid)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="concat($mal.link.prefix, $xref, $mal.link.extension)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.target.extended
Output the target URL for an element with an extended `xref` attribute.
:Stub: true
@revision[version=3.28 date=2017-08-11 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.

This template is called by {mal.link.target} to create URLs for links with
a `/` or `:` in the `xref` attribute. By default, it just outputs the
value of $href. Override this template to provide behavior for extended
`xref` attributes.
-->
<xsl:template name="mal.link.target.extended">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
  <xsl:value-of select="$href"/>
</xsl:template>


<!--**==========================================================================
mal.link.target.custom
Output the target URL for an element with `action` or extended `xref` attributes.
:Stub: true
@revision[version=3.4 date=2012-01-17 status=final]

[xsl:params]
$node: The `link` or other element creating the link.
$action: The `action` attribute of $node.
$xref: The `xref` attribute of $node.
$href: The `href` attribute of $node.

This template is called by {mal.link.target} to create URLs for custom links.
Use this template to support the `action` attribute or extended `xref`
attributes containing slash or colon characters.
-->
<xsl:template name="mal.link.target.custom">
  <xsl:param name="node" select="."/>
  <xsl:param name="action" select="$node/@action"/>
  <xsl:param name="xref" select="$node/@xref"/>
  <xsl:param name="href" select="$node/@href"/>
</xsl:template>


<!--**==========================================================================
mal.link.guidelinks
Output the guide links for a page or section.
@revision[version=3.18 date=2015-06-07 status=final]

[xsl:params]
$node: The `page` or `section` element to generate links for.
$role: A space-separated list of link roles, used to select the appropriate title, default `"guide"`.

This template outputs all the guide links for a page or section, whether
declared as guide links in the page or section or as topic links from another
guide page.  It outputs each of the links as a `link` element within the
Mallard namespace.  Each `link` element has an `xref` attribute pointing
to the target page or section. Or, in the case of external links, the `link`
element has an `href` attribute pointing to the external resource.

Each `link` element contains a `title` with `type="sort"` providing the
sort title of the target page or section. The $role attribute is used to
select a link title to sort on when a sort title is not present. The results
are not sorted when returned from this template. Use `xsl:sort` on the sort
titles to sort the results.

When a link comes from a guide link on $node that has an `href`
attribute but not an `xref` attribute, it is taken to be an external
link. In that case, the output link has an `href` attribute instead of
an `xref` attribute, and it has an `info` child element. This element
has a copy of all the child elements of the source `link` element.

The output is a result tree fragment.  To use these results, call
`exsl:node-set` on them.
-->
<xsl:template name="mal.link.guidelinks">
  <xsl:param name="node" select="."/>
  <xsl:param name="role" select="'guide'"/>
  <xsl:variable name="linkid">
    <xsl:call-template name="mal.link.linkid">
      <xsl:with-param name="node" select="$node"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="links">
    <xsl:for-each select="$node/mal:info/mal:link[@type = 'guide']">
      <xsl:variable name="link" select="."/>
      <xsl:choose>
        <xsl:when test="$link[@xref]">
          <xsl:variable name="linklinkid">
            <xsl:call-template name="mal.link.xref.linkid"/>
          </xsl:variable>
          <xsl:for-each select="$mal.cache">
            <xsl:variable name="linklinknode" select="key('mal.cache.key', $linklinkid)"/>
            <xsl:if test="count($linklinknode) > 0">
              <mal:link xref="{$linklinkid}">
                <mal:title type="sort">
                  <xsl:call-template name="mal.link.sorttitle">
                    <xsl:with-param name="node" select="$linklinknode"/>
                    <xsl:with-param name="role" select="$role"/>
                  </xsl:call-template>
                </mal:title>
              </mal:link>
            </xsl:if>
          </xsl:for-each>
        </xsl:when>
        <xsl:when test="$link[@href]">
          <mal:link href="{$link/@href}">
            <mal:title type="sort">
              <xsl:choose>
                <xsl:when test="mal:title[@type = 'sort']">
                  <xsl:value-of select="normalize-space(mal:title[@type = 'sort'][1])"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="normalize-space(mal:title[not(@type)][1])"/>
                </xsl:otherwise>
              </xsl:choose>
            </mal:title>
            <mal:info>
              <xsl:copy-of select="$link/*"/>
            </mal:info>
          </mal:link>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  <xsl:copy-of select="$links"/>
  <xsl:variable name="linknodes" select="exsl:node-set($links)/*"/>
  <xsl:for-each select="$mal.cache">
    <xsl:for-each select="key('mal.cache.link.key', concat('topic:', $linkid))/../..">
      <xsl:variable name="linklinkid">
        <xsl:call-template name="mal.link.linkid"/>
      </xsl:variable>
      <xsl:if test="$linklinkid != '' and not($linknodes[@xref = $linklinkid])">
        <mal:link xref="{$linklinkid}">
          <mal:title type="sort">
            <xsl:call-template name="mal.link.sorttitle">
              <xsl:with-param name="node" select="."/>
              <xsl:with-param name="role" select="$role"/>
            </xsl:call-template>
          </mal:title>
        </mal:link>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>


<!--**==========================================================================
mal.link.topiclinks
Output the topic links for a page or section.
@revision[version=3.18 date=2015-06-06 status=final]

[xsl:params]
$node: The `page` or `section` element to generate links for.
$groups: The list of all valid link groups for $node.
$role: A space-separated list of link roles, used to select the appropriate title, default `"topic"`.

This template outputs all the topic links for a guide page or section, whether
declared as topic links in the page or section or as guide links from another
page or section.  It outputs each of the links as a `link` element within the
Mallard namespace.  Each `link` element has an `xref` attribute pointing
to the target page or section. Or, in the case of external links, the `link`
element has an `href` attribute pointing to the external resource.

Each `link` element contains a `title` with `type="sort"` providing the
sort title of the target page or section. The $role attribute is used to
select a link title to sort on when a sort title is not present. The results
are not sorted when returned from this template. Use `xsl:sort` on the sort
titles to sort the results.

Each `link` element also contains a `group` attribute.  The `group`
attribute is normalized.  It will either point to a link group declared
in $groups, or it will be set to `#default`.  Each `link` element also
contains a `groupsort` attribute giving the numerical position of the
`group` attribute in the normalized group list for $node.

The $groups parameter can be calculated automatically from $node.

When a link comes from a topic link on $node that has an `href`
attribute but not an `xref` attribute, it is taken to be an external
link. In that case, the output link has an `href` attribute instead of
an `xref` attribute, and it has an `info` child element. This element
has a copy of all the child elements of the source `link` element.

The output is a result tree fragment.  To use these results, call
`exsl:node-set` on them.
-->
<xsl:template name="mal.link.topiclinks">
  <xsl:param name="node" select="."/>
  <xsl:param name="groups">
    <xsl:variable name="_groups">
      <xsl:choose>
        <xsl:when test="$node/mal:links[@type = 'topic']">
          <xsl:text> </xsl:text>
          <xsl:for-each select="$node/mal:links[@type = 'topic']">
            <xsl:choose>
              <xsl:when test="@groups">
                <xsl:value-of select="@groups"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>#default</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:text> </xsl:text>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(' ', $node/@groups, ' ')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:if test="not(contains($_groups, ' #first '))">
      <xsl:text>#first</xsl:text>
    </xsl:if>
    <xsl:value-of select="$_groups"/>
    <xsl:if test="not(contains($_groups, ' #default '))">
      <xsl:text>#default </xsl:text>
    </xsl:if>
    <xsl:if test="not(contains($_groups, ' #last '))">
      <xsl:text>#last </xsl:text>
    </xsl:if>
  </xsl:param>
  <xsl:param name="role" select="'topic'"/>
  <xsl:variable name="groupslist" select="str:split($groups)"/>
  <xsl:variable name="defaultpos">
    <xsl:for-each select="$groupslist">
      <xsl:if test="string(.) = '#default'">
        <xsl:value-of select="position()"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="linkid">
    <xsl:call-template name="mal.link.linkid">
      <xsl:with-param name="node" select="$node"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="links">
    <xsl:for-each select="$node/mal:info/mal:link[@type = 'topic']">
      <xsl:variable name="link" select="."/>
      <xsl:variable name="grouppos">
        <xsl:if test="$link/@group">
          <xsl:for-each select="$groupslist">
            <xsl:if test="string(.) = $link/@group">
              <xsl:value-of select="position()"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:variable>
      <xsl:variable name="groupsort">
        <xsl:value-of select="$grouppos"/>
        <xsl:if test="string($grouppos) = ''">
          <xsl:value-of select="$defaultpos"/>
        </xsl:if>
      </xsl:variable>
      <xsl:variable name="groupattrs">
        <xsl:attribute name="group">
          <xsl:value-of select="$groupslist[number($groupsort)]"/>
        </xsl:attribute>
        <xsl:attribute name="groupsort">
          <xsl:value-of select="$groupsort"/>
        </xsl:attribute>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$link[@xref]">
          <xsl:variable name="linklinkid">
            <xsl:call-template name="mal.link.xref.linkid"/>
          </xsl:variable>
          <xsl:for-each select="$mal.cache">
            <xsl:variable name="linklinknode"
                          select="key('mal.cache.key', $linklinkid)"/>
            <xsl:if test="count($linklinknode) > 0">
              <mal:link xref="{$linklinkid}">
                <xsl:attribute name="group">
                  <xsl:value-of select="$groupslist[number($groupsort)]"/>
                </xsl:attribute>
                <xsl:attribute name="groupsort">
                  <xsl:value-of select="$groupsort"/>
                </xsl:attribute>
                <mal:title type="sort">
                  <xsl:call-template name="mal.link.sorttitle">
                    <xsl:with-param name="node" select="$linklinknode"/>
                    <xsl:with-param name="role" select="$role"/>
                  </xsl:call-template>
                </mal:title>
              </mal:link>
            </xsl:if>
          </xsl:for-each>
        </xsl:when>
        <xsl:when test="$link[@href]">
          <mal:link href="{$link/@href}">
            <xsl:attribute name="group">
              <xsl:value-of select="$groupslist[number($groupsort)]"/>
            </xsl:attribute>
            <xsl:attribute name="groupsort">
              <xsl:value-of select="$groupsort"/>
            </xsl:attribute>
            <mal:title type="sort">
              <xsl:choose>
                <xsl:when test="mal:title[@type = 'sort']">
                  <xsl:value-of select="normalize-space(mal:title[@type = 'sort'][1])"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="normalize-space(mal:title[not(@type)][1])"/>
                </xsl:otherwise>
              </xsl:choose>
            </mal:title>
            <mal:info>
              <xsl:copy-of select="$link/*"/>
            </mal:info>
          </mal:link>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  <xsl:copy-of select="$links"/>
  <xsl:variable name="linknodes" select="exsl:node-set($links)/*"/>
  <xsl:for-each select="$mal.cache">
    <xsl:for-each select="key('mal.cache.link.key', concat('guide:', $linkid))">
      <xsl:variable name="source" select="../.."/>
      <xsl:variable name="linklinkid">
        <xsl:call-template name="mal.link.xref.linkid">
          <xsl:with-param name="node" select="$source"/>
          <xsl:with-param name="xref" select="$source/@id"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:if test="$linklinkid != '' and not($linknodes[@xref = $linklinkid])">
        <xsl:variable name="link" select="."/>
        <xsl:variable name="grouppos">
          <xsl:if test="$link/@group">
            <xsl:for-each select="$groupslist">
              <xsl:if test="string(.) = $link/@group">
                <xsl:value-of select="position()"/>
              </xsl:if>
            </xsl:for-each>
          </xsl:if>
        </xsl:variable>
        <xsl:variable name="groupsort">
          <xsl:value-of select="$grouppos"/>
          <xsl:if test="string($grouppos) = ''">
            <xsl:value-of select="$defaultpos"/>
          </xsl:if>
        </xsl:variable>
        <mal:link xref="{$linklinkid}">
          <xsl:attribute name="group">
            <xsl:value-of select="$groupslist[number($groupsort)]"/>
          </xsl:attribute>
          <xsl:attribute name="groupsort">
            <xsl:value-of select="$groupsort"/>
          </xsl:attribute>
          <mal:title type="sort">
            <xsl:call-template name="mal.link.sorttitle">
              <xsl:with-param name="node" select="$source"/>
              <xsl:with-param name="role" select="$role"/>
            </xsl:call-template>
          </mal:title>
        </mal:link>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>


<!--**==========================================================================
mal.link.seealsolinks
Output the see-also links for a page or section.
@revision[version=3.18 date=2015-06-07 status=final]

[xsl:params]
$node: The `page` or `section` element to generate links for.
$role: A space-separated list of link roles, used to select the appropriate title, default `"seealso"`.

This template outputs all the see-also links for a page or section, whether
declared in the page or section or in another page or section.  It outputs
each of the links as a `link` element within the Mallard namespace.  Each
`link` element has an `xref` attribute pointing to the target page or section.
Or, in the case of external links, the `link` element has an `href` attribute
pointing to the external resource.

Each `link` element contains a `title` with `type="sort"` providing the
sort title of the target page or section. The $role attribute is used to
select a link title to sort on when a sort title is not present. The results
are not sorted when returned from this template. Use `xsl:sort` on the sort
titles to sort the results.

When a link comes from a topic link on $node that has an `href`
attribute but not an `xref` attribute, it is taken to be an external
link. In that case, the output link has an `href` attribute instead of
an `xref` attribute, and it has an `info` child element. This element
has a copy of all the child elements of the source `link` element.

The output is a result tree fragment.  To use these results, call
`exsl:node-set` on them.
-->
<xsl:template name="mal.link.seealsolinks">
  <xsl:param name="node" select="."/>
  <xsl:param name="role" select="'seealso'"/>
  <xsl:variable name="linkid">
    <xsl:call-template name="mal.link.linkid">
      <xsl:with-param name="node" select="$node"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="links">
    <xsl:for-each select="$node/mal:info/mal:link[@type = 'seealso']">
      <xsl:variable name="link" select="."/>
      <xsl:choose>
        <xsl:when test="$link[@xref]">
          <xsl:variable name="linklinkid">
            <xsl:call-template name="mal.link.xref.linkid"/>
          </xsl:variable>
          <xsl:for-each select="$mal.cache">
            <xsl:variable name="linklinknode" select="key('mal.cache.key', $linklinkid)"/>
            <xsl:if test="count($linklinknode) > 0">
              <mal:link xref="{$linklinkid}">
                <mal:title type="sort">
                  <xsl:call-template name="mal.link.sorttitle">
                    <xsl:with-param name="node" select="$linklinknode"/>
                    <xsl:with-param name="role" select="$role"/>
                  </xsl:call-template>
                </mal:title>
              </mal:link>
            </xsl:if>
          </xsl:for-each>
        </xsl:when>
        <xsl:when test="$link[@href]">
          <mal:link href="{$link/@href}">
            <mal:title type="sort">
              <xsl:choose>
                <xsl:when test="mal:title[@type = 'sort']">
                  <xsl:value-of select="normalize-space(mal:title[@type = 'sort'][1])"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="normalize-space(mal:title[not(@type)][1])"/>
                </xsl:otherwise>
              </xsl:choose>
            </mal:title>
            <mal:info>
              <xsl:copy-of select="$link/*"/>
            </mal:info>
          </mal:link>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  <xsl:copy-of select="$links"/>
  <xsl:variable name="linknodes" select="exsl:node-set($links)/*"/>
  <xsl:for-each select="$mal.cache">
    <xsl:for-each select="key('mal.cache.link.key', concat('seealso:', $linkid))/../..">
      <xsl:variable name="linklinkid">
        <xsl:call-template name="mal.link.linkid"/>
      </xsl:variable>
      <xsl:if test="$linklinkid != '' and not($linknodes[@xref = $linklinkid])">
        <mal:link xref="{$linklinkid}">
          <mal:title type="sort">
            <xsl:call-template name="mal.link.sorttitle">
              <xsl:with-param name="node" select="."/>
              <xsl:with-param name="role" select="$role"/>
            </xsl:call-template>
          </mal:title>
        </mal:link>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>


<!--**==========================================================================
mal.link.linktrails
Output link trails for a page or section.
@revision[version=3.4 date=2012-01-18 status=final]

[xsl:params]
$node: The `page` or `section` element to generate links for.
$trail: The link trail leading to $node.
$root: The ID of the root page.

This template outputs lists of links, where each list is a path of topic links
that leads to $node.  Each link list is output as a `link` element in the
Mallard namespace with an `xref` attribute pointing to the target page or
section.  Each `link` has a `title` element with `type="sort"` providing
the sort title of the target page or section.

Each `link` element may also contain another `link` element providing the
next link in the trail.  Each of these links also contains a sort titles and
may also contain another link.

The results are not sorted when returned from this template.  Use `xsl:sort`
on the nested sort titles to sort the results.  The output is a result tree
fragment.  To use these results, call `exsl:node-set` on them.

This template calls itself recursively.  It finds the guide links for $node
using {mal.link.guidelinks}.  It then calls {mal.link.linktrails} on each
guide, wrapping $trail with a link to the guide as the new $trail parameter.

If there are no guide links for $node and $node is a `section` element,
this template calls itself on the containing page, wrapping $trails with a
link to that page.  This `link` element has the attribute `child="section"`
to indicate the link from it to its child is not a topic link.

Recursion stops when the ID of $node is $root.  Link trails are only
output if they reach $root, which is {mal.link.default_root} by default.
-->
<!--
FIXME:
* Terminate at $root
* Only output if we reach $root
* Cycle detection
-->
<xsl:template name="mal.link.linktrails">
  <xsl:param name="node" select="."/>
  <xsl:param name="trail" select="/false"/>
  <xsl:param name="root" select="$mal.link.default_root"/>
  <xsl:variable name="guidelinks">
    <xsl:call-template name="mal.link.guidelinks">
      <xsl:with-param name="node" select="$node"/>
      <xsl:with-param name="role" select="'trail'"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="guidenodes" select="exsl:node-set($guidelinks)/*"/>
  <xsl:choose>
    <xsl:when test="count($guidenodes) = 0">
      <xsl:choose>
        <xsl:when test="$node/self::mal:section">
          <xsl:variable name="page" select="$node/ancestor::mal:page"/>
          <xsl:call-template name="mal.link.linktrails">
            <xsl:with-param name="node" select="$page"/>
            <xsl:with-param name="trail">
              <mal:link xref="{$page/@id}" child="section">
                <xsl:copy-of select="$page/mal:info/mal:title[@type='sort']"/>
                <xsl:copy-of select="$trail"/>
              </mal:link>
            </xsl:with-param>
            <xsl:with-param name="root" select="$root"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="$trail"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$guidenodes[@xref = $root]">
      <mal:link xref="{$root}">
        <xsl:copy-of select="$guidenodes[@xref = $root][1]/mal:title"/>
        <xsl:copy-of select="$trail"/>
      </mal:link>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$guidenodes">
        <xsl:variable name="self" select="."/>
        <xsl:choose>
          <xsl:when test="$self/@xref = $root">
            <mal:link xref="{$self/@xref}">
              <xsl:copy-of select="$self/mal:title"/>
              <xsl:copy-of select="$trail"/>
            </mal:link>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="$mal.cache">
              <xsl:call-template name="mal.link.linktrails">
                <xsl:with-param name="node" select="key('mal.cache.key', $self/@xref)"/>
                <xsl:with-param name="trail">
                  <mal:link xref="{$self/@xref}">
                    <xsl:copy-of select="$self/mal:title"/>
                    <xsl:copy-of select="$trail"/>
                  </mal:link>
                </xsl:with-param>
                <xsl:with-param name="root" select="$root"/>
              </xsl:call-template>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
mal.link.sorttitle
Output the sort title for a page or section.
@revision[version=3.10 date=2013-07-30 status=final]

[xsl:params]
$node: The `page` or `section` element to output a sort title for.
$role: A space-separated list of link roles, used to select the appropriate title.

This template returns a sort title for a page or section as a normalized string.
If $node defines a sort title in its `info` element, the value of that title
is always used first. Otherwise, if $role is defined and $node has a link
title with a matching role, that title is used. Otherwise, if $node has a link
title with no role, that title is used. Otherwise, the primary title is used.
-->
<xsl:template name="mal.link.sorttitle">
  <xsl:param name="node" select="."/>
  <xsl:param name="role" select="''"/>
  <xsl:choose>
    <xsl:when test="$node/mal:info/mal:title[@type = 'sort']">
      <xsl:value-of select="normalize-space($node/mal:info/mal:title[@type = 'sort'][1])"/>
    </xsl:when>
    <xsl:when test="$role != '' and $node/mal:info/mal:title[@type = 'link'][@role = $role]">
      <xsl:value-of select="normalize-space($node/mal:info/mal:title[@type = 'link'][@role = $role][1])"/>
    </xsl:when>
    <xsl:when test="$node/mal:info/mal:title[@type = 'link'][not(@role)]">
      <xsl:value-of select="normalize-space($node/mal:info/mal:title[@type = 'link'][not(@role)][1])"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="normalize-space($node/mal:title[1])"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>