summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/xmltex/base/xmltex.tex
blob: 70a41b40e19ae5c7103305910f5304b1855f05b5 (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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% xmltex.tex

%% Copyright 2000 David Carlisle, NAG Ltd.
%% re-released by Sebastian Rahtz June 2002
%% This file is distributed under the LaTeX Project Public License
%% (LPPL) as found at http://www.latex-project.org/lppl.txt
%% Either version 1.0, or at your option, any later version.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\catcode`\{=1 \catcode`\}=2 \catcode`\@=11 \catcode`\#=6

\gdef\XML@tempa#1: #2.tex,v #3 #4 #5 #6 #7${
  \def\xmltexversion{#4 v#3 (#6)}}

\XML@tempa
$Id: xmltex.tex,v 1.9 2002/06/25 rahtz Exp $

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initial setup so that xmltex independent of any existing format
% even if normally built on top of latex

\endlinechar-1

\catcode`\&=4
\catcode`\^=7
\catcode`\_13
\catcode`\|=14 %
\catcode`\~=13 %

\ifx\count@\@undefined
  \countdef\count@200 % fingers crossed
  \countdef\XML@ns@count201\relax
  \expandafter\def\csname newcount\endcsname#1{}
  \expandafter\def\csname newtoks\endcsname#1{}
  \toksdef\toks@200\relax
  \toksdef\XML@catalogue201\relax
  \toksdef\XML@attribute@toks202\relax
  \def\maxdimen{16383.99999pt}
  \def\space{ }
  \chardef\active13 %
  \countdef\z@0 %
\fi
\ifx\@tempcnta\@undefined
  \countdef\@tempcnta202 % fingers crossed
  \countdef\@tempcntb203 % fingers crossed
  \edef\@spaces{\space\space\space\space}
  \begingroup
    \catcode`\>12
    \gdef\strip@prefix#1>{}
    \endgroup
\fi


% set up 7bit range
\count@0
\catcode0=13
\gdef\XML@tempa{
 \begingroup
   \uccode0\count@
  \uppercase{\endgroup
    \edef^^@{
      \ifnum\catcode\count@=11 %
        \noexpand\utfeightay\else\noexpand\utfeightax\fi
      \noexpand^^@}
    \expandafter\edef\csname 8:\string^^@\endcsname{\string^^@}}
 \ifnum\count@<127\advance\count@1 \expandafter\XML@tempa\fi}
\XML@tempa
\catcode0=9

\begingroup
\catcode`\^^M=\active\gdef^^M{\utfeightay^^M}
\catcode`\^=\active\gdef^{\utfeightay^}
\catcode`\_=\active\gdef_{\utfeightay_}
\catcode`\~=\active\gdef~{\utfeightay~}
\catcode`\%=\active\gdef%{\utfeightay%}
\catcode`\$=\active\gdef${\utfeightay$}
\catcode`\#=\active\gdef#{\utfeightay#}
\catcode`\(=1
\catcode`\)=2
\catcode`\{=\active\gdef{(\utfeightay{)
\catcode`\}=\active\gdef}(\utfeightay})
\catcode`\/0 \catcode`\\=\active /gdef\(/utfeightay\)
/endgroup



\long\def\@gobble#1{}
\def\@empty{}

\ifx\@@end\@undefined
  \let\@@end\end
\fi

\ifx\nfss@catcodes\@undefined
 \def\nfss@catcodes{
  \catcode`\\0
  \catcode`\{1
  \catcode`\}2
  \catcode`\%14
  \catcode`\@11
  \catcode`\#6
  \catcode`\"12
  \catcode`\'12
  \catcode`\<12
  \catcode`\=12
  \catcode`\>12
  \catcode`\^7 %
  }
\fi

% This is standard in LaTeX.
\ifx\zap@space\@undefined
\def\zap@space#1 {#1}
\fi

\newlinechar`\^^J

\ifx\@@input\@undefined
\let\@@input\input
\def\input#1{\@@input#1 }
\fi
\chardef\active=13
\chardef\@ne=1

%
\ifx\IfFileExists\@undefined
  \def\IfFileExists#1#2#3{\def\@filef@und{#1 }#2}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\immediate\write20{xmltex version: \xmltexversion}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\let\tabcellsep&

\def\afterfi#1\fi{\fi#1}


% set catcodes of low chars to 12 and hight 10 13.
\gdef\XML@tempa#1#2{
  \catcode\count@#2\relax
  \ifnum\count@<#1\relax
     \advance\count@\@ne
     \afterfi
     \XML@tempa{#1}{#2}
  \fi
  }
\count@0\relax
\XML@tempa{`\^^K}{12}

\count@127\relax
\XML@tempa{255}{13}


\def\XML@catcodes{
% white
  \catcode`\ \active
  \catcode`\^^M\active
  \catcode`\^^I\active
% xml
  \catcode`\<\active
  \catcode`\>\active
  \catcode`\:\active
  \catcode`\[\active
  \catcode`\]\active
  \catcode`\%\active
  \catcode`\&\active
  \catcode`\"\active
  \catcode`\'\active
  \catcode`\=\active
% tex
  \catcode`\/\active
  \catcode`\!\active
  \catcode`\?\active
  \catcode`\-\active
  \catcode`\$\active
  \catcode`\{\active
  \catcode`\}\active
  \catcode`\#\active
  \catcode`\_\active
  \catcode`\\\active
  \catcode`\~\active\def~{\utfeightay~}
% and these are not catcodes
  \let\XML@ns@a@\XML@ns@a@xml
  \let\XML@ns\XML@ns@xml
}


\catcode`\/\active
\catcode`\!\active
\catcode`\?\active
\catcode`\"\active
\catcode`\'\active
\catcode`\<\active
\catcode`\>\active
\catcode`\&\active
\catcode`\_\active
\catcode`\ 10
\catcode`\^^M\active



%% half baked bom and utf-16 support
\catcode`^^ff\active
\catcode`^^fe\active
\catcode0=9

% do this also in everyjob incase a <?xmltex dump?> has turned it iff.
\def^^ff^^fe{
  \def\XML@thisencoding{utf-16}}
\def^^fe^^ff{
  \def\XML@thisencoding{utf-16}}

%%%%%%%%%%%%%%%%%%%%%%%%%%


%% warnings
\def\XML@warnNI#1{
  {\let\protect\string\utfeight@protect@typeout\message{^^J#1}}}
\def\XML@warn#1{
  {\let\protect\string\utfeight@protect@typeout\message{^^J\XML@w@#1}}}
\let\XML@trace@warn\XML@warn
\let\XML@trace@warnNI\XML@warnNI
\let\XML@trace@warnE\message

\let\XML@w@\@empty

% quoted literals
% " or '
% #1 command to call, gets quoted string as #1
% #2 " or '
% " and ' assumed active
\def\XML@quoted#1#2{
   \ifx#2"\expandafter\XML@qq
   \else\ifx#2'\expandafter\expandafter\expandafter\XML@q
   \else
     \ERROR#2
   \fi
   \fi
   #1}


\def\XML@qq#1#2"{#1{#2}}
\def\XML@q#1#2'{#1{#2}}


%%%%%%%%%%%%%%%%%

% < handler

% / ! ? assumed active
\def\XML@lt@markup#1{
  \catcode`\^^I=10 %
  \catcode`\^^M=10 %
  \catcode`\ =10 %
  \ifx/#1\XML@getend
  \else\ifx!#1\XML@getdecl
  \else\ifx?#1\XML@getpi
  \else\XML@getname#1\@}

\def\XML@first@lt{
  \let<\XML@lt@markup
  \let^^ff\@undefined
  \let^^fe\@undefined
  \XML@lt@markup}


%% reset catcodes
\def\XML@reset{
  \nfss@catcodes
  \catcode`\$3
  \catcode`\&4
  \catcode`\^7
  \catcode`\_8
  \catcode`\:12
  \catcode`\!12
  \catcode`\=12
  \catcode`\=12
  \catcode`\|12
  \catcode`\ =10 
  \catcode`\~\active\def~{\nobreakspace{}}
% catcodes?
  \let\XML@ns@a@\XML@ns@a@tex
  \let\XML@ns\XML@ns@tex}


%% begin tag
\begingroup
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
%
% #1 first char of name
% grab the name into an xdef so that derminating string can
% be any of whitespace / or >
\gdef\XML@getname#1\@{
\fi\fi\fi
\begingroup
\catcode`\^^M\active
\catcode`\^^I\active
\catcode`\ \active
\def {\iffalse{\fi}\XML@getname@}
\let^^M %
\let^^I %
\def/{\iffalse{\fi}\XML@getname@/}
\def>{\iffalse{\fi}\XML@getname@>}
\unrestored@protected@xdef\XML@tempa{\iffalse}\fi#1}

\endgroup

% finish the special group for the name xdef, start an XML@begingroup
% for the element, and begin processing any attributes.
\def\XML@getname@{
  \endgroup
  \XML@begingroup
  \edef\XML@w@{ \XML@w@}
  \let\begintag\XML@tempa
  \let\XML@parent\XML@this@element
  \XML@attribute@toks{}
  \XML@getattrib}

% elements put in an \XML@begingroup which is a not a group at the
% outer level to save save stack, but turns itself into a group
% for nexted elements.
\def\XML@begingroup{
 \def\XML@begingroup{
   \begingroup
   \let\XML@begingroup\begingroup
   \let\XML@endgroup\endgroup}}

\let\XML@endgroup\@@end



%% attributes

% #1 first letter of attribute name, or / or > to finish.
\def\XML@getattrib#1{
  \ifx#1/
    \expandafter\XML@endempty
  \else
  \ifx#1>
     \expandafter\expandafter\expandafter\XML@startelement
  \else
    \XML@getattrib@a#1
  \fi
  \fi}

\let\XML@@getattrib\XML@getattrib

\begingroup
\catcode`\:\active
\catcode`\<12
\catcode`\>12

% restore normal XML whitespace regime
% namespace check element name (has to be done after attribute handling)
% trace the element start then `do' the element which might be
% noop, execute package code, or grab, depending.

\gdef\XML@startelement{
  \XML@default@attributes
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \XML@ns\begintag
  \edef\XML@this@element{
    \csname XMLNS@\XML@this@prefix\expandafter\endcsname\noexpand:
                          \XML@this@local}
  \XML@trace@warn{<\XML@this@element}
  \XML@checkknown
  \XML@attrib@trace
  \XML@doelement}

\endgroup

% This is a noop if tracing turned off, otherwise loop through
% attlist with a typeout on each.
\def\XML@attrib@trace{
  \begingroup
    \let\XML@doattribute\XML@doattribute@warn
    \def\XMLNS@{0}
    \utfeight@protect@typeout
    \the\XML@attribute@toks
    \XML@trace@warnE{ >}
  \endgroup}

% execute package code for an element start.
\def\XML@doelement{
  \csname
    E:\XML@this@element
  \endcsname
}

\begingroup
\catcode`\:\active
\uccode`\~`\^^I%

% grab element content into a token register.
% has to pass through xdef to normalise encodings.
\uppercase{
\gdef\XML@grabelement{
\catcode`\ \active\catcode`\^^M\active\catcode`\^^I\active
  \global\XMLgrabtoks\expandafter{
    \the\expandafter\XMLgrabtoks
      \expandafter<\XML@this@element~}
% check if I can switch this just once, where grabelement switched.
   \begingroup
   \let\XML@doattribute\XML@grabattribute
   \def\XMLNS@{0}
   \expandafter\let\csname XMLNS@0\endcsname\XMLNS@
   \the\XML@attribute@toks
   \endgroup
%
\catcode`\ \active\catcode`\^^M\active\catcode`\^^I\active
  \global\XMLgrabtoks\expandafter{
    \the\XMLgrabtoks
    >}
   \XMLgrab@}
}

% #1  namespace
% #2  local name
% #3  value

\catcode`\=\active
\gdef\XML@grabattribute#1#2#3{
  \protected@xdef\XML@tempa{\csname XMLNS@#1\endcsname:#2}
  \global\XMLgrabtoks\expandafter{
    \the\expandafter\XMLgrabtoks
    \XML@tempa="#3" }}
\endgroup

% #1 should be empty, between the / and the >
% probably should put some internal form here rather than literally adding
% end tag to be reparsed, but this simplifies grab code.
\def\XML@endempty#1>{
  \expandafter\XML@startelement
  \expandafter<\expandafter/\begintag>}


%% check we know what to do with an element
\def\XML@checkknown{
  \expandafter\ifx
% catcode :?    \csname E:\XML@this@element\endcsname
    \csname E:\csname XMLNS@\XML@this@prefix\endcsname:\XML@this@local\endcsname
    \relax
   \let\XML@use\@empty
   \ifnum0=\csname XMLNS@\XML@this@prefix\endcsname
     \let\XML@NAME\XML@this@local
% prerun catalogue in this case which might coerce element into a new namespace
     \the\XML@catalogue
   \else
     \edef\XML@NAMESPACE{\csname XMLNS@\XML@this@prefix\endcsname}
     \fi
   \let\XML@NAME\relax
   \the\XML@catalogue
   \inputonce\XML@use
   \expandafter\ifx\csname E:\csname
       XMLNS@\XML@this@prefix\endcsname:\XML@this@local\endcsname\relax
     \XML@trace@warnE{Undefined}
   \fi
 \fi}



%% end tag

\def\XML@getend#1\@#2>{
  \fi
\catcode`\ \active
% removed by Peter Housel housel@acm.org 2000/09/06 
% \catcode`\^^M\active\catcode`\^^I\active
  \XML@getend@a#2 \@}

\begingroup
\catcode`\/=12
\catcode`\<=12
\catcode`\>=12
\catcode`\:=12

% namespace normalise element name in end tag.
\gdef\XML@getend@a#1 #2\@{
\catcode`\ \active\catcode`\^^M\active\catcode`\^^I\active
  \def\endtag{#1}
  \XML@ns\endtag
  \XML@trace@warn{</\csname XMLNS@\XML@this@prefix\endcsname:\XML@this@local>}
  \XML@doend
}

% execute package code for element end.
\gdef\XML@doend{
  \csname
    E/:\csname XMLNS@\XML@this@prefix\endcsname:\XML@this@local
  \endcsname
  \XML@endgroup
% added by Peter Housel housel@acm.org 2000/09/06 
 \catcode`\^^M\active \catcode`\^^I\active \catcode`\ \active}
\endgroup

% flag children: put \@empty after every child element
% used by \xmltextwochildren and friends

% need active and non active : and / so...
\begingroup
\catcode`\:\active
\uccode`\*=`\:
\uccode`\.=`\/
\uccode`\a\z@
\uccode`\b\z@
\uccode`\c\z@
\uccode`\d\z@
\uccode`\e\z@
\uccode`\n\z@
\uccode`\r\z@
\uccode`\o\z@
\uccode`\t\z@

\uppercase{
\gdef\XML@grabend{
  \ifx\XML@this@level\XML@w@
% end inner group to restore \XML@doelement and friends
    \endgroup
    \XML@trace@warn{Grabbed content}
    \csname
      E.*\csname XMLNS@\XML@this@prefix\endcsname
        *\XML@this@local
    \expandafter\endcsname\expandafter{
      \the\XMLgrabtoks}
    \XML@trace@warn{End grabbed content}
    \XML@endgroup
% \XMLstring usage means catcode restoring varies
% must fix this one day, for now use ifnum avoidance
\ifnum\catcode`\^^M=10
\catcode`\ \active\catcode`\^^M\active\catcode`\^^I\active
\fi
  \else
    \xdef\XML@tempa{\noexpand<\noexpand/
      \csname XMLNS@\XML@this@prefix\expandafter\endcsname\noexpand:
             \XML@this@local
    \noexpand>
    \ifx\XML@next@level\XML@w@\noexpand\@empty\fi}
    \global\XMLgrabtoks\expandafter{
      \the\expandafter\XMLgrabtoks
      \XML@tempa}
    \XML@endgroup
  \expandafter
    \XMLgrab@
  \fi}}
\endgroup
%%%

% syntax for these will probably change.
\def\xmltexfirstchild#1\@empty#2\@{
  #1}
\def\xmltextwochildren#1#2#3\@empty#4\@empty{
  #1{#3}#2{#4}}
\def\xmltexthreechildren#1#2#3#4\@empty#5\@empty#6\@empty{
  #1{#4}#2{#5}#3{#6}}
\def\xmltexforall#1#2{
  \xmltexf@rall#1#2< >\@empty}

\def\xmltexf@rall#1#2<#3 #4>#5\@empty{
  \ifx\relax#3\relax
  \else
  \def\xml@name{#3}#1{<#3 #4>#5}
  \expandafter\xmltexf@rall\expandafter#1
  \fi}

% #1  entity name (or tex macro holding same, eg from an attrbute)
% #2 #3 inserted before each compontent
% so #2{GIF}#3{xxx/yyy.gif}
\begingroup
\catcode`\&12
\catcode`\+12

\gdef\NDATAEntity#1{
  \expandafter\expandafter\expandafter
  \XML@ndataentity\csname+&#1\endcsname}

\gdef\XML@ndataentity#1#2#3#4{
  #3{#1}#4{#2}}

\endgroup  


%%%%%%%

% this relies on = being catcode 13.
\begingroup
\catcode`\=\active

\gdef\XML@getattrib@a#1\fi\fi#2={
  \fi\fi
   \XML@set@this@attribute#1#2 \@
   \XML@quoted\XML@attribval}

\endgroup

%% remove trailing space from `foo ='
\def\XML@set@this@attribute#1 #2\@{
% should probably do an edef at this point, and optimise
% later code
  \def\XML@this@attribute{#1}}

% #1 = attribute value
\def\XML@attribval#1{
% allow for arbitrary catcodes.
  \xdef\XML@tempa{\expandafter\strip@prefix\meaning\XML@this@attribute}
  \ifx\XML@tempa\XML@ns@decl
    \XML@ns@uri{}{#1}
   \else
     \XML@ns\XML@this@attribute
% catcode avoidance
     \edef\XML@this@prefix{\expandafter\strip@prefix\meaning\XML@this@prefix}
     \ifx\XML@this@prefix\XML@ns@decl
       \XML@ns@uri\XML@this@local{#1}
     \else
      \begingroup
% check if this still needed with protect setting?
%       \def"{\noexpand&quot;}
      \utfeight@protect@internal
       \xdef\XML@tempa{
          \the\XML@attribute@toks
          \noexpand\XML@doattribute{\XML@this@prefix}{\XML@this@local}{#1}}
       \endgroup
       \XML@attribute@toks\expandafter{\XML@tempa}
      \fi
    \fi
  \XML@getattrib}


%% activate attributes

% this seems over complicated and perhaps I should re-implement.
% currently tries to avoid making a csname for each attribute.
% declaration of attributes provides a normal tex command name
% to access the value in the element code, could  have instead
% just had declaration of attribute name and default and used something
% like \attributevalue{html:href} but would need to work out a way
% of resolving prefixes at definition time if this was embedded in
% the element code. (The prefix such as `html' used in the definition
% file isn't known by the time the code is run.) 
\def\XML@doattribute@warn#1#2#3{
  \XML@trace@warn{\@spaces\csname XMLNS@#1\endcsname:#2 = \string"#3\string"}}

% #1 element specific attribute defaults, first token is
% macro for namespace-global attributes (hence \expandafter)
\def\XML@setattributes#1{
  \let\XMLNS@@\XMLNS@
   \def\XMLNS@{0}
   \the\expandafter\XML@attribute@toks#1\relax\relax
   \let\XMLNS@\XMLNS@@}

% #1 prefix
% #2 local name
% #3 value
\def\XML@doattribute#1#2#3{
  \xdef\XML@tempa##1{\noexpand##1{
       \noexpand\XML@attrib\csname XMLNS@#1\endcsname:#2\relax}}
  \XML@tempa\XML@attrib@x{#3}
  \XML@tempa\XML@attrib@y}

% #1 \XML@attrib qname\relax
% #2 value given in document instance
\def\XML@attrib@x#1#2{
    \gdef\XML@tempb##1#1##2##3##4\relax\relax{
    \def##2{#2}
    ##1##4\relax\relax}}

% #1 \XML@attrib qname\relax
% #2 original attribute defaults
\def\XML@attrib@y#1#2\relax\relax{
  \XML@tempb#2#1\XML@temp@l{6}\relax\relax}


% if default is inherit, set it to \relax the first time, otherwise
% let whatever value it has drop through. Note this is inheritance of the
% tex csname declared as the internal access, not of the xml attribute name. 
% #1 = junk
% #2 = tex csname
% #3 = attribute default (should be encoding neutral: not normalised)
\def\XML@attrib#1\relax#2#3{
  \ifx\inherit#3\relax% #3 might be empty
    \ifx#2\@undefined
      \def#2{\relax}
    \fi
  \else
    \def#2{#3}
  \fi}

% any distinguishing value would do...
\let\inherit\XML@attrib


\newtoks\XML@attribute@toks

%%%%% namespace declarations

\newcount\XML@ns@count

% need to protect against active chars
\def\XML@ns@decl{xmlns}
\edef\XML@ns@decl{\expandafter\strip@prefix\meaning\XML@ns@decl}


% #1 prefix (or empty)
% #2 uri
% globally allocate number to uri if new
% locally alocate prefix to number
% globally allocate number as a prefix (canonical form)
\def\XML@ns@uri#1#2{
  \utfeight@protect@chars
  \XML@ns@alloc{#2}
  \expandafter\edef\csname XMLNS@#1\endcsname
                      {\csname XML:#2\endcsname}
  \XML@trace@warn{xmlns:#1 = \csname XMLNS@#1\endcsname}
  \unprotect@utfeight
 }

% and the same without any prefix
% always use in scope of utfeight protect
% #1 uri
\def\XML@ns@alloc#1{
  \expandafter\ifx\csname XML:#1\endcsname\relax
    \global\advance\XML@ns@count\@ne
    \expandafter\xdef\csname XML:#1\endcsname{\the\XML@ns@count}
    \global\expandafter\let\csname A:\the\XML@ns@count\endcsname\@empty
    \XML@trace@warnNI{URI: \csname XML:#1\endcsname\space = #1}
    \expandafter\xdef\csname XMLNS@\the\XML@ns@count\endcsname
                      {\the\XML@ns@count}
  \fi}

% and version for xmt files
\let\DeclareNamespace\XML@ns@uri

%% namespace support

%% : is active in xml state but inactive in tex state, so need to do
%% this twice, grrr...
\begingroup
\catcode`\:\active

% #1 = qname to be split on :
\gdef\XML@ns@xml#1{
  \expandafter\XML@ns@a@xml#1:\@:\\}

% #1 = prefix (or empty)
% #2 = local name or \@ if no prefix
\gdef\XML@ns@a@xml#1:#2:#3\\{
  \ifx\@#2
    \XML@ns@b{}{#1}
  \else
     \XML@ns@b{#1}{#2}
  \fi}
\endgroup

% same with inactive :
% #1 = qname to be split on :
\def\XML@ns@tex#1{
  \expandafter\XML@ns@a@tex#1:\@:\\}

% #1 = prefix (or empty)
% #2 = local name or \@ if no prefix
\def\XML@ns@a@tex#1:#2:#3\\{
  \ifx\@#2
    \XML@ns@b{}{#1}
  \else
     \XML@ns@b{#1}{#2}
  \fi}

\let\XML@ns@a@\XML@ns@a@tex
\let\XML@ns\XML@ns@tex

%%%%%%%%%%%%%%%%%%%%

% nullnamespace 
\expandafter\def\csname XML:\endcsname{0}
\expandafter\let\csname A:0\endcsname\@empty
\def\XMLNS@{0}
\expandafter\gdef\csname XMLNS@0\endcsname{0}

% xml namespace 
\let\utfeightax\string
\expandafter\def\csname XML:http://www.w3.org/1998/xml\endcsname{1}
\expandafter\let\csname A:1\endcsname\@empty
\def\XMLNS@xml{1}
\expandafter\gdef\csname XMLNS@1\endcsname{1}
\XML@ns@count1

% #1 = prefix or empty
% #2 = local name
\def\XML@ns@b#1#2{
  \begingroup
  \utfeight@protect@chars
  \xdef\XML@tempa{#1}
  \xdef\XML@tempb{#2}
  \endgroup
  \let\XML@this@prefix\XML@tempa
  \let\XML@this@local\XML@tempb
  }

%%%%%
% pi
\begingroup
\catcode`\?\active
\catcode`\ \active
\catcode`\^^M\active
\catcode`\^^I\active

\gdef\XML@getpi#1\@{
\fi\fi\fi
\begingroup
\utfeight@protect@chars
\catcode`\ \active
\catcode`\^^M\active
\catcode`\^^I\active
\def?{\endcsname?}
\let \endcsname
\let^^M\endcsname
\let^^I\endcsname
\expandafter\XML@getpi@\csname
Q:}

\endgroup

\def\XML@getpi@#1{
  \endgroup
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \ifx#1\@undefined
    \expandafter\XML@getpi@x
  \fi
  #1}

\def\XML@getpi@x#1#2?>{
  \XML@dopi{Undefined}{}}

% currently ? not reset by XML@reset
\expandafter\def \csname Q:xmltex\endcsname{
  \begingroup
  \XML@reset
  \catcode`\>\active
  \XML@xmltexpi}


\gdef\XML@xmltexpi#1?>{
  \endgroup
  \XML@dopi{xmltex}{#1}}



% #1 = piname or `piname Undefined'
\def\XML@dopi#1#2{
  \XML@trace@warn{\string<?#1?>}
  #2}

\begingroup
\catcode`\^^I\active

\gdef\XML@grabpi#1#2{
  \global\XMLgrabtoks\expandafter{
  \the\XMLgrabtoks<?#1^^I#2?>}
  \XMLgrab@}

\endgroup

 

%% XML and declarations
% only care about encoding. Ignore version and standalone.

% #1 content
% #2 end of test code
\begingroup
\catcode`\=\active

\expandafter\gdef \csname Q:xml\endcsname{
  \catcode`\^^M10
  \catcode`\^^I10
  \catcode`\ 10 %
  \XML@xmldecl}

\gdef\XML@xmldecl#1?>{
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \XML@encoding#1 e="utf-8"\relax}


% actually encoding supposed to be after version if it comes at all
% so I could simplify this and get rid of the loop.
\gdef\XML@encoding#1 #2{
  \if\noexpand#2e
    \expandafter\XML@encoding@aux
  \else
    \expandafter\XML@encoding
  \fi}

\gdef\XML@encoding@aux#1={
  \XML@quoted\XML@setenc}

\endgroup

% do nothing if newly specied encoding is same as old one
% #1 is name of encoding (upper or lower case, and dubious catcodes
% #2 is junk

\def\XML@setenc#1#2\relax{
  \lowercase{\gdef\XML@tempa{#1}}
  \xdef\XML@tempa{\expandafter\strip@prefix\meaning\XML@tempa}
  \ifx\XML@tempa\XML@thisencoding
  \else
    \ifx\XML@utfeight\XML@tempa
      \XML@setutfeight
    \else
      \let\XML@thisencoding\XML@tempa
      \XML@trace@warnNI{Encoding = \XML@thisencoding}
      \begingroup
      \XML@reset\input{\XML@thisencoding.xmt}
      \endgroup
    \fi
  \fi}

% public version of same
\def\FileEncoding#1{
  \XML@setenc{#1}\relax}

% catcode neutral lowercase utf-8

\def\XML@utfeight{utf-8}
\edef\XML@utfeight{\expandafter\strip@prefix\meaning\XML@utfeight}

%% internalise utf8 encoding as needed before every file include.
\begingroup
\catcode`\~13
\catcode`\"12
\catcode`\<12

\gdef\utfeightloop{
  \uccode`\~\count@
  \expandafter\uppercase\XML@tempa
  \advance\count@\@ne
  \ifnum\count@<\@tempcnta
  \expandafter\utfeightloop
  \fi}

\gdef\XML@setutfeight{
  \ifx\XML@utfeight\XML@thisencoding
  \else
    \let\XML@thisencoding\XML@utfeight
    \XML@trace@warnNI{Encoding = \XML@thisencoding}
%
    \begingroup
%
    \count@"C2
    \@tempcnta"E0
    \gdef\XML@tempa{{
      \xdef~####1{\noexpand\utfeightb\string~####1}}}
    \utfeightloop
%
    \count@"E0
    \@tempcnta"F0
    \gdef\XML@tempa{{
      \xdef~####1####2{\noexpand\utfeightc\string~####1####2}}}
    \utfeightloop
%
    \@tempcnta"F4
    \gdef\XML@tempa{{
      \xdef~####1####2####3{\noexpand\utfeightd\string~####1####2####3}}}
    \utfeightloop
%
    \endgroup
  \fi}

\endgroup


\def\xmlinput#1{
 \IfFileExists{#1}
  {\expandafter\XML@xmlinput\expandafter
    \XML@setenc\expandafter{\XML@thisencoding}\relax
  }{\XML@warn{No file: #1}}}

\def\XML@xmlinput{
  \def^^ff^^fe{\XML@setenc{utf-16}\relax}
  \def^^fe^^ff{\XML@setenc{utf-16}\relax}
% in principle a parsed entity might just be text with no markup
% but the utf16 is so broken anyway don't worry about that.
  \let<\XML@first@lt
  \XML@setutfeight
  \@@input\@filef@und\relax}

%%%%%%%%%%%%%

%% declarations
% made safe against active chars.
% #1 = rest of if test
% #2#3 = first two characters after <!
\def\XML@getdecl#1\@#2#3{
\fi\fi
  \if-\noexpand#2\XML@comment     %   --
  \else\if N\noexpand#3\XML@entity%   EN TITY
  \else\if L\noexpand#3\XML@dec@e%    EL EMENT
  \else\if A\noexpand#2\XML@dec@a%    AT TLIST
  \else\if D\noexpand#2\XML@doctype%  DO CTYPE
  \else\if C\noexpand#3\XML@cdata%    [C DATA 
  \else        \XML@dec@n%            NO TATION
%                                      could also pick up [IGNORE/[INCLUDE
%                                      but they not allowed in internal subset.
@}

%% Just skip element declarations
% #1 = rest of \if
% #2 ELEMENT declaration
\def\XML@dec@e#1@#2>{
  \fi\fi\fi
  \XML@checkend@subset}


% attribute declarations
% #1 = rest of if test + TLIST
% #2 = element name
\def\XML@dec@a#1 #2 {
  \fi\fi\fi\fi
  \protected@xdef\XML@tempa{#2}
  \XML@dec@a@x}

\gdef\XML@dec@a@x#1 #2{
  \protected@xdef\XML@tempb{#1}
   \if(\noexpand#2
     \begingroup
     \catcode`\(\active
     \expandafter\XML@dec@a@brack
   \else
      \expandafter\XML@dec@a@type
   \fi}

\begingroup
\catcode`\(\active

% #1 = enumerated attribute type tokens, up to )
\gdef\XML@dec@a@brack#1){
  \endgroup
  \XML@dec@a@hash}

\endgroup

% #1 = junk up to next space token
\def\XML@dec@a@type#1 {
  \XML@dec@a@hash}

\begingroup
\catcode`\$=\catcode`\#
\catcode`\#=12

% #1 = nextchar after space, if it is # step to next space
% otherwise look for possible " or ' or >
\gdef\XML@dec@a@hash$1{
  \if\noexpand$1#
    \expandafter\XML@dec@a@type
  \else
    \ifx$1>
      \let\ERROR\@undefined
      \expandafter\expandafter\expandafter\XML@checkend@subset
    \else
      \let\ERROR\XML@dec@a@nodef
      \XML@dec@a@def$1
    \fi
  \fi}


\endgroup

\gdef\XML@dec@a@nodef#1\fi\fi#2{
  \fi\fi
  \XML@dec@a@x#1}


\def\XML@dec@a@def#1\fi\fi{
 \fi\fi
  \XML@quoted\XML@dec@a@default#1}

\def\XML@dec@a@default#1#2{
  \XML@warn{Default: \XML@tempa\space\XML@tempb="#1"}
  \ifx\XML@default@attributes\relax
    \let\XML@default@attributes\@empty
  \fi
  \toks@\expandafter{\XML@default@attributes}
  \protected@xdef\XML@default@attributes{
    \the\toks@\noexpand\XML@add@attrib{\XML@tempa}{\XML@tempb}{#1}}
% reusing this wastes some tests but only done in local subset
  \XML@dec@a@hash#2
  }


\let\XML@default@attributes\relax

% this comparison is encoding normalised, but namespace unaware, grr.
\def\XML@add@attrib#1#2#3{
  \gdef\XML@tempa{#1}
  \ifx\XML@tempa\begintag
   \def\XML@this@attribute{#2}
% stop  getattrib looking for nexted xml syntax attribute setting.
    \let\XML@getattrib\relax
    \XML@attribval{#3}
    \let\XML@getattrib\XML@@getattrib
  \fi}

%% comment
%  - is active
% #1 = rest of if test
% #2 = comment text
\begingroup
\catcode`\-\active
\uppercase{\endgroup
\def\XML@comment#1@#2-->}{
  \fi
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \XML@trace@warn{\string<!-- -->}
  \XML@comment@}

\def\XML@comment@{
  \XML@checkend@subset}

\def\XML@grabcomment@{
  \XMLgrab@}

%% entity defs

% #1 = rest of \if test + TITY
% #2 = % or entity name
\begingroup
\catcode`\&=12
\catcode`\%=13

\gdef\XML@entity#1 #2 {
  \fi\fi
  \ifx%#2
  \def\XML@input{
    \ifx\XML@use\XML@SYSTEM\expandafter\@gobble\else
      \noexpand\inputonce\fi}
  \expandafter\XML@p@ent
  \else
  \def\XML@input{\noexpand\xmlinput}
  {\utfeight@protect@chars\xdef\XML@ename{&#2}}
  \XML@trace@warn{\XML@ename; = }
  \expandafter\XML@ent
   \fi}

\endgroup

% input some file at most once (and ignore arguments that
% expand to empty)
% done in a local group
\def\inputonce#1{
  \expandafter\ifx\csname xmt:#1\endcsname\relax
  \global\expandafter\let\csname xmt:#1\endcsname\@ne
  \begingroup
  \XML@reset
% package files should have their own namespace declarations
% don't want to inherit from some random point when file is loaded.
% in principle should clear all prefix assignments in local scope
% but I don't currently maintain a list of those, and not needed as long
% as package files declare all prefixes used. But do set the default
% namespace back to the null namespace.
%
% should force utf-8 as well.
  \def\XMLNS@{0}
  \input{#1}
  \endgroup
  \fi}

% ignore include of empty filename
\expandafter\let\csname xmt:\endcsname\@ne

% \noexpand protect against active ascii
\begingroup
\catcode`\%12 
\gdef\XML@p@ent#1 #2{
  {\utfeight@protect@chars\xdef\XML@ename{%#1}}
  \XML@trace@warn{\XML@ename; = }
  \if\noexpand#2P\XML@E@public
  \else\if\noexpand#2S\XML@E@system
   \else\XML@E@internal#2}
\endgroup

% #1 = next char( P or S for external entities)
\def\XML@ent#1{
  \if\noexpand#1P\XML@E@public
  \else\if\noexpand#1S\XML@E@system
   \else\XML@E@internal#1}


\begingroup
\catcode`\:\active

% special `prefix' that just removes following colon.
\expandafter\gdef\csname XMLNS@*\endcsname#1{}

\endgroup

\begingroup
\catcode`\:=12

%#1 = " or '
\gdef\XML@E@internal#1{
  \fi\fi
  \begingroup
  \let\XML@trace@warn\@gobble
  \let\XML@endgroup\endgroup
  \let\XML@begingroup\begingroup
% make " or ' close the grab `element'. the nameless close tag is completed
% by the > coming from the ENTITY declaration syntax.
% Using a mangled grab code is a bit complicated but it allows
% catcode 10 simplification in the normal case of element handling
% and allows characters to be correctly normalised to utf8.
  \def#1{</}
  \expandafter\def\csname
      E\string/:\endcsname{
    \afterassignment\XML@E@internal@x
    \expandafter\gdef\csname+\XML@ename\endcsname}
  \begingroup
% stop xmlns `attribute' being recognised
  \let\XML@ns@decl\relax
  \let\XML@this@local\@empty
% set up special prefix to gobble colon
  \def\XML@this@prefix{*}
% disable these as nothing will be known until namespaces reenabled
  \let\XML@checkknown\relax
  \let\XML@attrib@trace\relax
% hobble namespace code to put all name in local part.
  \def\XML@ns##1{
    \protected@edef\XML@this@local{##1}
    \def\XML@this@prefix{*}}
  \xmlgrab}

\endgroup

% expandafter away an \else clause in grabelement then check for ]
\def\XML@E@internal@x{
   \endgroup
    \aftergroup\XML@trace@warn
    \expandafter\aftergroup\csname+\XML@ename\endcsname
\aftergroup\fihack
}

% this hack has to undo the one above moarked by
%   % \XMLstring usage means catcode restoring varies
% must fix this as well one day
\def\fihack#1\fi{\expandafter\XML@checkend@subset}

% need to add (somewhere) a replacement of " to &quot; so that
% xxx='a"b"c'  doesn't end up as xxx="a"b"c"
% #1 replacement text
% #2 white space and  >
\begingroup
\uccode`\~`\#
\catcode`\~\active
\uppercase{\endgroup
\def\XML@E@internal@#1#2>{
   \expandafter\protected@xdef\csname+\XML@ename\endcsname{#1}
   \XML@trace@warn{\@spaces\string"#1\string"}
  \XML@checkend@subset}}

%% check for ]> that ends internal subset
\begingroup
\catcode`\]\active


% #1 is next token in local subset (normally < or >)
% after subset finishes % stops being markup, and the package
% relating to any external entity in the doctype is loaded.
\gdef\XML@checkend@subset{
  \catcode`\^^M10
  \catcode`\^^I10
  \catcode`\ 10 %
  \XML@checkend@subset@}

% #1 = next character
\catcode`\%\active
\gdef\XML@checkend@subset@#1#2#3#4{
  \ifx]#1
  \let\XML@w@\@empty
  \XML@trace@warn{]}
  \gdef%{\utfeightay%}
  \let\XML@checkend@subset\relax
  \expandafter\XML@loaddoctype
  \fi
  #1#2#3#4}

\endgroup

%% #1#2 just gobble ]>
\def\XML@loaddoctype#1#2{
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \ifx\XML@D@dtd\relax\else 
    \XML@trace@warn{Doctype Package: \XML@D@dtd}
    \inputonce\XML@D@dtd
  \fi}


% #1 = rest of \if test + PUBLIC
\def\XML@E@public#1 {
   \fi
   \XML@quoted\XML@E@pubid}

% #1 = FPI
\def\XML@E@pubid#1{
  \def\XML@PUBLIC{#1}
  \edef\XML@PUBLIC{\expandafter\strip@prefix\meaning\XML@PUBLIC}
  \XML@quoted\XML@E@systemid}

% #1 = rest of if test up to SYSTEM
\def\XML@E@system#1 {
   \fi\fi
   \def\XML@PUBLIC{}
   \XML@quoted\XML@E@systemid}


% #1 URL
% #2 next token, N or >
\def\XML@E@systemid#1#2{
  \def\XML@SYSTEM{#1}
  \let\XML@use\XML@SYSTEM
  \XML@trace@warn{\@spaces Public: \XML@PUBLIC}
  \XML@trace@warn{\@spaces System: \XML@SYSTEM}
  \the\XML@catalogue
  \if\noexpand#2N
   \expandafter\XML@E@ndata
  \else
    \afterfi
    \XML@E@internal@{\XML@input{\XML@use}}#2
  \fi}
  

% NDATA token terminated by > or white space
% #1 = DATA
% #2 ndata token with possible extra space
\def\XML@E@ndata#1 #2>{\XML@ndata@#2 >}

% #1 = ndata toke
% #2 = junk
\def\XML@ndata@#1 #2>{
    \XML@E@internal@{{#1}{\XML@use}}>}


%% DOCTYPE
% #1 rest of \if test + OCTYPE
% #2 document element name
% #3 P or S or [ or >
% noexpand for P and S, [ assumed active

\begingroup
\catcode`\[\active

\gdef\XML@doctype#1 #2 #3{
 \fi\fi\fi\fi\fi
  \def\documentelement{#2}
  \let\XML@D@dtd\relax
  \XML@trace@warn{Document Element: \documentelement}
  \if\noexpand#3P\XML@D@public
  \else\if\noexpand#3S\XML@D@system
  \else\ifx#3[\XML@D@internal
  \else%must be > the end
    \XML@D@empty
  @}


\gdef\XML@D@empty @{
   \fi\fi\fi}

% #1 = rest of \if test + UBLIC
\gdef\XML@D@public#1 {
   \fi
   \XML@quoted\XML@pubid}

% #1 = FPI
\gdef\XML@pubid#1{
  \def\XML@PUBLIC{#1}
  \edef\XML@PUBLIC{\expandafter\strip@prefix\meaning\XML@PUBLIC}
  \XML@quoted\XML@systemid}


% #1 = rest of \if test + YSTEM
\gdef\XML@D@system#1 {
   \fi\fi
   \def\XML@PUBLIC{}
   \XML@quoted\XML@systemid}

% #1 = URI
\gdef\XML@systemid#1{
  \protected@edef\XML@SYSTEM{#1}
  \edef\XML@SYSTEM{\expandafter\strip@prefix\meaning\XML@SYSTEM}
  \XML@trace@warn{Doctype Public: \XML@PUBLIC}
  \XML@trace@warn{Doctype System: \XML@SYSTEM}
  \let\XML@use\@empty
  \the\XML@catalogue
  \let\XML@D@dtd\XML@use
  \XML@D@internal@}

% #1 = rest of if test
\gdef\XML@D@internal#1@{
  \fi\fi\fi
  \XML@D@internal@[}

% #1 = [ for local subset or > for the end.
\catcode`\%\active
\gdef\XML@D@internal@#1{
  \ifx[#1
    \XML@trace@warn{Internal Subset[}
    \let%\XML@pcent
    \edef\XML@w@{ \XML@w@}
     \expandafter\XML@checkend@subset
  \else
      | it had better be the closing >
   \fi}

\endgroup


%%%%%%%%%%%%%%%
%% catalogue support
\newtoks\XML@catalogue

%% should rationalise this code

% #1 = FPI
% #2 = xmltex package file
\def\PUBLIC#1#2{
 \xdef\XML@tempa{#1}
 \xdef\XML@tempa{\noexpand\the\XML@catalogue\noexpand\XML@@PUBLIC
             {\expandafter\strip@prefix\meaning\XML@tempa}}
 \global\XML@catalogue\expandafter\expandafter\expandafter{
   \XML@tempa{#2}}}


% #1 = URI
% #2 = xmltex package file
\def\SYSTEM#1#2{
 \xdef\XML@tempa{#1}
 \xdef\XML@tempa{\noexpand\the\XML@catalogue\noexpand\XML@@SYSTEM
             {\expandafter\strip@prefix\meaning\XML@tempa}}
 \global\XML@catalogue\expandafter\expandafter\expandafter{
   \XML@tempa{#2}}}

% #1 = namespace URI
% #2 = xmltex package file
\def\NAMESPACE#1#2{
  \utfeight@protect@chars
  \XML@ns@alloc{#1}
  \edef\@tempa{{\csname XML:#1\endcsname}}
  \global\XML@catalogue\expandafter{\the\expandafter\XML@catalogue
     \expandafter\XML@@NAMESPACE\@tempa{#2}}
  \unprotect@utfeight}


% #1 = unprefixed element name
% #2 = xmltex package file
\def\NAME#1#2{
 \global\XML@catalogue\expandafter{\the\XML@catalogue\XML@@NAME{#1}{#2}}}

% #1 = unprefixed element name
% #2 = namespace URI
\def\XMLNS#1#2{
  \utfeight@protect@chars
  \XML@ns@alloc{#2}
  \edef\@tempa{{#1}{\csname XML:#2\endcsname}}
  \global\XML@catalogue\expandafter{\the\expandafter\XML@catalogue
     \expandafter\XML@@XMLNS\@tempa}
  \unprotect@utfeight}



\def\XML@@PUBLIC#1#2{
 \gdef\XML@tempa{#1}
  \ifx\XML@tempa\XML@PUBLIC
    \def\XML@use{#2}
  \fi}

\def\XML@@SYSTEM#1#2{
  \def\@tempa{#1}
  \ifx\@tempa\XML@SYSTEM
    \def\XML@use{#2}
  \fi}

\def\XML@@NAMESPACE#1#2{
  \def\@tempa{#1}
  \ifx\@tempa\XML@NAMESPACE
    \def\XML@use{#2}
  \fi}


\def\XML@@NAME#1#2{
  \def\@tempa{#1}
  \ifx\@tempa\XML@NAME
    \def\XML@use{#2}
  \fi}



\begingroup
\catcode`\:\active

\def\XML@@XMLNS#1#2{
  \def\@tempa{#1}
  \ifx\@tempa\XML@NAME
    \edef\XMLNS@{#2}
    \edef\XML@this@element{
      \XMLNS@\noexpand:\XML@this@local}
    \XML@trace@warn{ \XML@this@element}
    \let\XML@NAMESPACE\XMLNS@
  \fi}
\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% entity refs
\begingroup
\catcode`\$=\catcode`\#
\catcode`\#=\active

\gdef\XML@amp@markup$1$2;{
  \ifx#$1\@empty
   \XML@charref$2;
   \XML@tempa
  \else
   \begingroup\utfeight@protect@chars
   \expandafter\aftergroup
   \csname+\string&$1$2\expandafter\endcsname
   \endgroup
  \fi}

\endgroup

\let&\XML@amp@markup

\begingroup
\catcode`\%=12
\catcode`\&=12

\gdef\XML@pcent#1;{
  \csname+%#1\endcsname
  \XML@checkend@subset}

\gdef\XMLentity#1#2{
  \expandafter\gdef\csname+&#1\endcsname{#2}}

\endgroup

% predefined definitions
\begingroup
\XMLentity{amp}{\utfeightaz&}
\XMLentity{quot}{\utfeightax"}
\XMLentity{apos}{\utfeightax'}
\XMLentity{lt}{\utfeightaz<}
\XMLentity{gt}{\utfeightax>}
\endgroup

%% character refs

% longwinded way so can share code later, also need to do d case.
% this does up to x1F FFFF which is higher than needed for XML
% (x10 FFFF)
\begingroup
\catcode`\"=12
\catcode`\<=12
\catcode`\.=12
\catcode`\,=12
\catcode`\;=12
\catcode`\!=12
\catcode`\~=13

% definition is of form 
% \utfeightX <non active char>+
% except for non active chars below 128 which are just def of catcode 12 version.
\gdef\XML@charref#1#2;{
  \begingroup
  \uppercase{\count@\if x\noexpand#1"\else#1\fi#2}\relax
  \ifnum\count@<"80\relax
    \uccode`\~\count@
    \uppercase{
    \ifnum\catcode\count@=\active
      \gdef\XML@tempa{\utfeightay~}
    \else
      \gdef\XML@tempa{~}
    \fi}
  \else\ifnum\count@<"800\relax
     \XML@utfeight@a,
     \XML@utfeight@b C\utfeightb.,
  \else\ifnum\count@<"10000\relax
     \XML@utfeight@a;
     \XML@utfeight@a,
     \XML@utfeight@b E\utfeightc.{,;}
   \else
     \XML@utfeight@a;
     \XML@utfeight@a,
     \XML@utfeight@a!
     \XML@utfeight@b F\utfeightd.{!,;}
    \fi
    \fi
  \fi
  \endgroup}

% while I support mixed tex/xml files I need to have a version
% of &#123; that always fetches the definition even if
% character is currently non active
\global\let\XML@@charref\XML@charref


\gdef\XML@charref@tex#1#2;{
  \begingroup
  \uppercase{\count@\if x\noexpand#1"\else#1\fi#2}\relax
  \ifnum\count@<"80\relax
    \uccode`\~\count@
    \uppercase{
      \gdef\XML@tempa{\utfeightay~}}
  \else\ifnum\count@<"800\relax
     \XML@utfeight@a,
     \XML@utfeight@b C\utfeightb.,
  \else\ifnum\count@<"10000\relax
     \XML@utfeight@a;
     \XML@utfeight@a,
     \XML@utfeight@b E\utfeightc.{,;}
   \else
     \XML@utfeight@a;
     \XML@utfeight@a,
     \XML@utfeight@a!
     \XML@utfeight@b F\utfeightd.{!,;}
    \fi
    \fi
  \fi
  \endgroup}

\gdef\XML@utfeight@a#1{
     \@tempcnta\count@
     \divide\count@64
     \@tempcntb\count@
     \multiply\count@64
     \advance\@tempcnta-\count@
     \advance\@tempcnta"80
     \uccode`#1\@tempcnta
     \count@\@tempcntb}

\gdef\XML@utfeight@b#1#2#3#4{
     \advance\count@"#10\relax
     \uccode`#3\count@
     \uppercase{\gdef\XML@tempa{#2#3#4}}}


%%%%%%%%%%%%%%%%

% #1 unicode slot, either 123 decimal or xA23 hex
% #2 tex definition of character when used as character data.
% code for chars below 127 somewhat experimental
\gdef\UnicodeCharacter#1#2{
   \begingroup
% suppress active test in charref
   \def\active{\catcode\count@}
   \XML@charref#1;
   \expandafter\expandafter\expandafter
   \expandafter\expandafter\expandafter
   \expandafter
    \gdef\XML@tempa{#2}
  \endgroup}

\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtoks\XMLgrabtoks

\def\xmlgrab{
  \begingroup
  \global\XMLgrabtoks{}
  \let\XML@this@level\XML@w@
  \edef\XML@next@level{ \XML@w@}
  \let\XML@doelement\XML@grabelement
  \let\XML@doend\XML@grabend
  \let\XML@docdata\XML@grabcdata
  \let\XML@comment@\XML@grabcomment@
  \let\XML@dopi\XML@grabpi
  \XMLgrab@}

\def\XMLgrab@{
  \utfeight@protect@internal
  \def<{\iffalse{\fi}\XMLgrab@@}
  \xdef\XML@tempa{\iffalse}\fi}

\def\XMLgrab@@{
  \global\XMLgrabtoks\expandafter{\the\expandafter\XMLgrabtoks\XML@tempa}
  \XML@lt@markup}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% cdata
\begingroup
\catcode`\[\active
\catcode`\]\active

% #1 = DATA
\gdef\XML@cdata #1[{
 \fi\fi\fi\fi\fi\fi
  \catcode`\^^M\active
  \catcode`\^^I\active
  \catcode`\ \active
  \XML@cdata@a}

% #1 = CDADA section text
\gdef\XML@cdata@a#1]]>{
  \begingroup
  \XML@trace@warn{CDATA}
  \edef<{\noexpand\utfeightaz\string<}
  \edef&{\noexpand\utfeightaz\string&}
  \XML@docdata{#1}}

\endgroup

% #1 = CDADA section text
\def\XML@docdata#1{#1\endgroup}

% #1 = CDADA section text
\def\XML@grabcdata#1{
  \utfeight@protect@internal
  \edef<{\noexpand\utfeightaz\string<}
  \edef&{\noexpand\utfeightaz\string&}
  \xdef\XML@tempa{#1}
  \endgroup
   \expandafter\XMLgrab@\XML@tempa}

%%%%%%%%%%
% notation

% parse past the public and system ids
% in case they contain a >.
% unlike entities PUBLIC need not have a system id
% #1 junk
% #2 notation name
% #3 PUBLIC or SYSTEM
\def\XML@dec@n#1N #2 #3 {
 \fi\fi\fi\fi\fi\fi
  \XML@trace@warn{NOTATION #2}
  \XML@quoted\XML@notation
  }

\def\XML@notation#1#2{
  \ifx>#2
   \expandafter\XML@checkend@subset
  \else
    \afterfi
    \XML@quoted\XML@notation#2
  \fi}

%%%%%%%%
%% xmt definitions

%% #1 element, may use prefix : using prefixes in current scope
%% #2 attlist
%% #3 begin code
%% #4 end code
%% if #3 is \xmlgrab, #4 may access the content of the element as #1
\long\def\XMLelement#1#2#3#4{
  \XML@ns{#1}
  \xdef\XML@tempc{:\csname
    XMLNS@\XML@this@prefix\endcsname:\XML@this@local}
% attlist
  \toks@\expandafter{\csname A:\csname
    XMLNS@\XML@this@prefix\endcsname\endcsname}
  #2
  \expandafter\gdef\csname E\XML@tempc\expandafter\endcsname
  \expandafter{\expandafter\XML@setattributes\expandafter{\the\toks@}#3}
  \gdef\XML@tempa{#3}
  \ifx\XML@tempa\XML@xmlgrab
    \expandafter\gdef\csname E\string/\XML@tempc\endcsname##1
    {#4}
  \else
    \expandafter\gdef\csname E\string/\XML@tempc\endcsname
    {#4}
  \fi}

\def\XML@xmlgrab{\xmlgrab}

%% #1 attribute, may use prefix : using prefixes in current scope
%% #2 macro to access attribute in begin or end code
%% #3 default

\long\def\XMLattribute#1#2#3{
  {\def\XMLNS@{0}
  \XML@ns{#1}
  \xdef\XML@tempa{\noexpand\XML@attrib
      \csname XMLNS@\XML@this@prefix\endcsname
        :\XML@this@local\relax\noexpand#2}}
  \toks@\expandafter{\the\expandafter\toks@\XML@tempa{#3}}}

\long\def\XMLattributeX#1#2#3{
  {\def\XMLNS@{0}
  \XML@ns{#1}
  \xdef\XML@tempa{\noexpand\XML@attrib
      \csname XMLNS@\XML@this@prefix\endcsname
        :\XML@this@local\relax\noexpand#2}}
  \toks@\expandafter{\the\expandafter\toks@\XML@tempa{#3}\utfeight@chardef#2}}

\def\utfeight@chardef#1{
\begingroup
\utfeight@protect@chars
\xdef\x@temp{#1}
\endgroup
\let#1\x@temp}

% version for namespace global attributes, used at top level.
%% #1 prefix for namespace this is for.  using prefixes in current scope
%% #2 attribute, may use prefix : using prefixes in current scope
%% #3 macro to access attribute in begin or end code
%% #4 default

\long\def\XMLnamespaceattribute#1#2#3#4{
   \toks@\expandafter\expandafter\expandafter{\csname A:\csname
    XMLNS@#1\endcsname\endcsname}
  \XMLattribute{#2}{#3}{#4}
  \expandafter\xdef\csname A:\csname
    XMLNS@#1\endcsname\endcsname{\the\toks@}}
   
\long\def\XMLnamespaceattributeX#1#2#3#4{
   \toks@\expandafter\expandafter\expandafter{\csname A:\csname
    XMLNS@#1\endcsname\endcsname}
  \XMLattributeX{#2}{#3}{#4}
  \expandafter\xdef\csname A:\csname
    XMLNS@#1\endcsname\endcsname{\the\toks@}}
   


%% #1 QName, may use prefix : using prefixes in current scope
%% #2 macro to access attribute in begin or end code
\begingroup
\catcode`\:\active

\long\gdef\XMLname#1#2{{
  \XML@ns{#1}
  \xdef#2{
      \csname XMLNS@\XML@this@prefix\endcsname
        \noexpand:\XML@this@local}}}
\endgroup

\begingroup
\catcode`\<=12
\catcode`\>=12
\catcode`\:=12
\catcode`\/=12

\gdef\XMLstring#1#2<>{
  \begingroup
  \let\XML@endgroup\endgroup
  \let\XML@this@local\@empty
  \let\XML@this@prefix\@empty
  \expandafter\def\csname
      E/:\XMLNS@:\endcsname{
    \gdef#1}
  \XML@catcodes
  \xmlgrab}

\gdef\XMLstringX#1#2<>{
  \begingroup
  \let\XML@endgroup\endgroup
  \let\XML@this@local\@empty
  \let\XML@this@prefix\@empty
  \expandafter\def\csname
      E/:\XMLNS@:\endcsname{
    \xdef#1}
  \XML@catcodes
  \utfeight@protect@chars
  \xmlgrab}

\endgroup

%% #1 name
%% #2 code, gets data as #1
\long\def\XMLProcessInstruction#1#2{
  \expandafter\gdef\csname P:#1\endcsname##1{#2}}

%%%%%%%%%%%
%% xmltex format support
\everyjob\expandafter{\the\everyjob
  \immediate\write20{xmltex version: \xmltexversion:}
\def^^ff^^fe{\XML@setenc{utf-16}\relax}
\def^^fe^^ff{\XML@setenc{utf-16}\relax}
\ActivateASCII{45}% -
\UnicodeCharacter{45}{-\kern\z@}
\let<\XML@first@lt
  \IfFileExists{xmltex.cfg}
  {\begingroup
  \XML@reset
  \@@input\@filef@und
  \endgroup}{\XML@warn{No File: xmltex.cfg}}
  \IfFileExists{\jobname.cfg}
  {\begingroup
  \XML@reset
  \@@input\@filef@und
  \endgroup}{\XML@warn{No File: \jobname.cfg}}
}


%%%%%%%%%%%
%% not currently used
%
%\def\XML@setlatexcodes{
%\ifnum\catcode`\&\active
%  \let\XML@restore\XML@catcodes
%  \let\XML@setlatexcodes\relax
%\fi}
%
%\let\XML@restore\relax


%% allow package and class loading with normal latex catcodes


\def\@fileswith@pti@ns#1[#2]#3[#4]{
  \XML@reset
  \catcode`\^^M5 %
  \let\@fileswith@pti@ns\@@fileswith@pti@ns
  \@@fileswith@pti@ns{#1}[{#2}]{#3}[{#4}]
  \XML@catcodes
  \let\@fileswith@pti@ns\XML@@fileswith@pti@ns}

\let\XML@@fileswith@pti@ns\@fileswith@pti@ns



% and similar for input of aux files
\def\@input#1{
  \XML@reset
  \catcode`\^^I\active
  \catcode`\<\active
  \catcode`\>\active
  \catcode`\&\active
  \catcode`\#\active
  \catcode`\/\active
  \catcode`\:\active
  \catcode`\=\active
  \let\XML@charref\XML@charref@tex
  \IfFileExists{#1}{\@@input\@filef@und}{\typeout{No file #1.}}
  \XML@catcodes
  \let\XML@charref\XML@@charref
}

% and end doc
\def\enddocument{
   \@enddocumenthook
   \@checkend{document}
   \clearpage
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse
\XML@reset
  \catcode`\^^I\active
  \catcode`\<\active
  \catcode`\>\active
  \catcode`\&\active
  \catcode`\#\active
  \catcode`\/\active
  \catcode`\:\active
  \catcode`\=\active
  \let\XML@charref\XML@charref@tex
       \makeatletter \input\jobname.aux
     \fi
     \@dofilelist
     \ifdim \font@submax \string>\fontsubfuzz\relax
       \@font@warning{Size substitutions with differences\MessageBreak
                  up to \font@submax\space have occured.\@gobbletwo}
     \fi
     \@defaultsubs
     \@refundefined
     \if@filesw
       \ifx \@multiplelabels \relax
         \if@tempswa
           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}
         \fi
       \else
         \@multiplelabels
       \fi
     \fi
   \endgroup
   \deadcycles\z@\@@end}


%% protected write
\long\def \protected@write#1#2#3{
      \begingroup
       \let\thepage\relax
       #2
       \utfeight@protect@external
       \let\protect\@unexpandable@protect
       \edef\reserved@a{\write#1{#3}}
       \reserved@a
      \endgroup
      \if@nobreak\ifvmode\nobreak\fi\fi
}


%% typeout etc
\def\set@display@protect{
  \let\protect\string
  \utfeight@protect@typeout}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
% xmltex namespace 
\expandafter\def\csname
  XML:http://www.dcarlisle.demon.co.uk/xmltex\endcsname{2}
\expandafter\let\csname A:2\endcsname\@empty
\expandafter\gdef\csname XMLNS@2\endcsname{2}
\XML@ns@count2

\iffalse
% this is some currently used code aiming at having an aux file
% in xml syntax using commands from the xmltex namespace.
% this would avoid some of the problems involved in having
% mixed xml/tex aux files.
\def\addcontentsline#1#2#3{
  \addtocontents{#1}{
  <2:contentsline level="#2">
   <2:toctitle>#3</2:toctitle>
   <2:page>\thepage</2:page>
  </2:contentsline>}}

\long\def\addtocontents#1#2{
  \protected@write\@auxout
      {\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}
      {<2:@writefile ext="#1">#2</2:@writefile>}}

\def\numberline#1{<2:numberline>#1</2:numberline>}

\fi
% end of xml-aux code.

%%%%%%%%%%%%%%%%%%
% tracing
\long\def\@gobble#1{}
\long\def\@gobbletwo#1#2{}
\long\def\@gobblethree#1#2#3{}

\def\xmltraceonly{
  \global\hfuzz\maxdimen
  \global\nullfont
  \global\everypar{}
  \global\let\XML@doelement\relax
  \global\let\wrong@fontshape\relax
  \global\let\selectfont\relax
  \expandafter\gdef\expandafter\XML@catcodes\expandafter{\XML@catcodes
 \gdef\unprotect@utfeight{
  \let<\XML@lt@markup
  \let&\XML@amp@markup
  \global\let\utfeightax\@gobble
  \global\let\utfeightay\@gobble
  \global\let\utfeightaz\@gobble
  \global\let\utfeightb\@gobbletwo
  \global\let\utfeightc\@gobblethree
  \global\let\utfeightd\@gobblefour}
  \unprotect@utfeight}
  \gdef\XML@doend{\XML@endgroup}
  \gdef\XML@docdata##1{\endgroup}
  \global\let\XML@dopi\@gobbletwo
}

\def\xmltraceoff{
  \global\let\XML@trace@warn\@gobble
  \global\let\XML@trace@warnNI\@gobble
  \global\let\XML@trace@warnE\@gobble
  \global\let\XML@attrib@trace\relax}


%%%%%
%% xmltex PI
%% <?xmltex tracingall ?>
%% <?xmltex typeout {hello world!} ?>
%% content of pi may be a latex command.
%% Arguments may be given in {} as usual
%% Do not use \ . First `word' (ignoring white space) taken as a tex
%% command name. May be a standard latex command, as here,
%% or some special command defined in a cfg file or package.
\begingroup
\catcode`[=1
\catcode`]=2
\catcode`\{\active
\catcode`\}\active
\XMLProcessInstruction[xmltex][
  \XML@pi#1{\relax}]

\gdef\XML@pi#1{#2}[
  \ifx\relax#2
   \csname\zap@space#1 \@empty\expandafter\endcsname
  \else
   \afterfi
   \expandafter
      \XML@pi@b\csname\zap@space#1 \@empty\endcsname[#2]
  \fi]

\gdef\XML@pi@b#1{#2}[
  \ifx\relax#2
   #1
  \else
   \afterfi
   \XML@pi@b#1[#2]
  \fi]

\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% unicode support

% \utfeighta#1         1 byte utf8 char
% \utfeightb#1#2       2 byte utf8 char
% \utfeightc#1#2#3     3 byte utf8 char
% \utfeightd#1#2#3#4   4 byte utf8 char

\def\unprotect@utfeight{
  \let<\XML@lt@markup
  \let&\XML@amp@markup
  \def\utfeightax##1{
    \csname 8:\string##1\endcsname}
  \let\utfeightay\utfeightax
  \let\utfeightaz\utfeightax
  \def\utfeightb##1##2{
    \csname 8:##1\string##2\endcsname}
  \def\utfeightc##1##2##3{
    \csname 8:##1\string##2\string##3\endcsname}
  \def\utfeightd##1##2##3##4{
    \csname 8:##1\string##2\string##3\string##4\endcsname}}

\unprotect@utfeight

% do this also in everyjob
\let<\XML@first@lt


% for moving internal arguments  (not writes)
\def\utfeight@protect@internal{
  \let\utfeightax\noexpand
  \let\utfeightay\noexpand
  \def\utfeightaz{
    \noexpand\utfeightaz\noexpand}
  \let<\relax\let&\relax
  \def\utfeightb##1##2{
    \noexpand\utfeightb##1\string##2}
  \def\utfeightc##1##2##3{
    \noexpand\utfeightc##1\string##2\string##3}
  \def\utfeightd##1##2##3##4{
    \noexpand\utfeightd##1\string##2\string##3\string##4}}


% for external files (expands one in an edef and once in a write
\def\utfeight@protect@external{
  \def\utfeightax{
    \noexpand\noexpand\noexpand}
  \let\utfeightay\utfeighta@ref
  \let\utfeightaz\utfeighta@ref
  \edef<{\string<}
  \edef&{\string&}
  \def\utfeightb##1##2{
    ##1\string##2}
  \def\utfeightc##1##2##3{
    ##1\string##2\string##3}
  \def\utfeightd##1##2##3##4{
    ##1\string##2\string##3\string##4}}

% for typeouts and immediate writes and messages
%\def\utfeight@protect@typeout{
%  \let\utfeightax\noexpand
%  \let\utfeightay\noexpand
%  \let\utfeightaz\utfeighta@ref
%  \let<\relax\let&\relax
%  \def\utfeightb##1##2{##1\string##2}
%  \def\utfeightc##1##2##3{##1\string##2\string##3}
%  \def\utfeightd##1##2##3##4{##1\string##2\string##3\string##4}}

% plan b
\def\utfeight@protect@typeout{
  \utfeight@protect@chars
  \let<\relax
  \let&\relax}

% for csname (will fall over < or & but they should not be there)
\def\utfeight@protect@chars{
  \let\utfeightax\string
  \let\utfeightay\string
  \let\utfeightaz\string
  \def\utfeightb##1##2{
    ##1\string##2}
  \def\utfeightc##1##2##3{
    ##1\string##2\string##3}
  \def\utfeightd##1##2##3##4{
    ##1\string##2\string##3\string##4}}


\def\utfeighta@ref#1{
  \string&\string##\number\expandafter`\string#1\string;}


%%%%%%%%%%%%%%%%%%%%
%% mapping input encodings to unicode.


\begingroup
\catcode`\"=12\relax
\gdef\InputCharacter#1#2{
  \begingroup
   \XML@charref#2;
   \count@"0\if\noexpand x#1\relax\else\count@#1\fi\relax
   \uccode`\~\count@
    \uppercase{
     \global\let~\XML@tempa}
  \endgroup}
\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%
% default encoding

%% need to change this default if hit BOM or xml or text decl,
\XML@setutfeight

%%%%%%%%%%%%%%%%%%%%%
% adding xmltex style protection to standard latex commands.
% if xmltex being used with other formats this does no harm
% except take up a bit of space.

\begingroup
\catcode`\:\active
\catcode`\/\active

\gdef\markboth#1#2{\gdef\@themark{{#1}{#2}}{
     \utfeight@protect@internal
     \let\protect\@unexpandable@protect
     \let\label\relax \let\index\relax \let\glossary\relax
     \mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
\gdef\markright#1{{\let\protect\@unexpandable@protect
     \utfeight@protect@internal
     \let\label\relax \let\index\relax \let\glossary\relax
     \expandafter\@markright\@themark
     {#1}\mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}

\endgroup

% this one not safe, restore not complete
\def\protected@edef{
   \let\@@protect\protect
   \let\protect\@unexpandable@protect
   \utfeight@protect@internal
   \afterassignment\restore@protect
   \edef
}
\def\protected@xdef{
   \begingroup
   \utfeight@protect@internal
   \let\protect\@unexpandable@protect
   \afterassignment\endgroup
   \xdef
}
\def\unrestored@protected@xdef{
   \utfeight@protect@internal
   \let\protect\@unexpandable@protect
   \xdef
}

% should really save and restore, not always back to markup
\def\restore@protect{\let\protect\@@protect
   \unprotect@utfeight
}

%%%%% stop this doing damage, also stops it working
\def\MakeUppercase#1{#1}


%%%%%%%%%%%%%%%%%%%%%

% support for making 7bit characters active

\begingroup
\catcode`\"=12\relax
\gdef\ActivateASCII#1{
  \uppercase{\count@"0\if x\noexpand#1\relax\else\count@#1\fi\relax}
  \toks@\expandafter{\nfss@catcodes}
       \xdef\nfss@catcodes{
       \catcode\the\count@=\the\catcode\the\count@\relax\the\toks@}
  \toks@\expandafter{\XML@catcodes}
     \xdef\XML@catcodes{
       \catcode\the\count@\active\the\toks@}
  \expandafter\ifx\csname8:"\endcsname\relax
    \expandafter\gdef\csname8:"\endcsname{"}
  \fi}
\endgroup


% some of these should not be active by default, but OK for now.
% could use \ActivateASCII for most if not all of these now.
% should probably use \UnicodeCharacter for these now
\expandafter\def\csname8:\string<\endcsname{\ifmmode\langle\else\textless\fi}
\expandafter\def\csname8:\string>\endcsname{\ifmmode\rangle\else\textgreater\fi}
\expandafter\def\csname8:\string{\endcsname{\{}
\expandafter\def\csname8:\string}\endcsname{\}}

% activateacii would use ax code, want ay code so ^ not written to aux files
% and clash with tex usage. so locally lie and make it a letter
\ActivateASCII{94}% ^ for tex ^^ notation in aux files
\UnicodeCharacter{94}{\textasciicircum}


\ActivateASCII{x5C}% \
\UnicodeCharacter{x5C}{\textbackslash}
\ActivateASCII{x5F}% \
\UnicodeCharacter{x5F}{\textunderscore}
\ActivateASCII{123}% {
\ActivateASCII{125}% {

%%%%%%%%%%%%%%%%
% white space

\UnicodeCharacter{13}{ \ignorespaces}
\UnicodeCharacter{32}{ \ignorespaces}
\UnicodeCharacter{9}{ \ignorespaces}


\expandafter\def\expandafter\obeylines\expandafter{
\expandafter\def\csname 8:\string^^M\endcsname{\leavevmode\hfil \break\null}}

\expandafter\def\expandafter\xmlnewlineaction\expandafter{
\expandafter\def\csname 8:\string^^M\endcsname}

\expandafter\def\expandafter\obeyspaces\expandafter{
\expandafter\def\csname 8: \endcsname{\nobreakspace}}

% tabs just do whatver the current space does.
\catcode`\^^I\active
\expandafter\def\csname 8:\string^^I\expandafter\endcsname
       \expandafter{\csname 8: \endcsname}


% tex conventions
\XML@reset
\IfFileExists{xmltex.cfg}
  {
  \@@input\expandafter\@filef@und\expandafter\relax
   \expandafter\XML@setenc\expandafter{\XML@thisencoding}\relax
  }{\XML@warn{No File: xmltex.cfg}}


% get xmltex in catcode 12, for comparing with \jobname
\gdef\XML@tempa#1>{}
\gdef\XML@tempb{xmltex}
\xdef\XML@tempb{\expandafter\XML@tempa\meaning\XML@tempb}
%
\gdef\XML@tempc{pdfxmltex}
\xdef\XML@tempc{\expandafter\XML@tempa\meaning\XML@tempc}
%
\xdef\XML@tempa{\lowercase{\gdef\noexpand\XML@tempa{\jobname}}}
\XML@tempa


% if jobname is xmltex or pdfxmltex dump the format, otherwise
% try to load \jobname.cfg  and input \xmlfile
% put white space back so the filename can be read off the command line
\ifx\XML@tempa\XML@tempc
  \let\XML@tempb\XML@tempc
\fi
\ifx\XML@tempa\XML@tempb
  \def\XML@tempa{
\catcode`\ =10\relax
\catcode`\^^M=10\relax
\catcode`\^^I=10\relax
\dump}
\else
\IfFileExists{\jobname.cfg}
  {
  \@@input\expandafter\@filef@und\expandafter\relax
   \expandafter\XML@setenc\expandafter{\XML@thisencoding}\relax
  }{\XML@warn{No File: \jobname.cfg}}


\ifx\xmlfile\@undefined
 \def\XML@tempa{\catcode`\-12\relax}
\else
  \def\XML@tempa{\catcode`\-12\relax\input\xmlfile\relax}
\fi
\fi

\endlinechar`\^^M \expandafter\XML@catcodes\XML@tempa