summaryrefslogtreecommitdiff
path: root/sandbox/xml2rst/xml2rstlib/xml2rst-nopy.xsl
blob: a4d15762889717d460d5435218cac7a85ebf95d8 (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
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY CR "&#x0A;">
<!-- "xml:space='preserve'" is needed for use with libxslt -->
<!-- "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" is needed for
     use with xsltproc -->
<!-- Used to create a blank line -->
<!ENTITY tCR "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xml:space='preserve'>&CR;</xsl:text>">
<!-- Used when the line before must be ended -->
<!ENTITY tEOL "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xml:space='preserve'>&CR;</xsl:text>">
<!ENTITY tSP "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xml:space='preserve'> </xsl:text>">
]>

<!--
     Copyright (C) 2005, 2006 Stefan Merten, David Priest
     Copyright (C) 2009, 2010, 2011 Stefan Merten

     xml2rst.xsl is free software; you can redistribute it and/or modify
     it under the terms of the GNU 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
     02111-1307, USA.
-->

<!-- ********************************************************************** -->
<!-- ********************************************************************** -->

<!-- These elements in the DTD need support:

     - ``colspec`` has attribute "stub %yesorno; #IMPLIED"

     - ``document`` has attribute "title CDATA #IMPLIED"

       Probably rendered by the `.. title::` directive

     -->

<!--
Set namespace extensions. These are used as [shortname]:[tag] throughout the 
XSL-FO files.
xsl: eXtensible Stylesheet Language
u: user extensions (indicates utility 'call-template' routines defined in 
these XSL files)
data: Data elements used by the stylesheet
-->
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:u="u"
    xmlns:data="a"
    exclude-result-prefixes="data"
    xmlns:str="http://exslt.org/strings"
    xmlns:dyn="http://exslt.org/dynamic"
    xmlns:math="http://exslt.org/math"
    extension-element-prefixes="str dyn math">

  <!-- xmlns:regexp="http://exslt.org/regular-expressions" not supported :-( -->

  <xsl:output
      method="text"
      omit-xml-declaration="yes"
      indent="no"/>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!-- Parameter to configure title markup; see manual page for description -->
  <xsl:param
      name="adornment"
      select="'o=o-u=u-u~u`u,u.'"/>

  <!-- Parameter for folding long lines; see manual page for description -->
  <xsl:param
      name="fold"
      select="0"/>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <xsl:variable
      name="apos"
      select='"&apos;"'/>

  <xsl:variable
      name="structural_elements"
      select="'*document*section*topic*sidebar*'"/>

  <xsl:variable
      name="structural_subelements"
      select="'*title*subtitle*docinfo*decoration*transition*'"/>

  <xsl:variable
      name="bibliographic_elements"
      select="'*address*author*authors*contact*copyright*date*field*organization*revision*status*version*'"/>

  <xsl:variable
      name="decorative_elements"
      select="'*footer*header*'"/>

  <xsl:variable
      name="simple_body_elements_no_substitution"
      select="'*comment*doctest_block*image*literal_block*paragraph*pending*raw*rubric*target*'"/>

  <xsl:variable
      name="folding_elements"
      select="concat('*comment*paragraph*rubric*attribution*caption*line*', substring-after($bibliographic_elements, '*'))"/>

  <xsl:variable
      name="simple_body_elements"
      select="concat($simple_body_elements_no_substitution, 'substitution_definition*')"/>

  <xsl:variable
      name="compound_body_elements"
      select="'*admonition*attention*block_quote*bullet_list*caution*citation*compound*danger*definition_list*enumerated_list*error*field_list*figure*footnote*hint*important*line_block*note*option_list*system_message*table*tip*warning*container*'"/>

  <xsl:variable
      name="body_elements"
      select="concat($simple_body_elements, substring-after($compound_body_elements, '*'))"/>

  <xsl:variable
      name="admonitions"
      select="'*admonition*attention*caution*danger*error*hint*important*note*tip*warning*'"/>

  <xsl:variable
      name="simple_body_subelements"
      select="'*attribution*caption*classifier*colspec*field_name*label*line*option_argument*option_string*term*'"/>

  <xsl:variable
      name="compound_body_subelements"
      select="'*definition*definition_list_item*description*entry*field*field_body*legend*list_item*option*option_group*option_list_item*row*tbody*tgroup*thead*'"/>

  <xsl:variable
      name="inline_elements"
      select="'*abbreviation*acronym*citation_reference*emphasis*footnote_reference*generated*image*inline*literal*problematic*reference*strong*subscript*substitution_reference*superscript*target*title_reference*raw*'"/>

  <xsl:variable
      name="inline_containers"
      select="concat($simple_body_elements_no_substitution, substring-after($inline_elements, '*'))"/>

  <xsl:variable
      name="directives"
      select="'*admonition*attention*caution*comment*danger*error*footnote*hint*important*note*tip*warning*image*figure*topic*sidebar*rubric*meta*raw*citation*compound*substitution_definition*container*'"/>

  <xsl:variable
      name="titled_elements"
      select="'*sidebar*topic*admonition*'"/>

  <xsl:variable
      name="blank_after"
      select="concat($structural_elements, substring-after($structural_subelements, '*'), substring-after($body_elements, '*'))"/>

  <xsl:variable
      name="adornment_characters"
      select="concat($apos, '!&quot;#$%&amp;()*+,-./:;&lt;=&gt;?@[\]^_`{|}~')"/>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!--
  Content Model: ((title, subtitle?)?, docinfo?, decoration?,
  %structure.model;)

  Attributes:    The document element contains only the common attributes: ids,
  names, dupnames, source, and classes.

  Depending on the source of the data and the stage of processing, the 
  "document" may not initially contain a "title". A document title is not 
  directly representable in reStructuredText. Instead, a lone top-level section
  may have its title promoted to become the document title, and similarly for a
  lone second-level (sub)section's title to become the document subtitle. The 
  "docinfo" may be transformed from an initial field_list, and "decoration" is 
  usually constructed programmatically.
  -->
  <!-- == structural_element -->
  <xsl:template
      match="document">
    <xsl:if
	test="//generated[@classes = 'sectnum']">
      <xsl:text>.. section-numbering::</xsl:text>
      &tEOL;
      &tCR;
    </xsl:if>
    <xsl:call-template
	name="u:outputClass">
      <xsl:with-param
	  name="blankAfter"
	  select="true()"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (title, %structure.model;)
  Attributes:    The section element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == structural_element -->
  <xsl:template
      match="section">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:blank"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- == structural_element -->
  <xsl:template
      match="section[@classes = 'system-messages']"/>
  <!-- Ignore system messages completely -->
  <!-- This should be really in `generated' -->

  <!-- ******************************************************************** -->

  <!--
  Content Model: (title, subtitle?, (%body.elements;)+)
  Attributes:    The sidebar element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == structural_element == directive -->
  <xsl:template
      match="sidebar">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. sidebar:: </xsl:text>
    <xsl:value-of
	select="title"/>
    &tEOL;
    <xsl:if
	test="subtitle">
      <xsl:call-template
	  name="u:param">
	<xsl:with-param
	    name="name"
	    select="'subtitle'"/>
	<xsl:with-param
	    name="value"
	    select="subtitle"/>
	<xsl:with-param
	    name="ancestors"
	    select="ancestor-or-self::*"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:call-template
	name="u:params"/>
    <!-- Always blank line after parameter block -->
    &tCR;
    <xsl:apply-templates
	select="*[not(self::title) and not(self::subtitle)]"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (title?, (%body.elements;)+)
  Attributes:    The topic element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == structural_element == directive -->
  <xsl:template
      match="topic">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. topic:: </xsl:text>
    <xsl:value-of
	select="title"/>
    &tEOL;
    <xsl:call-template
	name="u:params"/>
    <xsl:apply-templates
	select="*[not(self::title)]"/>
  </xsl:template>

  <!-- == structural_element == directive -->
  <xsl:template
      match="topic[starts-with(@classes, 'contents')]">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. contents:: </xsl:text>
    <xsl:apply-templates
	select="title"/>
    &tEOL;
    <xsl:call-template
	name="u:params">
      <xsl:with-param
	  name="params"
	  select="@*[name() != 'ids' and name() != 'names' and name() != 'classes']"/>
    </xsl:call-template>
    <xsl:variable
	name="isLocal"
	select="substring-before(@classes, ' local')"/>
    <xsl:variable
	name="realClassesLocal"
	select="normalize-space(substring-after(@classes, 'contents'))"/>
    <xsl:variable
	name="realClassesNode">
      <xsl:choose>
	<xsl:when
	    test="$isLocal">
	  <xsl:value-of
	      select="normalize-space(substring-before($realClassesLocal, 'local'))"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="$realClassesLocal"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable
	name="realClasses"
	select="string($realClassesNode)"/>
    <xsl:if
	test="$isLocal">
      <xsl:call-template
	  name="u:param">
	<xsl:with-param
	    name="name"
	    select="'local'"/>
	<xsl:with-param
	    name="value"
	    select="''"/>
	<xsl:with-param
	    name="ancestors"
	    select="ancestor-or-self::*"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if
	test="$realClasses">
      <xsl:call-template
	  name="u:param">
	<xsl:with-param
	    name="name"
	    select="'class'"/>
	<xsl:with-param
	    name="value"
	    select="$realClasses"/>
        <xsl:with-param
	    name="ancestors"
	    select="ancestor-or-self::*"/>
      </xsl:call-template>
    </xsl:if>
    <!-- Autogenerated content is discarded -->
    &tCR;
  </xsl:template>

  <!-- == structural_element == directive -->
  <xsl:template
      match="topic[@classes='dedication' or @classes='abstract']">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>:</xsl:text>
    <xsl:apply-templates
	select="title"/>
    <xsl:text>: </xsl:text>
    &tEOL;
    <xsl:apply-templates
	select="*[not(self::title)]"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (title, (%body.elements;)+)
  Attributes:    The admonition element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element == directive -->
  <xsl:template
      match="admonition">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. admonition:: </xsl:text>
    <xsl:apply-templates
	select="title"/>
    &tEOL;
    <xsl:call-template
	name="u:params">
      <xsl:with-param
	  name="params"
	  select="@*[name() != 'classes' or not(starts-with(., 'admonition-'))]"/>
    </xsl:call-template>
    <xsl:call-template
	name="u:indent"/>
    <xsl:apply-templates
	select="*[not(self::title)]"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)+
  Attributes:    The note element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element == directive -->
  <xsl:template
      match="attention | caution | danger | error | hint | important | note | tip | warning">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. </xsl:text>
    <xsl:value-of
	select="name()"/>
    <xsl:text>:: </xsl:text>
    <xsl:call-template
	name="u:params">
      <xsl:with-param
	  name="params"
	  select="@*[name() != 'classes']"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (header?, footer?)
  Attributes:    The decoration element contains only the common attributes:
  ids, names, dupnames, source, and classes.

  Although the content model doesn't specifically require contents, no empty 
  decoration elements are ever created.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="//document/decoration">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- TODO To be rendered as `.. header::` directive -->
  <!-- == decorative_element -->
  <xsl:template
      match="//document/decoration/header">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- TODO To be rendered as `.. footer::` directive -->
  <!-- == decorative_element -->
  <xsl:template
      match="//document/decoration/footer">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%bibliographic.elements;)+
  Attributes:    The docinfo element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="docinfo">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:blank"/>
    <xsl:apply-templates/>
    <xsl:call-template
	name="u:blank"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: ((author, organization?, address?, contact?)+)
  Attributes:    The authors element contains only the common attributes: ids,
  names, dupnames, source, and classes.

  In reStructuredText, multiple author's names are separated with semicolons 
  (";") or commas (","); semicolons take precedence. There is currently no way 
  to represent the author's organization, address, or contact in a 
  reStructuredText "Authors" field.
  -->
  <!-- == bibliographic_element == folding_element -->
  <xsl:template
      match="docinfo/authors">
    <xsl:call-template
	name="u:outputFolding">
      <xsl:with-param
	  name="prefix">
	<xsl:text>:</xsl:text>
	<xsl:value-of
	    select="name()"/>
	<xsl:text>: </xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!--
  Content Model: %text.model;
  Attributes:    All docinfo elements contains the common attributes (ids,
  names, dupnames, source, and classes)
	      Some docinfo elements also have xml:space.
  -->
  <xsl:template
      match="docinfo/authors/*">
    <xsl:apply-templates/>
    <!-- no semicolon after final author -->
    <xsl:if
	test="generate-id(current()) != generate-id(../*[last()])">
      <xsl:text>; </xsl:text>
    </xsl:if>
  </xsl:template>

  <!--
  Content Model: (field_name, field_body)
  Attributes:    The field element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == bibliographic_element -->
  <xsl:template
      match="docinfo/field">
    <!-- contents handled by ordinary field lists -->
    <xsl:apply-templates/>
    <!-- Supply an EOL because following elements do not recognize this -->
    &tEOL;
  </xsl:template>

  <!--
  Content Model: %text.model;
  Attributes:    All docinfo elements contains the common attributes (ids,
  names, dupnames, source, and classes)
	      Some docinfo elements also have xml:space.
  -->
  <!-- == bibliographic_element == folding_element -->
  <xsl:template
      match="docinfo/*[name()!='authors' and name()!='field']">
    <xsl:call-template
	name="u:outputFolding">
      <xsl:with-param
	  name="prefix">
	<xsl:text>:</xsl:text>
	<xsl:value-of
	    select="name()"/>
	<xsl:text>: </xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: EMPTY
  Attributes:    The transition element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="transition">
    <xsl:call-template
	name="u:outputClass"/>
    &tCR; <!-- req: blank line before -->
    <xsl:text>-----</xsl:text>
    &tEOL;
    <!-- Add a required blank line after unless class follows immediately -->
    <xsl:if
	test="not(following-sibling::*[1]/@classes)">
      &tCR;
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!--
  IFF there is a /document/title element, it is the publication's title.  All 
  other titles will appear within sections.

  Content Model: %text.model;
  Attributes:    The title element contains the common attributes (ids, names, 
  dupnames, source, and classes), plus refid and auto.
      refid is used as a backlink to a table of contents entry.
      auto is used to indicate (with value "1") that the title has been
  numbered automatically.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="//document/title">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:variable
	name="textWS">
      <!-- Catch the title text as it is rendered so its length can be
           determined -->    
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:variable
	name="text"
	select="normalize-space($textWS)"/>
    <xsl:variable
	name="length"
	select="string-length($text)"/>
    <xsl:call-template
	name="u:overline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="1"/>
    </xsl:call-template>
    <xsl:value-of
	select="$text"/>
    &tEOL;    
    <xsl:call-template
	name="u:underline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="1"/>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Title Underlines are defined by their position within the tree.

  Content Model: %text.model;
  Attributes:    The title element contains the common attributes (ids, names, 
  dupnames, source, and classes), plus refid and auto.
      refid is used as a backlink to a table of contents entry.
      auto is used to indicate (with value "1") that the title has been
  numbered automatically.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="section/title">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:variable
	name="textWS">
      <!-- catch the title text as it is rendered -->    
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:variable
	name="text"
	select="normalize-space($textWS)"/>
    <xsl:variable
	name="length"
	select="string-length($text)"/>
    <xsl:variable
	name="depth"
	select="count(ancestor::section)"/>
    <xsl:call-template
	name="u:overline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="$depth + 2"/>
    </xsl:call-template>
    <xsl:value-of
	select="$text"/>
    &tEOL;    
    <xsl:call-template
	name="u:underline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="$depth + 2"/>
    </xsl:call-template>
    <!-- Add a blank line after unless structure follows immediately -->
    <xsl:if
	test="not(contains(concat($structural_elements, $compound_body_elements), concat('*', name(following-sibling::*[1]), '*')) or following-sibling::*[1]/@classes)">
      &tCR;
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The title element contains the common attributes (ids, names, 
  dupnames, source, and classes), plus refid and auto.
      refid is used as a backlink to a table of contents entry.
      auto is used to indicate (with value "1") that the title has been
  numbered automatically.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="title">
    <xsl:call-template
	name="u:outputClass">
      <xsl:with-param
	  name="alreadyBlanked"
	  select="true()"/>
    </xsl:call-template>
    <!-- blank line provided by parent -->
    <xsl:apply-templates/>
    <!-- no EOL: provided by parent -->
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  IFF there is a /document/title element, it is the publication's title.  All 
  other titles will appear within sections.

  Content Model: %text.model;
  Attributes:    The subtitle element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="//document/subtitle">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:variable
	name="textWS">
      <!-- Catch the title text as it is rendered -->    
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:variable
	name="text"
	select="normalize-space($textWS)"/>
    <xsl:variable
	name="length"
	select="string-length($text)"/>

    <!-- always a blank line above -->
    &tCR;
    <xsl:call-template
	name="u:overline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="2"/>
    </xsl:call-template>
    <xsl:value-of
	select="$text"/>
    &tEOL;    
    <xsl:call-template
	name="u:underline">
      <xsl:with-param
	  name="length"
	  select="$length"/>
      <xsl:with-param
	  name="depth"
	  select="2"/>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The subtitle element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == structural_subelement -->
  <xsl:template
      match="sidebar/subtitle">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:indent"/>
    <xsl:apply-templates/>
    &tEOL;
    &tCR;
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The comment element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == simple_body_element == folding_element == directive -->
  <xsl:template
      match="comment">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:call-template
	name="u:outputFolding">
      <xsl:with-param
	  name="prefix">
	<xsl:text>.. </xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The doctest_block element contains the common attributes (ids,
  names, dupnames, source, and classes), plus xml:space.
  -->
  <!-- == simple_body_element -->
  <xsl:template
      match="doctest_block">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:apply-templates/>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- An image element can have various roles; they are all covered here -->
  <!-- == simple_body_element == inline_element == directive -->
  <xsl:template
      match="image">
    <xsl:choose>
      <xsl:when
	  test="contains($inline_containers, concat('*', name(..), '*')) and @alt">
	<!-- An inline image with an `@alt' - must be a substitution
             reference -->
	<xsl:text>|</xsl:text>
	<!-- Original text is lost - use what we have -->
	<xsl:value-of
	    select="@alt"/>
	<xsl:text>|</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<!-- A directive -->
	<xsl:if
	    test="not(parent::figure)">
	  <xsl:if
	      test="not(parent::substitution_definition)">
	    <xsl:call-template
		name="u:BandI"/>
	    <xsl:text>.. </xsl:text>
	  </xsl:if>
	  <xsl:text>image:: </xsl:text>
	</xsl:if>
	<xsl:value-of
	    select="@uri"/>
	&tEOL;
	<xsl:choose>
	  <xsl:when
	      test="parent::figure">
	    <!-- `@classes' is special because it is in the parent -->
	    <xsl:if
		test="../@classes">
	      <xsl:call-template
		  name="u:param">
		<xsl:with-param
		    name="name"
		    select="'figclass'"/>
		<xsl:with-param
		    name="value"
		    select="../@classes"/>
		<xsl:with-param
		    name="ancestors"
		    select="ancestor::*"/>
	      </xsl:call-template>
	    </xsl:if>
	    <!-- `@align' is special because it is in the parent -->
	    <xsl:if
		test="../@align">
	      <xsl:call-template
		  name="u:param">
		<xsl:with-param
		    name="name"
		    select="'align'"/>
		<xsl:with-param
		    name="value"
		    select="../@align"/>
		<xsl:with-param
		    name="ancestors"
		    select="ancestor::*"/>
	      </xsl:call-template>
	    </xsl:if>
	    <xsl:call-template
		name="u:params">
	      <!-- `figure' would add one level of indentation --> 
	      <xsl:with-param
		  name="ancestors"
		  select="ancestor::*"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:when
	      test="parent::substitution_definition">
	    <xsl:call-template
		name="u:params">
	      <!-- `@alt' only for the real images --> 
	      <xsl:with-param
		  name="params"
		  select="@*[name() != 'alt']"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:call-template
		name="u:params">
	      <xsl:with-param
		  name="params"
		  select="@*[name() != 'ids' and name() != 'names']"/>
	    </xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:if
	    test="parent::reference">
	  <!-- A clickable image -->
	  <xsl:call-template
	      name="u:param">
	    <xsl:with-param
		name="name"
		select="'target'"/>
	    <xsl:with-param
		name="value">
	      <xsl:choose>
		<xsl:when
		    test="../@name">
		  <!-- An internal link -->
		  <xsl:call-template
		      name="u:inlineReference">
		    <xsl:with-param
			name="text"
			select="../@refid"/>
		  </xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
		  <!-- An external link -->
		  <xsl:value-of
		      select="../@refuri"/>
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:with-param>
	    <xsl:with-param
		name="ancestors"
		select="ancestor-or-self::*"/>
	  </xsl:call-template>
	</xsl:if>
	<!-- Always blank line after parameter block -->
	&tCR;
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (line_block | line)+
  Attributes:    The line_block element contains the common attributes (ids, 
  names, dupnames, source, and classes), plus xml:space.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="line_block">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:variable
	name="isEmbedded"
	select="name(..) = 'line_block'"/>
    <xsl:if
	test="not($isEmbedded)">
      <xsl:call-template
	  name="u:blank"/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The line element contains the common attributes (ids, 
  names, dupnames, source, and classes).
  -->
  <!-- == simple_body_subelement == folding_element -->
  <xsl:template
      match="line">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:variable
	name="indent">
      <xsl:call-template
	  name="u:indent"/>
    </xsl:variable>
    <xsl:value-of
	select="$indent"/>
    <xsl:choose>
      <xsl:when
	  test="node()">
	<!-- Only for non-empty lines -->
	<xsl:variable
	    name="lineBlockIndent">
	  <!-- Very special indendation for nested `line_block's -->
	  <xsl:for-each
	      select="ancestor::line_block[position() > 1]">
	    <xsl:value-of
		select="str:padding(4)"/>
	  </xsl:for-each>
	</xsl:variable>
	<xsl:call-template
	    name="u:outputFolding">
	  <xsl:with-param
	      name="prefix">
	    <xsl:text>| </xsl:text>
	    <xsl:value-of
		select="$lineBlockIndent"/>
	  </xsl:with-param>
	  <xsl:with-param
	      name="indent"
	      select="concat($indent, '  ', $lineBlockIndent)"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>|</xsl:text>
	&tEOL;
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The literal_block element contains the common attributes (ids,
  names, dupnames, source, and classes), plus xml:space.
  -->
  <!-- == simple_body_element == directive -->
  <xsl:template
      match="literal_block">
    <xsl:choose>
      <xsl:when
	  test=".//*[contains($inline_elements, concat('*', name(), '*'))]">
	<!-- If it contains inline elements this is a parsed-literal -->
	<xsl:call-template
	    name="u:BandI"/>
	<xsl:text>.. parsed-literal::</xsl:text>
	&tEOL;
	<xsl:call-template
	    name="u:params"/>
	&tCR;
      </xsl:when>
      <xsl:otherwise>
	<xsl:call-template
	    name="u:outputClass"/>
	<!-- TODO Support for the (fully) minimized style would be nice but
	          is difficult to accomplish because there is a linefeed
	          already when we arrive here :-( -->
	<xsl:call-template
	    name="u:BandI"/>
	<xsl:text>::</xsl:text>
	&tEOL;
	&tCR;
      </xsl:otherwise>
    </xsl:choose>
    <xsl:variable
	name="isQuotedLiteral">
      <xsl:call-template
	  name="u:isQuotedLiteral"/>
    </xsl:variable>
    <!-- Indent correctly depending on quoted literal or not -->
    <xsl:choose>
      <xsl:when
	  test="string-length($isQuotedLiteral)">
	<xsl:call-template
	    name="u:indent">
	  <xsl:with-param
	      name="ancestors"
	      select="ancestor::*"/>
	</xsl:call-template>
	<xsl:apply-templates
	    mode="quotedLiteral"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:call-template
	    name="u:indent">
	  <xsl:with-param
	      name="ancestors"
	      select="ancestor-or-self::*"/>
	</xsl:call-template>
	<xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
    &tEOL;
  </xsl:template>

  <!-- Indent a text of a quoted literal containing line feeds correctly -->
  <xsl:template
      match="text()"
      mode="quotedLiteral">
    <xsl:call-template
	name="u:indentLF">
      <xsl:with-param
	  name="indent">
	<xsl:call-template
	    name="u:indent">
	  <xsl:with-param
	      name="ancestors"
	      select="ancestor::*[position() > 1]"/>
	</xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- Determine whether `$text' is a quoted literal and return the quote
       character if so -->
  <xsl:template
      name="u:isQuotedLiteral">
    <xsl:param
	name="text"
	select="text()"/>
    <xsl:param
	name="quote"
	select="substring($text, 1, 1)"/>
    <xsl:if
	test="contains($adornment_characters, $quote) and substring($text, 1, 1) = $quote">
      <!-- Given quote is an adornment character and first character is this
           quote -->
      <xsl:choose>
	<xsl:when
	    test="contains($text, '&#xA;')">
	  <!-- Test the remaining lines -->
	  <xsl:call-template
	      name="u:isQuotedLiteral">
	    <xsl:with-param
		name="text"
		select="substring-after($text, '&#xA;')"/>
	    <xsl:with-param
		name="quote"
		select="$quote"/>
	  </xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
	  <!-- No more lines to test so this is a quoted literal -->
	  <xsl:value-of
	      select="$quote"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The paragraph element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == simple_body_element == folding_element -->
  <xsl:template
      match="paragraph">
    <xsl:variable
	name="previous"
	select="preceding-sibling::*[1]"/>
    <!-- Do indent except first element in some compound elements -->
    <xsl:variable
	name="needsIndent"
	select="($previous or not(parent::list_item or parent::field_body or contains($admonitions, concat('*', name(..), '*')))) and name($previous) != 'label'"/>
    <!-- Blank line in front if following a body element, except first
         elements in some compound elements -->
    <xsl:variable
	name="needsBlank"
	select="($previous or not(parent::list_item or ../parent::option_list_item or parent::field_body or parent::document or contains($admonitions, concat('*', name(..), '*')))) and (not($previous) or contains($body_elements, concat('*', name($previous), '*')) or name($previous) = 'title' and contains($titled_elements, concat('*', name(..), '*')) or name($previous) = 'docinfo')"/>
    <xsl:if
	test="$needsBlank">
      &tCR;
    </xsl:if>
    <xsl:if
	test="$needsIndent">
      <xsl:call-template
	  name="u:indent"/>
    </xsl:if>
    <xsl:if
	test="@classes">
      <!-- This paragraph has a classes attribute - always needs newline and
           indent -->
      <xsl:call-template
	  name="u:outputClass">
	<xsl:with-param
	    name="alreadyBlanked"
	    select="$needsBlank"/>
	<xsl:with-param
	    name="alreadyIndented"
	    select="$needsIndent"/>
      </xsl:call-template>
      <xsl:call-template
	  name="u:BandI"/>
    </xsl:if>
    <xsl:call-template
	name="u:outputFolding"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_element -->
  <xsl:template
      match="pending">
    <xsl:call-template
	name="u:notSupported"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_element == inline_element == directive -->
  <xsl:template
      match="raw">
    <xsl:choose>
      <xsl:when
	  test="contains($inline_containers, concat('*', name(..), '*'))">
	<!-- Used as a custom role -->
	<!-- TODO `role' directives must be generated for user-defined raw
	          roles. -->
	<!-- The name of the custom role is not contained in the input -->
	<xsl:text>:RAW-ROLE:`</xsl:text>
	<xsl:apply-templates/>
	<xsl:text>`</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<!-- A directive -->
	<xsl:call-template
	    name="u:outputClass"/>
	<xsl:call-template
	    name="u:BandI"/>
	<xsl:text>.. raw:: </xsl:text>
	<xsl:value-of
	    select="@format"/>
	&tEOL;
	<xsl:call-template
	    name="u:params">
	  <xsl:with-param
	      name="params"
	      select="@*[name() != 'format' and name() != 'classes']"/>
	</xsl:call-template>
	&tCR;
	<xsl:call-template
	    name="u:indent">
	  <xsl:with-param
	      name="ancestors"
	      select="ancestor-or-self::*"/>
	</xsl:call-template>
	<xsl:apply-templates/>
	&tEOL;
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_element == folding_element == directive -->
  <xsl:template
      match="rubric">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:call-template
	name="u:outputFolding">
      <xsl:with-param
	  name="prefix">
	<xsl:text>.. rubric:: </xsl:text>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:call-template
	name="u:params"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == compound_body_element == directive -->
  <xsl:template
      match="compound">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. compound::</xsl:text>
    &tEOL;
    <xsl:call-template
	name="u:params"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == compound_body_element == directive -->
  <xsl:template
      match="container">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. container::</xsl:text>
    <xsl:if
	test="@classes">
      &tSP;
      <xsl:value-of
	  select="@classes"/>
    </xsl:if>
    &tEOL;
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_element == directive -->
  <xsl:template
      match="substitution_definition">
    <!-- More than one child or not a directive is a replacement text -->
    <xsl:variable
	name="isReplace"
	select="not(* and count(node()) = 1 and contains($directives, concat('*', name(*[1]), '*')))"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:variable
	name="prefix">
      <xsl:text>.. |</xsl:text>
      <xsl:call-template
	  name="u:outputNames"/>
      <xsl:text>| </xsl:text>
    </xsl:variable>
    <xsl:choose>
      <xsl:when
	  test="$isReplace">
      <!-- TODO Substitution references for replace can not be found because
	        they are not marked as such; embedding them in `generated'
                would be nice -->
	<xsl:call-template
	    name="u:outputFolding">
	  <xsl:with-param
	      name="prefix">
	    <xsl:value-of
		select="$prefix"/>
	    <xsl:text>replace:: </xsl:text>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of
	    select="$prefix"/>
	<xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: ((%body.elements;)+, attribution?)
  Attributes:    The block_quote element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="block_quote">
    <xsl:if
	test="@classes = 'epigraph' or @classes = 'highlights' or @classes = 'pull-quote'">
      <xsl:call-template
	  name="u:BandI"/>
      <xsl:text>.. </xsl:text>
      <xsl:value-of
	  select="@classes"/>
      <xsl:text>::</xsl:text>
      &tEOL;
      <xsl:call-template
	  name="u:params"/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_subelement == folding_element -->
  <xsl:template
      match="attribution">
    <xsl:call-template
	name="u:outputClass"/>
    <!-- blank line between quote and attribution -->
    &tCR;
    <xsl:call-template
	name="u:indent"/>
    <xsl:call-template
	name="u:outputFolding">
      <xsl:with-param
	  name="prefix">
	<xsl:text>-- </xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == compound_body_element == directive -->
  <xsl:template
      match="citation">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. [</xsl:text>
    <xsl:value-of
	select="label"/>
    <xsl:text>] </xsl:text>
    <xsl:apply-templates
	select="*[not(self::label)]"/>
  </xsl:template>

  <!-- == simple_body_subelement -->
  <xsl:template
      match="citation/label">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == compound_body_element == directive -->
  <xsl:template
      match="figure">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. figure:: </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == simple_body_subelement == folding_element -->
  <xsl:template
      match="caption">
    <xsl:call-template
	name="u:indent"/>
    <xsl:call-template
	name="u:outputFolding"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == compound_body_subelement -->
  <xsl:template
      match="legend">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- TODO Footnotes should continue on line of definition -->

  <!-- user-numbered footnotes lack @auto -->
  <!-- == compound_body_element == directive -->
  <xsl:template
      match="footnote[not(@auto)]">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. [</xsl:text>
    <xsl:apply-templates
	select="label"/>
    <xsl:text>] </xsl:text>
    <xsl:apply-templates
	select="*[not(self::label)]"/>
  </xsl:template>

  <!-- autonumbered footnotes have @auto -->
  <!-- if the target footnote_reference@names matches its label, it was not a
       numbered-name footnote -->
  <!-- == compound_body_element == directive -->
  <xsl:template
      match="footnote[@auto='1']">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. [#</xsl:text>
    <xsl:if
	test="@names = @ids">
      <xsl:call-template
	  name="u:outputNames"/>
    </xsl:if>
    <xsl:text>] </xsl:text>
    <xsl:apply-templates
	select="*[not(self::label)]"/>
  </xsl:template>

  <!-- autosymboled footnotes have @auto -->
  <!-- == compound_body_element == directive -->
  <xsl:template
      match="footnote[@auto='*']">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>.. [*] </xsl:text>
    <xsl:apply-templates
	select="*[not(self::label)]"/>
  </xsl:template>

  <!-- == compound_body_element == directive -->
  <xsl:template
      match="footnote[starts-with(@names, 'TARGET_NOTE:\ ')]">
    <!-- This is not a footnote but a hint for a directive -->
    <xsl:if
	test="generate-id(//footnote[starts-with(@names, 'TARGET_NOTE:\ ')][1]) = generate-id(.)">
      <!-- Only for the first one -->
      <xsl:call-template
	  name="u:BandI"/>
      <!-- TODO May have a `classes` attribute -->
      <xsl:text>.. target-notes::</xsl:text>
      &tEOL;
    </xsl:if>
  </xsl:template>

  <!-- == simple_body_subelement -->
  <xsl:template
      match="footnote/label">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (list_item +)
  Attributes:    The bullet_list element contains the common attributes (ids,
  names, dupnames, source, and classes), plus bullet.
      bullet is used to record the style of bullet from the input data.
      In documents processed from reStructuredText, it contains one of "-",
  "+", or "*". It may be ignored in processing.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="bullet_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates
	mode="bullet_list"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (definition_list_item +)
  Attributes:    The definition_list element contains only the common
  attributes: ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="definition_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (term, classifier?, definition)
  Attributes:    The definition_list_item element contains only the common 
  attributes: ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="definition_list_item">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The term element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == simple_body_subelement -->
  <xsl:template
      match="term">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The classifier element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == simple_body_subelement -->
  <xsl:template
      match="classifier">
    <xsl:text> : </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)+
  Attributes:    The definition element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="definition">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (list_item +)
  Attributes:    The enumerated_list element contains the common attributes
  (ids, names, dupnames, source, and classes), plus enumtype, prefix, suffix, and
  start.
      enumtype is used to record the intended enumeration sequence, one
  of "arabic" (1, 2, 3, ...), "loweralpha" (a, b, c, ..., z), "upperalpha" (A,
  B, C, ..., Z), "lowerroman" (i, ii, iii, iv, ..., mmmmcmxcix [4999]), or 
  "upperroman" (I, II, III, IV, ..., MMMMCMXCIX [4999]).
      prefix stores the formatting characters used before the enumerator. In 
  documents originating from reStructuredText data, it will contain either "" 
  (empty string) or "(" (left parenthesis). It may or may not affect
  processing.
      suffix stores the formatting characters used after the enumerator. In 
  documents originating from reStructuredText data, it will contain either "." 
  (period) or ")" (right parenthesis). Depending on the capabilities of the 
  output format, this attribute may or may not affect processing.
      start contains the ordinal value of the first item in the list, in 
  decimal. For lists beginning at value 1 ("1", "a", "A", "i", or "I"), this 
  attribute may be omitted.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="enumerated_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates
	mode="enumerated_list"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (field +)
  Attributes:    The field_list element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="field_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (field_name, field_body)
  Attributes:    The field element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="field">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ********************************************************************** -->

  <!--
  Content Model: %text.model;
  Attributes:    The field_name element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == simple_body_subelement -->
  <xsl:template
      match="field_name">
    <xsl:text>:</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>: </xsl:text>
    <!-- no EOL: field_body starts on same line -->
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)*
  Attributes:    The field_body element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="field_body">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (option_list_item +)
  Attributes:    The option_list element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="option_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:blank"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (option_group, description)
  Attributes:    The option_list_item element contains only the common
  attributes: ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="option_list_item">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:indent"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (option_group, description)
  Attributes:    The option_group element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="option_group">
    <xsl:apply-templates/>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (option_string, option_argument *)
  Attributes:    The option element contains only the common attributes: ids,
  names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="option">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
    <xsl:if
	test="generate-id(current()) != generate-id(../*[last()])">
      <!-- No comma after final option -->
      <xsl:text>, </xsl:text>
    </xsl:if>
    <!-- no EOL: description on same line -->
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (#PCDATA)
  Attributes:    The option_string element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == simple_body_subelement -->
  <xsl:template
      match="option_string">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (#PCDATA)
  Attributes:    The option_argument element contains the common attributes
  (ids,  names, dupnames, source, and classes), plus delimiter.
      delimiter contains the text preceding the option_argument:
  either the text separating it from the option_string (typically
  either "=" or " ")
  or the text between option arguments (typically either "," or " ").
  -->
  <!-- == simple_body_subelement -->
  <xsl:template
      match="option_argument">
    <xsl:value-of
	select="@delimiter"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)+
  Attributes:    The description element contains only the common attributes:
  ids, names, dupnames, source, and classes.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="description">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:apply-templates/>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)+
  Attributes:    The list_item element contains only the common attributes:
  ids, names, dupnames, source, and classes

  BULLET LIST
      bullet is used to record the style of bullet from the input data.
      In documents processed from reStructuredText, it contains one of "-",
  "+", or "*". It may be ignored in processing.
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="list_item"
      mode="bullet_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:if
	test="name(*[1]) != 'bullet_list'">
      <!-- Start the list item only if the first child is not another list -->
      <xsl:call-template
	  name="u:BandI"/>
      <xsl:value-of
	  select="../@bullet"/>    
      &tSP;  <!-- space after bullet -->
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  ENUMERATED LIST
      enumtype is used to record the intended enumeration sequence, one of 
  "arabic" (1, 2, 3, ...), "loweralpha" (a, b, c, ..., z), "upperalpha" (A, B, 
  C, ..., Z), "lowerroman" (i, ii, iii, iv, ..., mmmmcmxcix [4999]), or 
  "upperroman" (I, II, III, IV, ..., MMMMCMXCIX [4999]).
      prefix stores the formatting characters used before the enumerator. In 
  documents originating from reStructuredText data, it will contain either "" 
  (empty string) or "(" (left parenthesis). It may or may not affect
  processing.
      suffix stores the formatting characters used after the enumerator. In 
  documents originating from reStructuredText data, it will contain either "." 
  (period) or ")" (right parenthesis). Depending on the capabilities of the 
  output format, this attribute may or may not affect processing.
      start contains the ordinal value of the first item in the list, in 
  decimal. For lists beginning at value 1 ("1", "a", "A", "i", or "I"), this 
  attribute may be omitted.

  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="list_item"
      mode="enumerated_list">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:BandI"/>
    <xsl:call-template
	name="u:outputEnumerator"/>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Outputs a complete enumerator when called in an
       enumerated_list/list_item -->
  <xsl:template
      name="u:outputEnumerator">
    <!-- Use parent's numeration attribute -->
    <xsl:variable
	name="enumType"
	select="../@enumtype"/>
    <!-- Determine starting point -->
    <xsl:variable
	name="start">
      <xsl:choose>
	<xsl:when
	    test="../@start">
	  <xsl:value-of
	      select="../@start"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="1"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Determine position of this item in its real context -->
    <xsl:variable
	name="position">
      <xsl:variable
	  name="wanted"
	  select="generate-id()"/>
      <!-- Generate the right current node list -->
      <xsl:for-each
	  select="../list_item">
	<xsl:if
	    test="generate-id() = $wanted">
	  <xsl:value-of
	      select="position()"/>
	</xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <!-- Determine encoding of the number for the given numeration -->
    <xsl:variable
	name="cur">
      <xsl:call-template
	  name="u:position2Enumerator">
	<xsl:with-param
	    name="enumType"
	    select="$enumType"/>
	<xsl:with-param
	    name="position"
	    select="$position"/>
	<xsl:with-param
	    name="start"
	    select="$start"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- Determine encoding of the maximum number -->
    <xsl:variable
	name="max">
      <xsl:call-template
	  name="u:position2Enumerator">
	<xsl:with-param
	    name="enumType"
	    select="$enumType"/>
	<xsl:with-param
	    name="position"
	    select="count(../list_item)"/>
	<xsl:with-param
	    name="start"
	    select="$start"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- Output complete enumerator -->
    <xsl:value-of
	select="../@prefix"/>
    <xsl:value-of
	select="$cur"/>
    <xsl:value-of
	select="../@suffix"/>
    <!-- Output at least one trailing space -->
    &tSP;
    <!-- Output more whitespace to align with the maximum enumerator -->
    <xsl:if
	test="$enumType != 'lowerroman' and $enumType != 'upperroman'">
      <!-- Assumes that the maximum number has maximum string length -->
      <xsl:value-of
	  select="str:padding(string-length($max) - string-length($cur))"/>
    </xsl:if>
  </xsl:template>

  <!-- Determine the right ordinal enumerator based on the parameters -->
  <xsl:template
      name="u:position2Enumerator">
    <xsl:param
	name="enumType"/>
    <xsl:param
	name="start"/>
    <xsl:param
	name="position"/>
    <!-- Determine logical number -->
    <xsl:variable
	name="ordinal"
	select="$start - 1 + $position"/>
    <xsl:choose>
      <xsl:when
	  test="$enumType = 'arabic'">
	<xsl:value-of
	    select="$ordinal"/>
      </xsl:when>
      <xsl:when
	  test="$enumType = 'loweralpha'">
	<xsl:value-of
	    select="substring('abcdefghijklmnopqrstzuvwxyz', $ordinal, 1)"/>
      </xsl:when>
      <xsl:when
	  test="$enumType = 'upperalpha'">
	<xsl:value-of
	    select="substring('ABCDEFGHIJKLMNOPQRSTZUVWXYZ', $ordinal, 1)"/>
      </xsl:when>
      <!-- TODO Support for counting roman numbers -->
      <xsl:when
	  test="$enumType = 'lowerroman'">
	<xsl:text>i</xsl:text>
      </xsl:when>
      <xsl:when
	  test="$enumType = 'upperroman'">
	<xsl:text>I</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!--
  Content Model: (title?, tgroup+)
  Attributes:    The table element contains the common attributes and:
  frame, colsep, rowsep, pgwide
  -->
  <!-- == compound_body_element -->
  <xsl:template
      match="table">
    <xsl:call-template
	name="u:outputClass"/>
    <xsl:call-template
	name="u:blank"/>
    <xsl:apply-templates
	select="tgroup"/>
    <xsl:if
	test="title">
      <!-- TODO A table title must be rendered by using the `.. table::'
                directive -->
      <xsl:call-template
	  name="u:BandI"/>
      <xsl:apply-templates
	  select="title"/>
      &tEOL;
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (colspec*, thead?, tbody)
  Attributes:    The tgroup element contains the common attributes and:
  cols, colsep, rowsep, align
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="tgroup">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: EMPTY
  Attributes:    The colspec element contains the common attributes and:
  colnum, colname, colwidth, colsep, rowsep, align, char, charoff

  The colwidth attribute gives the width of the respective column in characters
  including padding whitespace but no separator markup.
  -->
  <!-- == simple_body_subelement -->
  <!-- @colwidth needed by children but element has no own output -->
  <xsl:template
      match="colspec"/>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (row+)
  Attributes:    The thead element contains the common attributes and:
  valign
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="thead">
    <xsl:apply-templates/>
  </xsl:template>

  <!--
  Content Model: (row+)
  Attributes:    The tbody element contains the common attributes and:
  valign
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="tbody">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (entry+)
  Attributes:    The row element contains the common attributes and:
  rowsep, valign
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="row">
    <!-- Separator line above unless first row of a tbody with no previous
         thead (in this case the separator line is output already as the
         closing separator line of thead) -->
    <xsl:if
	test="position() > 1 or parent::thead or parent::tbody and not(../../thead)">
      <xsl:call-template
	  name="u:rowSeparatorLine"/>
    </xsl:if>
    <!-- Determine heights in physical lines of all entries -->
    <xsl:variable
	name="heights">
      <xsl:for-each
	  select="entry">
	<xsl:variable
	    name="text">
	  <!-- Catch the text of all entries -->
	  <xsl:apply-templates/>
	</xsl:variable>
	<!-- Compute height of this entry; leading and trailing EOL must be
             subtracted -->
	<xsl:value-of
	    select="string-length($text) - string-length(translate($text, '&#xA;', '')) - 1"/>
	&tSP; <!-- A space as a list separator -->
      </xsl:for-each>
    </xsl:variable>
    <!-- Determine maximum height so every entry must be this high -->
    <xsl:variable
	name="maxHeight"
	select="math:max(str:tokenize(normalize-space($heights)))"/>
    <!-- Output all the physical lines of this row -->
    <xsl:call-template
	name="u:rowLines">
      <xsl:with-param
	  name="currentLine"
	  select="1"/>
      <xsl:with-param
	  name="maxLine"
	  select="$maxHeight"/>
    </xsl:call-template>
    <!-- Output final separator line if this is the last row -->
    <xsl:if
	test="position() = last()">
      <xsl:call-template
	  name="u:rowSeparatorLine">
	<xsl:with-param
	    name="char">
	  <!-- Determine correct character for the separator line -->
	  <xsl:choose>
	    <xsl:when
		test="parent::thead">
	      <xsl:value-of
		  select="'='"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of
		  select="'-'"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- Output physical line $currentLine of a row and continue until
       line $maxLine is output -->
  <xsl:template
      name="u:rowLines">
    <xsl:param
	name="currentLine"/>
    <xsl:param
	name="maxLine"/>
    <xsl:if
	test="$currentLine &lt;= $maxLine">
      <!-- If there are still physical lines to output do it -->
      <xsl:call-template
	  name="u:indent"/>
      <!-- Leading bar -->
      <xsl:text>|</xsl:text>
      <xsl:apply-templates>
	<xsl:with-param
	    name="currentLine"
	    select="$currentLine"/>
      </xsl:apply-templates>
      <!-- End of this physical line -->
      &tEOL;
      <!-- Continue with the next physical line -->
      <xsl:call-template
	  name="u:rowLines">
	<xsl:with-param
	    name="currentLine"
	    select="$currentLine + 1"/>
	<xsl:with-param
	    name="maxLine"
	    select="$maxLine"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- Output a separator line with all the right characters -->
  <xsl:template
      name="u:rowSeparatorLine">
    <xsl:param
	name="char"
	select="'-'"/>
    <xsl:call-template
	name="u:indent"/>
    <xsl:text>+</xsl:text>
    <xsl:for-each
	select="../../colspec">
      <xsl:value-of
	  select="str:padding(@colwidth, $char)"/>
      <xsl:text>+</xsl:text>
    </xsl:for-each>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: (%body.elements;)*
  Attributes:    The entry element contains the common attributes and:
  colname, namest, morerows, colsep, rowsep, align, char, charoff, valign and
  morecols
  -->
  <!-- == compound_body_subelement -->
  <xsl:template
      match="entry">
    <!-- TODO `classes` attribute needs support -->
    <!-- This is called in two ways; if $currentLine = 0 all physical lines
         of this entry must be output; if $currentLine > 0 the physical line
         with exactly this number shall be output -->
    <xsl:param
	name="currentLine"
	select="0"/>
    <xsl:variable
	name="column"
	select="position() + sum(preceding-sibling::entry/@morecols)"/>
    <!-- Determine width in characters needed for this entry -->
    <xsl:variable
	name="width">
      <xsl:call-template
	  name="u:computeEntryWidth">
	<xsl:with-param
	    name="colspecs"
	    select="../../../colspec"/>
	<xsl:with-param
	    name="column"
	    select="$column"/>
	<xsl:with-param
	    name="span"
	    select="@morecols"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- Output the entry completely or a certain physical line -->
    <xsl:call-template
	name="u:outputEntry">
      <xsl:with-param
	  name="string">
	<!-- Capture physical lines of the entry in a variable -->
	<xsl:apply-templates/>
      </xsl:with-param>
      <xsl:with-param
	  name="width"
	  select="$width"/>
      <xsl:with-param
	  name="expectedIndent">
	<!-- Capture indent for the entry generated by the normal template
             rules to remove it later -->
	<xsl:call-template
	    name="u:indent"/>
      </xsl:with-param>
      <xsl:with-param
	  name="outputLine"
	  select="$currentLine"/>
    </xsl:call-template>
    <!-- Final bar after the entry -->
    <xsl:text>|</xsl:text>
  </xsl:template>

  <!-- Compute width of the entry -->
  <xsl:template
      name="u:computeEntryWidth">
    <!-- The colspec elements of all columns -->
    <xsl:param
	name="colspecs"/>
    <!-- Column of this entry -->
    <xsl:param
	name="column"/>
    <!-- Number of columns this entry spans -->
    <xsl:param
	name="span"
	select="0"/>
    <xsl:param
	name="sum"
	select="0"/>
    <xsl:choose>
      <xsl:when
	  test="$span">
	<!-- If entry spans multiple columns compute their width -->
	<xsl:call-template
	    name="u:computeEntryWidth">
	  <xsl:with-param
	      name="colspecs"
	      select="$colspecs"/>
	  <xsl:with-param
	      name="column"
	      select="$column + 1"/>
	  <xsl:with-param
	      name="span"
	      select="$span - 1"/>
	  <!-- Add the separator character and the following column width -->
	  <xsl:with-param
	      name="sum"
	      select="$sum + 1 + $colspecs[$column]/@colwidth"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<!-- Does not span more columns so return sum and width of this
             column -->
	<xsl:value-of
	    select="$sum + $colspecs[$column]/@colwidth"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Outputs one or all lines of a table entry as a string trimmed left and
       padded -->
  <xsl:template
      name="u:outputEntry">
    <!-- Width of the entry; there is no provision for actual physical lines
         longer than this width -->
    <xsl:param
	name="width"/>
    <!-- The string containing the remaining physical lines; may be an empty
         string -->
    <xsl:param
	name="string"
	select="''"/>
    <!-- The indendation which is expected to be prefixed before every
         physical line -->
    <xsl:param
	name="expectedIndent"
	select="''"/>
    <!-- Is this the first call to this template -->
    <xsl:param
	name="isFirst"
	select="true()"/>
    <!-- Number of physical line to output or 0 to output all lines -->
    <xsl:param
	name="outputLine"
	select="0"/>
    <!-- Output is wanted if all or the first physical line are to be
         output -->
    <xsl:variable
	name="doOutput"
	select="$outputLine = 0 or $outputLine = 1"/>
    <xsl:variable
	name="stringLFHalfTrimmed">
      <xsl:choose>
	<xsl:when
	    test="$isFirst and substring($string, 1, 1) = '&#x0A;'">
	  <!-- Remove leading linefeed if this is the first time -->
	  <xsl:value-of
	      select="substring($string, 2)"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="$string"/>
	  </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable
	name="stringLFTrimmed">
      <xsl:choose>
	<xsl:when
	    test="$isFirst and substring($stringLFHalfTrimmed, string-length($stringLFHalfTrimmed), 1) = '&#x0A;'">
	  <!-- Remove trailing linefeed if this is the first time -->
	  <xsl:value-of
	      select="substring($stringLFHalfTrimmed, 1, string-length($stringLFHalfTrimmed) - 1)"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="$stringLFHalfTrimmed"/>
	  </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Determine remaining lines after the first one -->
    <xsl:variable
	name="remainingLines">
      <xsl:if
	  test="contains($stringLFTrimmed, '&#x0A;')">
	<xsl:value-of
	    select="substring-after($stringLFTrimmed, '&#x0A;')"/>
      </xsl:if>
    </xsl:variable>
    <xsl:if
	test="$doOutput">
      <!-- If this physical line must be output determine the first physical
           line -->
      <xsl:variable
	  name="firstLine">
	<xsl:choose>
	  <xsl:when
	      test="string-length($remainingLines)">
	    <xsl:value-of
		select="substring-before($stringLFTrimmed, '&#x0A;')"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of
		select="$stringLFTrimmed"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>
      <!-- Remove the leading indentation from the physical line which is
           brought there by the normal templates -->
      <xsl:variable
	  name="trimmed">
	<xsl:if
	    test="string-length($firstLine)">
	  <!-- Trim only non-empty lines -->
	  <xsl:value-of
	      select="substring-after($firstLine, $expectedIndent)"/>
	</xsl:if>
      </xsl:variable>
      <!-- Pad the line with a leading and a trailing space -->
      <xsl:variable
	  name="padded"
	  select="concat(' ', $trimmed, ' ')"/>
      <!-- Output the padded value -->
      <xsl:value-of
	  select="$padded"/>
      <!-- Fill up the width of the entry with spaces -->
      <xsl:if
	  test="$width - string-length($padded) &lt; 0">
	<xsl:message>
	  <xsl:text>WARNING: Table column too narrow (minimum: </xsl:text>
	  <xsl:value-of
	      select="string-length($padded)"/>
	  <xsl:text>)</xsl:text>
	  &tEOL;
	</xsl:message>
      </xsl:if>
      <xsl:value-of
	  select="str:padding($width - string-length($padded))"/>
    </xsl:if>
    <xsl:if
	test="$outputLine > 1 or $outputLine = 0 and string-length($remainingLines)">
      <!-- If a following physical line must be output or if all physical
           lines shall be output and there are remaining physical lines -->
      <xsl:if
	  test="$outputLine = 0">
	<!-- Output linefeed only if we output all the lines -->
	&tEOL;
      </xsl:if>
      <!-- Output the remaining lines -->
      <xsl:call-template
	  name="u:outputEntry">
	<xsl:with-param
	    name="width"
	    select="$width"/>
	<xsl:with-param
	    name="string"
	    select="$remainingLines"/>
	<xsl:with-param
	    name="expectedIndent"
	    select="$expectedIndent"/>
	<xsl:with-param
	    name="isFirst"
	    select="false()"/>
	<xsl:with-param
	    name="outputLine">
	  <xsl:choose>
	    <xsl:when
		test="$outputLine = 0">
	      <xsl:value-of
		  select="0"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of
		  select="$outputLine - 1"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!-- == inline_element -->
  <xsl:template
      match="citation_reference">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>[</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>]_</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == inline_element -->
  <xsl:template
      match="substitution_reference">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>|</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>|</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == inline_element -->
  <xsl:template
      match="emphasis">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>*</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>*</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- user-numbered footnotes lack @auto -->
  <!-- == inline_element -->
  <xsl:template
      match="footnote_reference[not(@auto)]">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>[</xsl:text>
    <xsl:value-of
	select="text()"/>
    <xsl:text>]_</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
    <!-- child paragraph provides blank line -->
  </xsl:template>

  <!-- automatically numbered footnotes have @auto -->
  <!-- if @names is different from label content, it is a named footnote -->
  <!-- == inline_element -->
  <xsl:template
      match="footnote_reference[@auto='1']">
    <xsl:variable
	name="ref"
	select="@refid"/>
    <xsl:if
	test="not(starts-with(//footnote[@ids=$ref]/@names, 'TARGET_NOTE:\ '))">
      <!-- Not a generated footnote reference for a `.. target-notes::';
           such footnote reference and the preceding space should be
           embedded in `generated'! -->
      <xsl:call-template
	  name="u:bkslshEscPre"/>
      <xsl:text>[#</xsl:text>
      <xsl:if
	  test="//footnote[@ids=$ref]/@names != //footnote[@ids=$ref]/label">
	<xsl:call-template
	    name="u:outputNames">
	  <xsl:with-param
	      name="names"
	      select="//footnote[@ids=$ref]/@names"/>
	</xsl:call-template>
      </xsl:if>
      <xsl:text>]_</xsl:text>
      <xsl:call-template
	  name="u:bkslshEscSuf"/>
    </xsl:if>
  </xsl:template>

  <!-- automatically symboled footnotes have @auto=* -->
  <!-- == inline_element -->
  <xsl:template
      match="footnote_reference[@auto='*']">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>[*]_</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  -->
  <!-- == inline_element -->
  <xsl:template
      match="literal">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>``</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>``</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Attribute combinations found in `standard' text and other examples:
       @refuri = standalone hyperlink
       @ids @refid = TOC, probably all in <generated>
       @name @refuri with matching <target> in document = named external hyperlink _
       @name @refuri immediately followed by matching <target> = named embedded URI _
       @name @refuri with no matching <target> in document = anonymous embedded URI __
       @anonymous @name @refuri with no matching <target> in document = anonymous explicit URI __
       @name @refid = internal cross-reference _
       @anonymous @name @refid = anonymous implicit internal reference __
       @name @refid image = clickable image to internal reference _
       @refuri image = clickable image to standalone hyperlink

       A target matches if target/@names contains the lower cased, whitespace
       quoted reference/@name
  -->

  <!-- Standalone hyperlink -->
  <!-- == inline_element -->
  <xsl:template
      match="reference[not(@name or @anonymous)]">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:choose>
      <xsl:when
	  test="starts-with(., 'PEP ')">
	<xsl:text>:PEP:`</xsl:text>
	<xsl:value-of
	    select="substring-after(., 'PEP ')"/>
	<xsl:text>`</xsl:text>
      </xsl:when>
      <xsl:when
	  test="starts-with(., 'RFC ')">
	<xsl:text>:RFC:`</xsl:text>
	<xsl:value-of
	    select="substring-after(., 'RFC ')"/>
	<xsl:text>`</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- External references -->
  <!-- == inline_element -->
  <xsl:template
      match="reference[@name and @refuri]">
    <!-- Determine normalized name by downcasing it -->
    <xsl:variable
	name="normalized"
	select="translate(normalize-space(@name), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
    <xsl:variable
	name="quoted"
	select="str:replace($normalized, ' ', '\ ')"/>
    <xsl:variable
	name="matching"
	select="//target[contains(@names, $quoted)]"/>
    <xsl:call-template
	name="u:inlineReference">
      <xsl:with-param
	  name="anonymous"
	  select="not($matching) or @anonymous"/>
      <xsl:with-param
	  name="embedded"
	  select="not(@anonymous) and (not($matching) or generate-id(following-sibling::node()[1]) = generate-id($matching))"/>
    </xsl:call-template>
  </xsl:template>

  <!-- Internal references -->
  <!-- == inline_element -->
  <xsl:template
      match="reference[@name and @refid]">
    <xsl:call-template
	name="u:inlineReference">
      <xsl:with-param
	  name="anonymous"
	  select="@anonymous"/>
    </xsl:call-template>
  </xsl:template>

  <!-- Image references -->
  <!-- == inline_element -->
  <xsl:template
      match="reference[image]">
    <!-- All done by the `image' tag -->
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  -->
  <!-- == inline_element -->
  <xsl:template
      match="strong">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>**</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>**</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == inline_element -->
  <xsl:template
      match="subscript">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>:sub:`</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>`</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- == inline_element -->
  <xsl:template
      match="superscript">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>:sup:`</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>`</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- The target element has various roles depending on context; they are
       all handled here -->
  <!-- == simple_body_element == inline_element == directive -->
  <xsl:template
      match="target">
    <xsl:choose>
      <xsl:when
	  test="name(preceding-sibling::*[1]) = 'reference'">
        <!-- An embedded inline target - handled by the reference itself -->
      </xsl:when>
      <xsl:when
	  test="contains($inline_containers, concat('*', name(..), '*'))">
	<!-- An inline target of some sort -->
	<xsl:call-template
	    name="u:inlineReference">
	  <xsl:with-param
	      name="isTarget"
	      select="true()"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:when
	  test="@anonymous">
	<!-- An anonymous target directive -->
	<xsl:call-template
	    name="u:outputClass"/>
	<xsl:call-template
	    name="u:BandI"/>
	<xsl:text>__ </xsl:text>
	<xsl:choose>
	  <xsl:when
	      test="@refid">
	    <xsl:call-template
		name="u:outputNamesRefid"/>
	    <xsl:text>_</xsl:text>
	  </xsl:when>
	  <xsl:when
	      test="@refuri">
	    <xsl:value-of
		select="@refuri"/>
	  </xsl:when>
	</xsl:choose>
	&tEOL;
      </xsl:when>
      <xsl:when
	  test="@names | @refid">
	<!-- A target directive -->
	<xsl:call-template
	    name="u:outputClass"/>
	<xsl:call-template
	    name="u:BandI"/>
	<xsl:text>.. _</xsl:text>
	<xsl:choose>
	  <xsl:when
	      test="@refid">
	    <xsl:call-template
		name="u:outputNamesRefid"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:variable
		name="quoteNeeded"
		select="contains(@names, ':')"/>
	    <xsl:if
		test="$quoteNeeded">
	      <xsl:text>`</xsl:text>
	    </xsl:if>
	    <xsl:call-template
		name="u:outputNames"/>
	    <xsl:if
		test="$quoteNeeded">
	      <xsl:text>`</xsl:text>
	    </xsl:if>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:text>:</xsl:text>
	<xsl:if
	    test="@refuri">
	  <xsl:text> </xsl:text>
	  <xsl:value-of
	      select="@refuri"/>
	</xsl:if>
	&tEOL;
      </xsl:when>
      <xsl:otherwise>
	<!-- Should not happen -->
	<xsl:call-template
	    name="u:notSupported"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  -->
  <!-- == inline_element -->
  <xsl:template
      match="title_reference">
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>`</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>`</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Content Model: %text.model;
  -->
  <!-- == inline_element -->
  <xsl:template
      match="inline">
    <!-- TODO `role' directives must be generated for plain and derived
              user-defined roles. -->
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of
	select="@classes"/>
    <xsl:text>:`</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>`</xsl:text>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- TODO `meta` directive must be implemented. -->

  <!-- ******************************************************************** -->

  <!--
  Docutils wraps generated elements around text that is inserted (generated) by
  Docutils; i.e., text that was not in the document, like section numbers 
  inserted by the "sectnum" directive.
  -->
  <!-- == inline_element -->
  <xsl:template
      match="generated"/>

  <!-- == inline_element -->
  <xsl:template
      match="problematic">
    <!-- Simply output the contained text because this is probably the
         original text-->
    <xsl:value-of
	select="text()"/>
  </xsl:template>

  <!-- == compound_body_element -->
  <xsl:template
      match="system_message"/>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!--
  When a block of text contains linefeeds, it was indented relative to a marker
  on the first line
  -->
  <xsl:template
      match="text()">
    <xsl:call-template
	name="u:indentLF"/>
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!-- Add a blank line if necessary and indent -->
  <xsl:template
      name="u:BandI">
    <xsl:call-template
	name="u:blank"/>
    <xsl:call-template
	name="u:indent"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Add a blank line in certain contexts -->
  <xsl:template
      name="u:blank">
    <xsl:apply-templates
        mode="blankSkipInline"
	select="preceding::*[1]"/>
  </xsl:template>

  <!-- Find the preceding element we are really interested in and act
       according to this element -->
  <xsl:template
      mode="blankSkipInline"
      match="*">
    <xsl:choose>
      <!-- Skip all inline elements and body subelements and check their
           parents -->
      <xsl:when
          test="contains(concat($inline_elements, $simple_body_subelements, $compound_body_subelements), concat('*', name(.), '*'))">
	<xsl:apply-templates
	    mode="blankSkipInline"
	    select=".."/>
      </xsl:when>
      <xsl:otherwise>
	<!-- Reached the type of element we decide on -->
	<xsl:if
	    test="contains($blank_after, concat('*', name(.), '*'))">
	  &tCR;
	</xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!--
  Indent a block if it's a child of...
  -->
  <data:lookup>
    <node
	name="address"
	indent="10"/>
    <node
	name="author"
	indent="9"/>
    <node
	name="authors"
	indent="10"/>
    <node
	name="contact"
	indent="10"/>
    <node
	name="copyright"
	indent="12"/>
    <node
	name="date"
	indent="7"/>
    <node
	name="organization"
	indent="15"/>
    <node
	name="revision"
	indent="11"/>
    <node
	name="status"
	indent="9"/>
    <node
	name="version"
	indent="10"/>
    <!-- This is only for `bullet_list/list_item';
         `enumerated_list/list_item' is handled special -->
    <node
	name="list_item"
	indent="2"/>
    <node
	name="definition_list_item"
	indent="4"/>
    <node
	name="field_body"
	indent="4"/>
    <node
	name="option_list_item"
	indent="4"/>
    <!-- This is also the indentation if block_quote comes as one of the
         special directives -->
    <node
	name="block_quote"
	indent="4"/>
    <node
	name="literal_block"
	indent="4"/>
    <node
	name="attribution"
	indent="3"/>
    <node
	name="line"
	indent="2"/>
  </data:lookup>

  <!-- Do indent according to ancestor -->
  <xsl:template
      name="u:indent">
    <!-- In some cases the ancestors to indent for need to be determined
         by the calling template -->
    <xsl:param
	name="ancestors"
	select="ancestor::*"/>
    <xsl:for-each
	select="$ancestors">
      <xsl:variable
	  name="this"
	  select="name()"/>
      <xsl:choose>
	<xsl:when
	    test="contains($directives, concat('*', $this, '*'))">
	  <!-- TODO Indentation of lines after some directives must be
	            indented to align with the directive instead of a
	            fixed indentation; however, this is rather complicated
	            since identation for parameters should be fixed -->
	  <xsl:value-of
	      select="str:padding(3)"/>
	</xsl:when>
	<xsl:when
	    test="$this = 'list_item' and parent::enumerated_list">
	  <!-- Enumerated list items base their indentation on the
               numeration -->
	  <xsl:variable
	      name="enumerator">
	    <xsl:call-template
		name="u:outputEnumerator"/>
	  </xsl:variable>
	  <xsl:value-of
	      select="str:padding(string-length($enumerator))"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="str:padding(document('')//data:lookup/node[@name=$this]/@indent)"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Indent a text containing line feeds correctly -->
  <xsl:template
      name="u:indentLF">
    <xsl:param
	name="string"
	select="."/>
    <!-- A fixed indentation may be given by caller -->
    <xsl:param
	name="indent">
      <!-- If not given compute it -->
      <xsl:call-template
	  name="u:indent"/>
    </xsl:param>
    <xsl:choose>
      <xsl:when
	  test="contains($string,'&#x0A;')">
	<!-- Output first physical line -->
	<xsl:value-of
	    select="substring-before($string, '&#x0A;')"/>
	&tEOL;
	<!-- Indent before the next line -->
	<xsl:value-of
	    select="$indent"/>
	<!-- Output remaining physical lines -->
	<xsl:call-template
	    name="u:indentLF">
	  <xsl:with-param
	      name="string"
	      select="substring-after($string, '&#x0A;')"/>
	  <xsl:with-param
	      name="indent"
	      select="$indent"/>
	</xsl:call-template>
      </xsl:when>
      <!-- String does not contain newline, so just output it -->
      <xsl:otherwise> 
	<xsl:value-of
	    select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Do output for those elements which do fold their inline content -->
  <xsl:template
      name="u:outputFolding">
    <!-- The prefix text to be output before the body -->
    <xsl:param
	name="prefix"
	select="''"/>
    <!-- The indentation for this body -->
    <xsl:param
	name="indent">
      <xsl:call-template
	  name="u:indent">
	<xsl:with-param
	    name="ancestors"
	    select="ancestor-or-self::*"/>
      </xsl:call-template>
    </xsl:param>
    <xsl:variable
	name="string">
      <!-- TODO Whitespace count of inline literals must be preserved -->
      <xsl:apply-templates/>
    </xsl:variable>
    <!-- Always output prefix with all trailing and leading spaces -->
    <xsl:value-of
	select="$prefix"/>
    <xsl:choose>
      <xsl:when
	  test="$fold &gt; 0">
	<xsl:variable
	    name="normalized"
	    select="normalize-space($string)"/>
	<xsl:choose>
	  <xsl:when
	      test="$string = ''">
	    <!-- Empty strings need no output -->
	  </xsl:when>
	  <xsl:when
	      test="$normalized = ''">
	    <!-- Only white-space in string; output a single space here -->
	    &tSP;
	  </xsl:when>
	  <xsl:otherwise>
	    <!-- Output leading white-space here -->
	    <xsl:if
		test="normalize-space(substring($string, 1, 1)) = ''">
	      &tSP;
	    </xsl:if>
	    <xsl:call-template
		name="u:indentFold">
	      <xsl:with-param
		  name="string"
		  select="$normalized"/>
	      <xsl:with-param
		  name="indent"
		  select="$indent"/>
	      <xsl:with-param
		  name="cursorColumn"
		  select="string-length($indent) + string-length($prefix)"/>
	    </xsl:call-template>
	    <!-- Output trailing white-space here -->
	    <xsl:if
		test="normalize-space(substring($string, string-length($string), 1)) = ''">
	      &tSP;
	    </xsl:if>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of
	    select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Indent a string with folding -->
  <xsl:template
      name="u:indentFold">
    <!-- Normalized string to output -->
    <xsl:param
	name="string"/>
    <!-- Indentation to use for a new line -->
    <xsl:param
	name="indent"/>
    <!-- Current output column -->
    <!-- TODO This is not a correct assumption for field definitions where
              the field name effectively determines the column of the first
              line -->
    <xsl:param
	name="cursorColumn"
	select="string-length($indent)"/>
    <!-- Do we start on a new (indented) line? -->
    <xsl:param
	name="isNewLine"
	select="true()"/>
    <xsl:variable
	name="firstWord">
      <xsl:choose>
	<!-- TODO Quoted spaces must not end a word -->
	<xsl:when
	    test="contains($string, ' ')">
	  <xsl:value-of
	      select="substring-before($string, ' ')"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of
	      select="$string"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable
	name="rest"
	select="substring-after($string, ' ')"/>
    <xsl:choose>
      <xsl:when
	  test="$string = ''"/>
      <xsl:when
	  test="$isNewLine">
	<!-- Output at least first word -->
	<xsl:value-of
	    select="$firstWord"/>
	<xsl:call-template
	    name="u:indentFold">
	  <xsl:with-param
	      name="string"
	      select="$rest"/>
	  <xsl:with-param
	      name="indent"
	      select="$indent"/>
	  <xsl:with-param
	      name="cursorColumn"
	      select="$cursorColumn + string-length($firstWord)"/>
	  <xsl:with-param
	      name="isNewLine"
	      select="false()"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:when
	  test="$cursorColumn + 1 + string-length($firstWord) &gt; $fold">
	<!-- Line would get too long; start new line, indent and continue -->
	&tEOL;
	<xsl:value-of
	    select="$indent"/>
	<xsl:call-template
	    name="u:indentFold">
	  <xsl:with-param
	      name="string"
	      select="$string"/>
	  <xsl:with-param
	      name="indent"
	      select="$indent"/>
	  <xsl:with-param
	      name="cursorColumn"
	      select="string-length($indent)"/>
	  <xsl:with-param
	      name="isNewLine"
	      select="true()"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<!-- In a line and first word fits; separate and add word -->
	&tSP;
	<xsl:value-of
	    select="$firstWord"/>
	<xsl:call-template
	    name="u:indentFold">
	  <xsl:with-param
	      name="string"
	      select="$rest"/>
	  <xsl:with-param
	      name="indent"
	      select="$indent"/>
	  <xsl:with-param
	      name="cursorColumn"
	      select="$cursorColumn + 1 + string-length($firstWord)"/>
	  <xsl:with-param
	      name="isNewLine"
	      select="false()"/>
	</xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output attributes of the current element as a field list -->
  <xsl:template
      name="u:params">
    <xsl:param
	name="params"
	select="@*"/>
    <!-- Ancestors are needed for determining indentation; caller may give
         them -->
    <xsl:param
	name="ancestors"
	select="ancestor-or-self::*"/>
    <xsl:for-each
	select="$params">
      <!-- Skip URIs based on parent -->
      <xsl:if
	  test="name() != 'uri' and name() != 'xml:space'">
	<xsl:call-template
	    name="u:param">
	  <xsl:with-param
	      name="ancestors"
	      select="$ancestors"/>
	</xsl:call-template>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- Output one attribute of the current element as a field list -->
  <xsl:template
      name="u:param">
    <xsl:param
	name="name"
	select="name()"/>
    <xsl:param
	name="value"
	select="."/>
    <!-- Ancestors are needed for determining indentation; caller may give
         them -->
    <xsl:param
	name="ancestors"
	select="ancestor::*"/>
    <xsl:call-template
	name="u:indent">
      <xsl:with-param
	  name="ancestors"
	  select="$ancestors"/>
    </xsl:call-template>
    <xsl:text>:</xsl:text>
    <xsl:choose>
      <xsl:when
	  test="$name = 'classes'">
	<xsl:text>class</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of
	    select="$name"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>:</xsl:text>
    <xsl:if
	test="$value">
      <xsl:text> </xsl:text>
      <xsl:value-of
	  select="$value"/>
    </xsl:if>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output `\' or `\ ' before some inline element if necessary -->
  <xsl:template
      name="u:bkslshEscPre">
    <!-- Get the sibling node directly before the current element -->
    <xsl:variable
	name="before"
	select="preceding-sibling::node()[1]"/>
    <xsl:choose>
      <!-- No sibling before this node -->
      <xsl:when
	  test="not($before)"/>
      <!-- Element directly before this - must be another inline element -->
      <xsl:when
	  test="name($before)">
	<!-- So separate it by a quoted space -->
	<xsl:text>\ </xsl:text>
      </xsl:when>
      <!-- Node directly before this is text - check it -->
      <xsl:when
	  test="not(contains(concat($apos, ' &#xA;&#x9;&#xD;&quot;([{&lt;-/:'), substring($before, string-length($before), 1)))">
	<!-- Does not end in one of the allowed characters so separate it -->
	<xsl:text>\ </xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- Output `\' after some inline element if necessary -->
  <xsl:template
      name="u:bkslshEscSuf">
    <!-- Get the sibling node directly after the current element -->
    <xsl:variable
	name="after"
	select="following-sibling::node()[1]"/>
    <xsl:choose>
      <!-- No sibling after this node -->
      <xsl:when
	  test="not($after)"/>
      <!-- Element directly after this - must be another inline element
           handling itself -->
      <xsl:when
	  test="name($after)"/>
      <!-- Node directly after this is text - check it -->
      <xsl:when
	  test="not(contains(concat($apos, ' &#xA;&#x9;&#xD;&quot;)]}&gt;-/:.,;!?\'), substring($after, 1, 1)))">
	<!-- Does not start with one of the allowed characters so separate
             it -->
	<xsl:text>\</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- ******************************************************************** -->

  <xsl:template
      name="u:notSupported">
    <xsl:call-template
	name="u:BandI"/>
    <xsl:text>######## NOT SUPPORTED: `</xsl:text>
    <xsl:value-of
	select="name(.)"/>
    <xsl:text>' ########</xsl:text>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <xsl:template
      name="u:overline">
    <!-- Length of the rendered(!) text -->
    <xsl:param
	name="length"/>
    <!-- Depth 1 and 2 are document title and subtitle while depths
         greater than 2 are normal section titles -->
    <xsl:param
	name="depth"/>
    <!-- Test whether a overline is wanted at all -->
    <xsl:if
	test="substring($adornment, 2 * ($depth - 1) + 1, 1) = 'o'">
      <xsl:value-of
	  select="str:padding($length, substring($adornment, 2 * ($depth - 1) + 2, 1))"/>
      &tEOL;
    </xsl:if>
  </xsl:template>

  <xsl:template
      name="u:underline">
    <!-- Length of the rendered(!) text -->
    <xsl:param
	name="length"/>
    <!-- Depth 1 and 2 are document title and subtitle while depths
         greater than 2 are normal section titles -->
    <xsl:param
	name="depth"/>
    <xsl:value-of
	select="str:padding($length, substring($adornment, 2 * ($depth - 1) + 2, 1))"/>
    &tEOL;
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output a non-standalone reference or target -->
  <xsl:template
      name="u:inlineReference">
    <xsl:param
	name="anonymous"
	select="false()"/>
    <xsl:param
	name="embedded"
	select="false()"/>
    <!-- Is this a target instead of a reference? -->
    <xsl:param
	name="isTarget"
	select="false()"/>
    <xsl:param
	name="text"
	select="node()"/>
    <xsl:call-template
	name="u:bkslshEscPre"/>
    <!-- Delimiter only if link contains other than alphanumerics -->
    <xsl:variable
	name="delimiter">
      <xsl:if
	  test="* or translate($text, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '') or $embedded">
	<xsl:text>`</xsl:text>
      </xsl:if>
    </xsl:variable>
    <xsl:if
	test="$isTarget">
      <xsl:text>_</xsl:text>
    </xsl:if>
    <xsl:value-of
	select="$delimiter"/>
    <xsl:apply-templates
	select="$text"/>
    <xsl:if
	test="$embedded">
      <xsl:text> &lt;</xsl:text>
      <xsl:value-of
	  select="@refuri"/>
      <xsl:text>&gt;</xsl:text>
    </xsl:if>
    <xsl:value-of
	select="$delimiter"/>
    <xsl:if
	test="not($isTarget)">
      <xsl:text>_</xsl:text>
      <xsl:if
	  test="$anonymous">
	<xsl:text>_</xsl:text>
      </xsl:if>
    </xsl:if>
    <xsl:call-template
	name="u:bkslshEscSuf"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output a class directive for the directly following element. -->
  <!-- TODO A class directive can also be used as a container putting the
	    respective attribute to its content; however, this is not
	    reflected in XML - you'd need to check a sequence of elements
	    whether they all have the same attribute; furthermore class
	    settings for block quotes needs to be treated special -->
  <xsl:template
      name="u:outputClass">
    <!-- Blank line already output? -->
    <xsl:param
	name="alreadyBlanked"
	select="false()"/>
    <!-- Indentation already output? -->
    <xsl:param
	name="alreadyIndented"
	select="false()"/>
    <!-- Add a blank line after class directive? -->
    <xsl:param
	name="blankAfter"
	select="false()"/>
    <xsl:if
	test="@classes">
      <xsl:if
	  test="not($alreadyBlanked)">
	<xsl:call-template
	    name="u:blank"/>
      </xsl:if>
      <xsl:if
	  test="not($alreadyIndented)">
	<xsl:call-template
	    name="u:indent"/>
      </xsl:if>
      <xsl:text>.. class:: </xsl:text>
      <xsl:value-of
	  select="@classes"/>
      &tEOL;
      <xsl:if
	  test="$blankAfter">
	&tCR;
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output a names attribute at index considering quoted spaces. -->
  <xsl:template
      name="u:outputNames">
    <!-- Blank line already output? -->
    <xsl:param
	name="names"
	select="@names"/>
    <xsl:param
	name="index"
	select="0"/>
    <xsl:value-of
	select="str:replace(str:tokenize(normalize-space(str:replace($names, '\ ', '|')))[position() = $index + 1], '|', ' ')"/>
  </xsl:template>

  <!-- ******************************************************************** -->

  <!-- Output a names attribute for a refid. -->
  <xsl:template
      name="u:outputNamesRefid">
    <xsl:param
	name="refid"
	select="@refid"/>
    <!-- Determine the elements which is referred -->
    <xsl:variable
	name="refElem"
	select="//*[@ids and math:max(dyn:map(str:tokenize(normalize-space(@ids)), 'number($refid = .)')) > 0]"/>
    <xsl:call-template
	name="u:outputNames">
      <xsl:with-param
	  name="names"
	  select="$refElem/@names"/>
      <xsl:with-param
	  name="index"
	  select="math:max(dyn:map(str:tokenize(normalize-space($refElem/@ids)), 'number($refid = .) * position() - 1'))"/>
    </xsl:call-template>
  </xsl:template>

  <!-- ******************************************************************** -->
  <!-- ******************************************************************** -->

  <!-- Report unknown tags -->
  <xsl:template
      match="*">
    <xsl:message>
      <xsl:text>`</xsl:text>
      <xsl:value-of
	  select="name(.)"/>
      <xsl:text>' encountered</xsl:text>
      <xsl:if
	  test="parent::*">
	<xsl:text> in `</xsl:text>
	<xsl:value-of
	    select="name(parent::*)"/>
	<xsl:text>'</xsl:text>
      </xsl:if>
      <xsl:text>, but no template matches.</xsl:text>
    </xsl:message>
  </xsl:template>

</xsl:stylesheet>

<!-- ********************************************************************** -->
<!-- ********************************************************************** -->
<!-- ********************************************************************** -->
<!-- POD manual page

=head1 NAME

xml2rst-nopy.xsl - An XSLT script to convert Docutils XML to reStructuredText

=head1 SYNOPSIS

  xsltproc docutils.xml xml2rst-nopy.xsl

=head1 NOTE

Today B<xml2rst-nopy.xsl> is not the recommended way to use
B<xml2rst>. The preferred way is to install B<lxml> and use
B<xml2rst.py> instead.

=head1 DESCRIPTION

B<xml2rst-nopy.xsl> is an XSLT script to convert Docutils XML to
reStructuredText. You can use your favorite XSLT processor supporting
EXSLT (e.g. xsltproc from the Gnome project) to generate
reStructuredText from the Docutils intermediate XML representation.
Its main use is to generate reStructuredText from some other format
where a converter to Docutils XML already exists.

=head2 Options

The following options are supported. They are XSLT parameters for the
whole script and must be given to the XSLT processor by the respective
option (xsltproc: B<- -param> or B<- -stringparam>).

=over 4

=item adornment

Configures title markup to use so different styles can be requested
easily.

The value of the parameter must be a string made up of a sequence of
character pairs. The first character of a pair is C<o> (overline) or
C<u> (underline) and the second character is the character to use for
the markup.

The first and the second character pair is used for document title and
subtitle, the following pairs are used for section titles where the
third pair is used for the top level section title.

Defaults to C<o=o-u=u-u~u:u.u`>.

=item fold

Configures whether long text lines in paragraphs should be folded and
to which length. This option is for input not coming from reST which
may have no internal line feeds in plain text strings.

If folding is enabled text strings not in a line feed preserving
context are first white-space normalized and then broken according to
the folding rules. Folding rules put out the first word and continue
to do so with the following words unless the next word would cross
the folding boundary. Words are delimited by white-space.

Defaults to C<0>, i.e. no folding.

=back

=head2 Unsupported features

It is generally not possible to create an exact reproduction of an
original reStructuredText source from an intermediate XML file. The
reason is that Docutils transports pretty much but not all information
of the original source into the XML. Also the sequence is changed
sometimes.

However, the coverage of Docutils features of B<xml2rst-nopy.xsl> is
pretty good. A few minor features are not supported:

=over 4

=item * Fully minimized style for literal blocks

=item * Substitution references for C<replace::> substitutions

=item * Counting roman numbers in enumerated lists

=item * Special table types like C<list-table::> and C<csv-table::>

=item * Custom role definitions

=back

=head1 INSTALLATION

Installation is not necessary. Just use the file as downloaded with
your favorite XSLT processor supporting EXSLT. For instance you can
use B<xsltproc> from the Gnome project.

=head1 AUTHOR

Stefan Merten <smerten@oekonux.de> based on works by David Priest.

=head1 LICENSE

This program is licensed under the terms of the GPL. See

	http://www.gnu.org/licenses/gpl.txt

=head1 AVAILABILTY

See

	http://www.merten-home.de/FreeSoftware/xml2rst/

=cut

-->