amxml 0.5.3

XML processor with some features of XPath 2.0 / 3.0 / 3.1
Documentation
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
//
// xpath_impl/eval.rs
//
// amxml: XML processor with XPath.
// Copyright (C) 2018 KOYAMA Hiro <tac@amris.co.jp>
//

use std::collections::HashMap;
use std::cmp::Ordering;
use std::error::Error;
use std::f64;
use std::i64;
use std::str::FromStr;
use std::usize;

use dom::*;
use xmlerror::*;
use xpath_impl::lexer::*;
use xpath_impl::parser::*;
use xpath_impl::xitem::*;
use xpath_impl::xsequence::*;
use xpath_impl::func::*;
use xpath_impl::oper::*;

// ---------------------------------------------------------------------
// 文字列→数値の変換。
// 空白 (オプション)、負符号 (オプション)、Number、空白 (オプション) が
// この順で連なる文字列を、IEEE 754の数値に変換する。
// それ以外はNaNにする。
// 規格上は、正記号も使えないことになる。
//
fn atof(s: &str) -> f64 {
    return f64::from_str(s.trim()).unwrap_or(f64::NAN);
}

fn atoi(s: &str) -> i64 {
    return i64::from_str(s.trim()).unwrap_or(0);
}

// ---------------------------------------------------------------------
//
fn usize_to_i64(n: usize) -> i64 {
    return n as i64;
}

// =====================================================================
// 評価環境
//
#[derive(Debug, PartialEq, Clone)]
struct VarNameValue {
    name: String,
    value: XSequence,
}

#[derive(Debug, PartialEq, Clone)]
pub struct EvalEnv {
    position: usize,                        // 組み込み函数 position() の値
    last: usize,                            // 組み込み函数 last() の値
    var_vec: Vec<VarNameValue>,             // 変数表
                                            // 同名の変数にはスコープ規則を適用
}

fn new_eval_env() -> EvalEnv {
    return EvalEnv{
        position: 0,
        last: 0,
        var_vec: vec!{},
    }
}

impl EvalEnv {

    // -----------------------------------------------------------------
    // 文書順に整列し、重複を除去する。
    //
    pub fn sort_by_doc_order(&self, node_array: &mut Vec<NodePtr>) {
        if node_array.len() <= 1 {
            return;
        }
        node_array.sort_by(|a, b| {
            return self.compare_by_doc_order(a, b);
        });
        let mut i = node_array.len() - 1;
        while 0 < i {
            if node_array[i] == node_array[i - 1] {
                node_array.remove(i);
            }
            i -= 1;
        }
    }

    // -----------------------------------------------------------------
    // 文書順を比較し、Ordering::{Less,Equal,Greater} を返す。
    //
    pub fn compare_by_doc_order(&self, a: &NodePtr, b: &NodePtr) -> Ordering {
        let a_order = a.document_order();
        let b_order = b.document_order();
        return a_order.cmp(&b_order);
    }

    // -----------------------------------------------------------------
    //
    fn set_var(&mut self, name: &str, value: &XSequence) {
        self.var_vec.insert(0, VarNameValue{
            name: String::from(name),
            value: value.clone(),
        });
    }

    // -----------------------------------------------------------------
    //
    fn set_var_item(&mut self, name: &str, value: &XItem) {
        self.var_vec.insert(0, VarNameValue{
            name: String::from(name),
            value: new_singleton(value),
        });
    }

    // -----------------------------------------------------------------
    //
    fn remove_var(&mut self, name: &str) {
        let mut index = usize::MAX;
        for (i, entry) in self.var_vec.iter().enumerate() {
            if entry.name == name {
                index = i;
                break;
            }
        }
        if index != usize::MAX {
            self.var_vec.remove(index as usize);
        }
    }

    // -----------------------------------------------------------------
    //
    fn get_var(&self, name: &str) -> Option<XSequence> {
        for entry in self.var_vec.iter() {
            if entry.name == name {
                return Some(entry.value.clone());
            }
        }
        return None;
    }

    // -----------------------------------------------------------------
    //
    fn set_position(&mut self, position: usize) -> usize {
        let old_position = self.position;
        self.position = position;
        return old_position;
    }
    fn set_last(&mut self, last: usize) -> usize{
        let old_last = self.last;
        self.last = last;
        return old_last;
    }

    // -----------------------------------------------------------------
    //
    pub fn get_position(&self) -> usize {
        return self.position;
    }
    pub fn get_last(&self) -> usize {
        return self.last;
    }
}

// =====================================================================
// (EVAL)
//
pub fn match_xpath(start_node: &NodePtr, xnode: &XNodePtr) -> Result<XSequence, Box<Error>> {

    let mut eval_env = new_eval_env();

    let start_xsequence = new_singleton_node(start_node);
    return evaluate_xnode(&start_xsequence, xnode, &mut eval_env);
}

// ---------------------------------------------------------------------
// あるXMLノードに対して、XPath構文木のあるノードを適用し、評価結果を返す。
//
fn evaluate_xnode(xseq: &XSequence, xnode: &XNodePtr,
                    eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    if is_nil_xnode(xnode) {
        panic!("Can't occur: evaluate_xnode, xnode is nil");
    }

    let xnode_type = get_xnode_type(&xnode);
    match xnode_type {

        XNodeType::OperatorPath => {
            // ---------------------------------------------------------
            // (1) 左辺値を評価する。
            //     ノードのみのシーケンスでなければエラー (空シーケンスは可)。
            //
            let left_xnode = get_left(xnode);
            let lhs = if ! is_nil_xnode(&left_xnode) {
                    evaluate_xnode(xseq, &left_xnode, eval_env)?
                } else {
                    new_xsequence()
                };

            // ---------------------------------------------------------
            // (1a) 右辺値の評価が必要ない場合は、そのまま左辺値を返す。
            //
            let right_xnode = get_right(xnode);
            if is_nil_xnode(&right_xnode) {
                return Ok(lhs);
            }

            // ---------------------------------------------------------
            // (1b) 左辺値がノードのみのシーケンスでなければ
            //      エラー (空シーケンスは可)。
            //
            if ! lhs.is_no_atom() {
                return Err(type_error!("Path演算子: ノード以外のアイテムがある。"));
            }

            // ---------------------------------------------------------
            // (2) lhsの各ノードについて、右辺値を評価する。
            //
            let mut node_exists = false;
            let mut atom_exists = false;
            let mut result_seq = new_xsequence();

            for item in lhs.iter() {
                let xseq = new_singleton(&item);
                let val_seq = evaluate_xnode(&xseq, &right_xnode, eval_env)?;

                if val_seq.is_empty() {
                    continue;
                }

                // -----------------------------------------------------
                // (2-1) ノードごとの評価値を合併していく。
                //       評価値がすべてノードのみのシーケンスか否かを
                //       調べておく。
                //
                if val_seq.is_no_atom() {
                    node_exists = true;
                } else {
                    atom_exists = true;
                }
                result_seq.append(&val_seq);

                // -----------------------------------------------------
                // (2-3) ノードと非ノードが混在していればエラー。
                //
                if node_exists && atom_exists {
                    return Err(type_error!("Path演算子: ノードと非ノードが混在している。"));
                }
            }

            // ---------------------------------------------------------
            // (3) 最後に、ノードのみのシーケンスであれば、整列、重複排除する。
            //
            if node_exists {
                let mut nodeset = result_seq.to_nodeset();
                eval_env.sort_by_doc_order(&mut nodeset);
                let sorted_seq = new_xsequence_from_node_array(&nodeset);
                return Ok(sorted_seq);
            } else {
                return Ok(result_seq);
            }
        },

        XNodeType::OperatorMap => {
            // ---------------------------------------------------------
            // (1) 左辺値を評価する。
            //
            let left_xnode = get_left(xnode);
            let lhs = if ! is_nil_xnode(&left_xnode) {
                    evaluate_xnode(xseq, &left_xnode, eval_env)?
                } else {
                    new_xsequence()
                };

            // ---------------------------------------------------------
            // (1a) 右辺値の評価が必要ない場合は、そのまま左辺値を返す。
            //
            let right_xnode = get_right(xnode);
            if is_nil_xnode(&right_xnode) {
                return Ok(lhs);
            }

            // ---------------------------------------------------------
            // (2) lhsの各ノードについて右辺値を評価し、順に合併していく。
            //     整列や重複排除はしない。
            //
            let mut result_seq = new_xsequence();
            for item in lhs.iter() {
                let xseq = new_singleton(&item);
                let val_seq = evaluate_xnode(&xseq, &right_xnode, eval_env)?;
                result_seq.append(&val_seq);
            }

            return Ok(result_seq);
        },

        XNodeType::AxisAncestor |
        XNodeType::AxisAncestorOrSelf |
        XNodeType::AxisAttribute |
        XNodeType::AxisChild |
        XNodeType::AxisDescendant |
        XNodeType::AxisDescendantOrSelf |
        XNodeType::AxisFollowing |
        XNodeType::AxisFollowingSibling |
        XNodeType::AxisParent |
        XNodeType::AxisPreceding |
        XNodeType::AxisPrecedingSibling |
        XNodeType::AxisRoot |
        XNodeType::AxisSelf => {
            return match_location_path(xseq, xnode, eval_env);
        },

        XNodeType::ContextItem => {
            return Ok(xseq.clone());
        }

        XNodeType::ApplyPredicate => {
            // 左辺値 (PrimaryExpr) に対して、右辺値の述語を適用して絞り込む。
            //
            let primary_xnode = &get_left(xnode);
            let postfix_xnode = &get_right(xnode);
            let primary_expr = evaluate_xnode(xseq, primary_xnode, eval_env)?;
            return filter_by_predicate(&primary_expr,
                            &get_left(&postfix_xnode), false, eval_env);
        },

        XNodeType::ApplyArgument => {
            // 左辺値 (PrimaryExpr) を函数と見て、右辺値の引数並びを適用する。
            //
            let primary_xnode = &get_left(xnode);
            let postfix_xnode = &get_right(xnode);
            let primary_expr = evaluate_xnode(xseq, primary_xnode, eval_env)?;
            return apply_argument(xseq, &primary_expr, &postfix_xnode, eval_env);
        },

        XNodeType::OperatorConcatenate => {
            // シーケンスを連結する。
            //
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_concatenate(&vec!{lhs, rhs});
        },

        XNodeType::OperatorOr => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let lhs_b = lhs.effective_boolean_value()?;
            if lhs_b == true {
                return Ok(new_singleton_boolean(true));
            } else {
                let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
                let rhs_b = rhs.effective_boolean_value()?;
                return Ok(new_singleton_boolean(rhs_b));
            }
        },
        XNodeType::OperatorAnd => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let lhs_b = lhs.effective_boolean_value()?;
            if lhs_b == false {
                return Ok(new_singleton_boolean(false));
            } else {
                let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
                let rhs_b = rhs.effective_boolean_value()?;
                return Ok(new_singleton_boolean(rhs_b));
            }
        },

        XNodeType::OperatorGeneralEQ => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_eq(&lhs, &rhs);
        },

        XNodeType::OperatorGeneralNE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_ne(&lhs, &rhs);
        },

        XNodeType::OperatorGeneralLT => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_lt(&lhs, &rhs);
        },

        XNodeType::OperatorGeneralLE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_le(&lhs, &rhs);
        },

        XNodeType::OperatorGeneralGT => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_gt(&lhs, &rhs);
        },

        XNodeType::OperatorGeneralGE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return general_compare_ge(&lhs, &rhs);
        },

        XNodeType::OperatorValueEQ => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_eq(&lhs, &rhs);
        },

        XNodeType::OperatorValueNE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_ne(&lhs, &rhs);
        },

        XNodeType::OperatorValueLT => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_lt(&lhs, &rhs);
        },

        XNodeType::OperatorValueLE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_le(&lhs, &rhs);
        },

        XNodeType::OperatorValueGT => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_gt(&lhs, &rhs);
        },

        XNodeType::OperatorValueGE => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return value_compare_ge(&lhs, &rhs);
        },

        XNodeType::OperatorAdd => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_add(&vec!{lhs, rhs});
        },

        XNodeType::OperatorSubtract => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_subtract(&vec!{lhs, rhs});
        },

        XNodeType::OperatorUnaryPlus => {
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_unary_plus(&vec!{rhs});
        },

        XNodeType::OperatorUnaryMinus => {
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_unary_minus(&vec!{rhs});
        },

        XNodeType::OperatorMultiply => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_multiply(&vec!{lhs, rhs});
        },

        XNodeType::OperatorDiv => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_divide(&vec!{lhs, rhs});
        },

        XNodeType::OperatorIDiv => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_integer_divide(&vec!{lhs, rhs});
        },

        XNodeType::OperatorMod => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_numeric_mod(&vec!{lhs, rhs});
        },

        XNodeType::OperatorConcat => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return fn_concat(&vec!{&lhs, &rhs});
        },

        XNodeType::OperatorUnion => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_union(&vec!{lhs, rhs}, eval_env);
        },

        XNodeType::OperatorIntersect => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_intersect(&vec!{lhs, rhs}, eval_env);
        },

        XNodeType::OperatorExcept => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_except(&vec!{lhs, rhs}, eval_env);
        },

        XNodeType::OperatorTo => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_to(&vec!{lhs, rhs});
        },

        XNodeType::OperatorIsSameNode => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_is_same_node(&vec!{lhs, rhs}, eval_env);
        },
        XNodeType::OperatorNodeBefore => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_node_before(&vec!{lhs, rhs}, eval_env);
        },
        XNodeType::OperatorNodeAfter => {
            let lhs = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            return op_node_after(&vec!{lhs, rhs}, eval_env);
        },

        XNodeType::IfExpr => {
            let cond = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let xnode_if_then_else = get_right(xnode);
            if get_xnode_type(&xnode_if_then_else) != XNodeType::IfThenElse {
                return Err(cant_occur!("IfExpr: rightがIfThenElseでない。"));
            }
            if cond.effective_boolean_value()? == true {
                let value = evaluate_xnode(xseq, &get_left(&xnode_if_then_else), eval_env)?;
                return Ok(value);
            } else {
                let value = evaluate_xnode(xseq, &get_right(&xnode_if_then_else), eval_env)?;
                return Ok(value);
            }
        },

        XNodeType::ForExpr => {
            return evaluate_xnode(xseq, &get_right(xnode), eval_env);
        },

        XNodeType::ForVarBind => {
            let var_name = get_xnode_name(&xnode);
            let range = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let mut result = new_xsequence();
            for xitem in range.iter() {
                eval_env.set_var_item(var_name.as_str(), xitem);
                let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
                result.append(&rhs);
                eval_env.remove_var(var_name.as_str());
            }
            return Ok(result);
        },

        XNodeType::LetExpr => {
            return evaluate_xnode(xseq, &get_right(xnode), eval_env);
        },

        XNodeType::LetVarBind => {
            // -----------------------------------------------------
            // 左辺値を評価し、変数値として登録した上で、右辺値を評価する。
            //
            let var_value = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let var_name = get_xnode_name(&xnode);

            eval_env.set_var(var_name.as_str(), &var_value);
            let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
            eval_env.remove_var(var_name.as_str());

            return Ok(rhs);
        },

        XNodeType::SomeExpr => {
            return evaluate_xnode(xseq, &get_right(xnode), eval_env);
        },

        XNodeType::SomeVarBind => {
            let var_name = get_xnode_name(&xnode);
            let range = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;

            for xitem in range.iter() {
                eval_env.set_var_item(var_name.as_str(), xitem);
                let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
                if rhs.effective_boolean_value()? == true {
                    return Ok(new_singleton_boolean(true));
                }
                eval_env.remove_var(var_name.as_str());
            }
            return Ok(new_singleton_boolean(false));
        },

        XNodeType::EveryExpr => {
            return evaluate_xnode(xseq, &get_right(xnode), eval_env);
        },

        XNodeType::EveryVarBind => {
            let var_name = get_xnode_name(&xnode);
            let range = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            for xitem in range.iter() {
                eval_env.set_var_item(var_name.as_str(), xitem);
                let rhs = evaluate_xnode(xseq, &get_right(xnode), eval_env)?;
                if rhs.effective_boolean_value()? == false {
                    return Ok(new_singleton_boolean(false));
                }
                eval_env.remove_var(var_name.as_str());
            }
            return Ok(new_singleton_boolean(true));
        },

        XNodeType::OperatorInstanceOf => {
            let expr_xseq = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let sequence_type_xnode = get_right(xnode);
            let b = match_sequence_type(&expr_xseq, &sequence_type_xnode)?;
            return Ok(new_singleton_boolean(b));
        },

        XNodeType::OperatorCastableAs => {
            let value = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let single_type_xnode = get_right(xnode);
            let type_name_xnode = get_left(&single_type_xnode);
            let type_name = get_xnode_name(&type_name_xnode);
            return Ok(new_singleton_boolean(value.castable_as(&type_name)));
        }

        XNodeType::OperatorCastAs => {
            let value = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;
            let single_type_xnode = get_right(xnode);
            let type_name_xnode = get_left(&single_type_xnode);
            let type_name = get_xnode_name(&type_name_xnode);
            return value.cast_as(&type_name);
        }

        XNodeType::FunctionCall => {
            // rightに連なっているArgumentTopノード群のleft以下にある
            // 式を評価し、argsArray (引数の配列) を得た後、
            // この引数列を渡して函数を評価する。
            //
            let mut args_array: Vec<XSequence> = vec!{};
            let mut curr_xnode = get_right(&xnode);
            while ! is_nil_xnode(&curr_xnode) {
                match get_xnode_type(&curr_xnode) {
                    XNodeType::ArgumentTop => {
                        let arg = evaluate_xnode(xseq,
                                    &get_left(&curr_xnode), eval_env)?;
                        args_array.push(arg);
                    },
                    _ => {
                        return Err(cant_occur!("FunctionCall: rightがArgumentTopでない。"));
                    },
                }
                curr_xnode = get_right(&curr_xnode);
            }
            return evaluate_function(&get_xnode_name(&xnode),
                    &mut args_array, xseq, eval_env);
        },

        XNodeType::StringLiteral => {
            return Ok(new_singleton_string(&get_xnode_name(&xnode)));
        },
        XNodeType::IntegerLiteral => {
            return Ok(new_singleton_integer(atoi(&get_xnode_name(&xnode))));
        },
        XNodeType::DecimalLiteral => {
            return Ok(new_singleton_decimal(atof(&get_xnode_name(&xnode))));
        },
        XNodeType::DoubleLiteral => {
            return Ok(new_singleton_double(atof(&get_xnode_name(&xnode))));
        },

        XNodeType::InlineFunction |
        XNodeType::NamedFunctionRef |
        XNodeType::PartialFunctionCall => {
            // インライン函数 | 名前付き函数参照:
            // この時点では評価せず、シングルトンとして包んで返す。
            return Ok(new_singleton_xnodeptr(&xnode));
        },

        XNodeType::Map |
        XNodeType::SquareArray |
        XNodeType::CurlyArray => {
            // マップ | 配列 (これも函数の一種として扱う)
            let xitem = convert_xnode_to_map_array(&xnode, &xseq, eval_env)?;
            return Ok(new_singleton(&xitem));
        },

        XNodeType::UnaryLookupByWildcard => {
            if let Ok(xitem_map) = xseq.get_singleton_map() {
                let mut result = new_xsequence();
                for key in xitem_map.map_keys().iter() {
                    result.append(&xitem_map.map_get(key).unwrap());
                }
                return Ok(result);

            } else if let Ok(xitem_array) = xseq.get_singleton_array() {
                let size = xitem_array.array_size();
                let mut result = new_xsequence();
                for i in 1 ..= size {
                    let index = new_xitem_integer(i as i64);
                    result.append(&xitem_array.array_get(&index).unwrap());
                }
                return Ok(result);

            } else {
                return Err(type_error!("lookup: マップ/配列でない。"));
            }
        },

        XNodeType::UnaryLookupByExpr => {
            let expr = evaluate_xnode(xseq, &get_left(xnode), eval_env)?;

            if let Ok(xitem_map) = xseq.get_singleton_map() {
                let mut result = new_xsequence();
                for key in expr.iter() {
                    if let Some(v) = xitem_map.map_get(key) {
                        result.append(&v);
                    } else {
                    }
                }
                return Ok(result);

            } else if let Ok(xitem_array) = xseq.get_singleton_array() {
                let mut result = new_xsequence();
                for index in expr.iter() {
                    if let Some(v) = xitem_array.array_get(index) {
                        result.append(&v);
                    } else {
                    }
                }
                return Ok(result);

            } else {
                return Err(type_error!("lookup: マップ/配列でない。"));
            }
        },

        XNodeType::VarRef => {
            let var_name = get_xnode_name(&xnode);
            if let Some(xseq) = eval_env.get_var(var_name.as_str()) {
                return Ok(xseq);
            } else {
                return Ok(new_xsequence());
            }
        },

        XNodeType::ParenthesizedExpr => {
            let lhs_xnode = get_left(xnode);
            if ! is_nil_xnode(&lhs_xnode) {
                return evaluate_xnode(xseq, &lhs_xnode, eval_env);
            } else {
                return Ok(new_xsequence());
            }
        },

        _ => {
            return Err(cant_occur!(
                "evaluate_xnode: xnode_type = {:?}", xnode_type));
        }
    }
}

// ---------------------------------------------------------------------
// XSequence中の各ノードに対し、xnodeで示されるLocStepを適用して
// 合致するノード集合を取得し、その合併をXSequenceとして返す。
//
fn match_location_path(xseq: &XSequence, xnode: &XNodePtr,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {
    let mut new_node_array: Vec<NodePtr> = vec!{};
    for node in xseq.to_nodeset().iter() {
        let mut matched_xseq = match_loc_step(node, xnode, eval_env)?;
        new_node_array.append(&mut matched_xseq.to_nodeset());
    }

    let result = new_xsequence_from_node_array(&new_node_array);
    return Ok(result);
}

// ---------------------------------------------------------------------
// XML木のあるノードを起点として、
// xNodeで示されるLocStep (例: 「child::foo[@attr='at']」) に、
// 軸、ノード・テスト、述語が合致するノード集合をXSequenceの形で返す。
//
fn match_loc_step(node: &NodePtr, xnode: &XNodePtr,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    let mut node_array: Vec<NodePtr> = vec!{};

    match get_xnode_type(&xnode) {
        XNodeType::AxisAncestor => {
            node_array = match_along_axis(node, xnode, array_ancestor);
        },

        XNodeType::AxisAncestorOrSelf => {
            node_array = match_along_axis(node, xnode, array_ancestor_or_self);
        },

        XNodeType::AxisAttribute => {
            node_array = match_along_axis(node, xnode, NodePtr::attributes);
        },

        XNodeType::AxisChild => {
            node_array = match_along_axis(node, xnode, NodePtr::children);
        },

        XNodeType::AxisDescendant => {
            node_array = match_along_axis(node, xnode, array_descendant);
        },

        XNodeType::AxisDescendantOrSelf => {
            node_array = match_along_axis(node, xnode, array_descendant_or_self);
        },

        XNodeType::AxisFollowing => {
            node_array = match_along_axis(node, xnode, array_following);
        },

        XNodeType::AxisFollowingSibling => {
            node_array = match_along_axis(node, xnode, array_following_sibling);
        },

        XNodeType::AxisParent => {
            if let Some(parent) = node.parent() {
                if match_node_test(&parent, xnode) {
                    node_array.push(parent.rc_clone());
                }
            }
        },

        XNodeType::AxisPreceding => {
            node_array = match_along_axis(node, xnode, array_preceding);
        },

        XNodeType::AxisPrecedingSibling => {
            node_array = match_along_axis(node, xnode, array_preceding_sibling);
        },

        XNodeType::AxisRoot => {
            node_array.push(node.root().rc_clone());
        },

        XNodeType::AxisSelf => {
            if match_node_test(&node, xnode) {
                node_array.push(node.rc_clone());
            }
        },

        _ => {
            return Err(cant_occur!("match_loc_step: xnode_type: {:?}",
                    get_xnode_type(&xnode)));
        },
    }

    // 述語によって絞り込む。
    let rhs = get_right(&xnode);
    if ! is_nil_xnode(&rhs) {
        let result = filter_by_predicates(
                &new_xsequence_from_node_array(&node_array), &rhs, eval_env)?;
        return Ok(result);
    } else {
        return Ok(new_xsequence_from_node_array(&node_array));
    }
}

// ---------------------------------------------------------------------
// 函数 along_axis_func を適用して得たノード配列から、match_node_test() に
// 合格したノードのみ集めて返す。
//
fn match_along_axis<F>(node: &NodePtr, xnode: &XNodePtr,
                        mut along_axis_func: F) -> Vec<NodePtr>
    where F: FnMut(&NodePtr) -> Vec<NodePtr> {

    let mut node_array: Vec<NodePtr> = vec!{};
    for n in along_axis_func(&node).iter() {
        if match_node_test(&n, xnode) {
            node_array.push(n.rc_clone());
        }
    }
    return node_array;
}

// ---------------------------------------------------------------------
// ancestor軸で合致する候補ノード。
//
fn array_ancestor(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    if let Some(parent) = node.parent() {
        node_array.append(&mut array_ancestor(&parent));
        node_array.push(parent.rc_clone());
    }
    return node_array;
}

// ---------------------------------------------------------------------
// ancestor-or-self軸で合致する候補ノード。
//
pub fn array_ancestor_or_self(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    node_array.append(&mut array_ancestor(node));
    node_array.push(node.rc_clone());
    return node_array;
}

// ---------------------------------------------------------------------
// descendant軸で合致する候補ノード。
//
fn array_descendant(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    for ch in node.children().iter() {
        node_array.push(ch.rc_clone());
        node_array.append(&mut array_descendant(ch));
    }
    return node_array;
}

// ---------------------------------------------------------------------
// descendant-or-self軸で合致する候補ノード。
//
fn array_descendant_or_self(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    node_array.push(node.rc_clone());
    node_array.append(&mut array_descendant(node));
    return node_array;
}

// ---------------------------------------------------------------------
// following軸で合致する候補ノード。
//
fn array_following(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    if node.node_type() != NodeType::Attribute {
        let all_nodes = array_descendant_or_self(&node.root());
        let descendant_or_self_nodes = array_descendant_or_self(&node);
        let mut self_occured = false;
        for ch in all_nodes.iter() {
            if self_occured && ! descendant_or_self_nodes.contains(ch) {
                node_array.push(ch.rc_clone());
            }
            if ch == node {
                self_occured = true;
            }
        }
    }
    return node_array;
}

// ---------------------------------------------------------------------
// following-sibling軸で合致する候補ノード。
//
fn array_following_sibling(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    if node.node_type() != NodeType::Attribute {
        if let Some(parent) = node.parent() {
            let mut occured = false;
            for ch in parent.children().iter() {
                if occured {
                    node_array.push(ch.rc_clone());
                }
                if ch == node {
                    occured = true;
                }
            }
        }
    }
    return node_array;
}

// ---------------------------------------------------------------------
// preceding軸で合致する候補ノード。
//
fn array_preceding(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    if node.node_type() != NodeType::Attribute {
        let all_nodes = array_descendant_or_self(&node.root());
        let ancestor_nodes = array_ancestor(&node);
        let mut self_occured = false;
        for ch in all_nodes.iter() {
            if ch == node {
                self_occured = true;
            }
            if ! self_occured && ! ancestor_nodes.contains(ch) {
                node_array.push(ch.rc_clone());
            }
        }
    }
    return node_array;
}

// ---------------------------------------------------------------------
// preceding-sibling軸で合致する候補ノード。
//
fn array_preceding_sibling(node: &NodePtr) -> Vec<NodePtr> {
    let mut node_array: Vec<NodePtr> = vec!{};
    if node.node_type() != NodeType::Attribute {
        if let Some(parent) = node.parent() {
            let mut occured = false;
            for ch in parent.children().iter() {
                if ch == node {
                    occured = true;
                }
                if ! occured {
                    node_array.push(ch.rc_clone());
                }
            }
        }
    }
    return node_array;
}

// ---------------------------------------------------------------------
// ノード・テスト (名前テストまたは種類テスト)。
//
fn match_node_test(node: &NodePtr, xnode: &XNodePtr) -> bool {

    // xnode: AxisNNNN;
    // get_left(&xnode) がNilのとき:
    //     get_xnode_name(&xnode): NameTestで照合する名前
    // get_left(&xnode) がXNodeType::KindTestのとき:
    //     そのget_left(&xnode): KindTestで照合する規則
    //
    let kind_test_xnode = get_left(&xnode);
    if is_nil_xnode(&kind_test_xnode) {
        return match_name_test(node, xnode);
    } else {
        return match_kind_test(node, &kind_test_xnode);
    }
}

// ---------------------------------------------------------------------
// ノードの名前テスト。
//
fn match_name_test(node: &NodePtr, xnode: &XNodePtr) -> bool {

    let name_test_pattern = get_xnode_name(&xnode);
        // ノード名と照合するパターン。
        // 例えば「child::para」というステップの「para」。

    // -------------------------------------------------------------
    // 省略記法「//」は「/descendant-or-self::node()/」、
    // 「..」は「parent::node()」の意味であるが、
    // 便宜上、NameTestの形式とし、「node()」を設定してある。
    // (XNodeType::KindTestのノードを作るよりも処理が簡単)
    //
    if name_test_pattern.as_str() == "node()" {
        return true;
    }

    // -------------------------------------------------------------
    // 軸によって決まる主ノード型と実際のノード型が一致して
    // いなければfalseとする。
    //   attribute軸 => attribute
    //   //namespace軸 => namespace
    //   それ以外 => element
    //
    let main_node_type =
        if get_xnode_type(&xnode) == XNodeType::AxisAttribute {
            NodeType::Attribute
        } else {
            NodeType::Element
        };

    if main_node_type != node.node_type() {
        return false;
    }

    // -------------------------------------------------------------
    // 名前の照合にもとづく判定 (「*」とも照合)
    //
    if name_test_pattern == node.name() {
        return true;
    }
    if name_test_pattern.as_str() == "*" {
        return true;
    }

    // -------------------------------------------------------------
    // 「na:*」との照合にもとづく判定
    //
    let v: Vec<&str> = name_test_pattern.splitn(2, ":").collect();
    if v.len() == 2 && v[1] == "*" {
        if node.space_name() == v[0] {
            return true;
        }
    }

    return false;
}

// ---------------------------------------------------------------------
// ノードの種類テスト
//
// DocumentTest                                  // ☆
// SchemaElementTest                             // ☆
// SchemaAttributeTest                           // ☆
// NamespaceNodeTest                             // ☆
//                                                  ☆ 未実装 (構文解析のみ)
//
// 2.5.1 Predefined Schema Types
// - 未検証の要素ノードについては、型註釈が「xs:untyped」になる。
//
// 2.5.5.3 Element Test
// - element()、element(*): 任意の要素ノードに合致。
// - element(ElementName): 要素名が一致。
// - element(ElementName, TypeName):
//       要素名が一致し、derives-from(xs:untyped, TypeName) が true、
//       かつ、nilledプロパティーがfalse (i.e. 属性 "xsi:nil" の値が "true" でない)。
// - element(ElementName, TypeName?):
//       要素名が一致し、derives-from(xs:untyped, TypeName) が true。
//       nilledプロパティーはtrueでもfalseでもよい。
// - element(*, TypeName):
//       derives-from(xs:untyped, TypeName) が true、
//       かつ、nilledプロパティーがfalse (i.e. 属性 "xsi:nil" の値が "true" でない)。
// - element(*, TypeName?):
//       derives-from(xs:untyped, TypeName) が true。
//       nilledプロパティーはtrueでもfalseでもよい。
//
// 2.5.5.5 Attribute Test
// - attribute()、attribute(*): 任意の属性ノードに合致。
// - attribute(AttributeName): 属性名が一致。
// - attribute(AttributeName, TypeName):
//       属性名が一致し、derives-from(xs:untypedAtomic, TypeName) が true。
// - attribute(*, TypeName):
//       derives-from(xs:untypedAtomic, TypeName) が true。
//
fn match_kind_test(node: &NodePtr, xnode: &XNodePtr) -> bool {
    // assert:: get_xnode_type(&kind_test_xnode) == XNodeType::KindTest

    let node_type = node.node_type();

    let test_xnode = get_left(xnode);
    match get_xnode_type(&test_xnode) {
        XNodeType::DocumentTest => {
            // DocumentTestは未実装。
        },

        XNodeType::ElementTest => {
            if node_type != NodeType::Element {
                return false;
            }

            let element_name = get_xnode_name(&test_xnode);
                    // 明示的に指定がない場合の既定値は *
            if element_name != "*" && element_name != node.name() {
                return false;
            }

            let type_name_xnode = get_left(&test_xnode);
            let type_name_ex = get_xnode_name(&type_name_xnode);
                    // 明示的に指定がない場合の既定値は xs:anyType?
            let type_name = type_name_ex.trim_right_matches('?');
            let with_q = type_name_ex.ends_with("?");
            if ! derives_from("xs:untyped", &type_name) {
                return false;
            }
            if ! with_q {
                if let Some(p) = node.attribute_value("xsi:nil") {
                    if p == "true" {
                        return false;
                    }
                }
            }

            return true;
        },

        XNodeType::AttributeTest => {
            if node_type != NodeType::Attribute {
                return false;
            }

            let attribute_name = get_xnode_name(&test_xnode);
                    // 明示的に指定がない場合の既定値は *
            if attribute_name != "*" && attribute_name != node.name() {
                return false;
            }

            let type_name_xnode = get_left(&test_xnode);
            let type_name = get_xnode_name(&type_name_xnode);
                    // 明示的に指定がない場合の既定値は xs:anyType
            if ! derives_from("xs:untypedAtomic", &type_name) {
                return false;
            }

            return true;
        },

        XNodeType::SchemaElementTest => {
            // SchemaElementTestは未実装。
        },

        XNodeType::SchemaAttributeTest => {
            // SchemaAttributeTestは未実装。
        },

        XNodeType::PITest => {
            let arg = get_xnode_name(&test_xnode);
            return node_type == NodeType::Instruction &&
                   (arg == "" || arg == node.name());
        },

        XNodeType::CommentTest => {
            return node_type == NodeType::Comment;
        },

        XNodeType::TextTest => {
            return node_type == NodeType::Text;
        },

        XNodeType::NamespaceNodeTest => {
            // NamespaceNodeTestは未実装。
        },

        XNodeType::AnyKindTest => {
            return true;
        },

        _ => {},
    }

    return false;
}

// ---------------------------------------------------------------------
// Location Stepで得たシーケンスに対して、述語を順次適用してしぼり込み、
// 最終的に得られるシーケンスを返す。
//
// apply_postfix() の処理の一部といった恰好だが、構文上、述語のみ
// 並んでいる状況に対応する。
//
// xseq: AxisNNNNNNNN (Location Step) を評価して得たシーケンス (ノード集合)。
// xnode: AxisNNNNNNNNの右。
//    rightをたどったノードはすべてPredicate{Rev}Topであり、
//    そのleft以下に述語式の構文木がある。
//    各ノードに対して述語を適用し、trueであったもののみにしぼり込む。
//
// AxisNNNNNNNN --- Predicate{Rev}Top --- Predicate{Rev}Top ---...
//  (NameTest)              |                     |
//                        (Expr)                (Expr)
//
fn filter_by_predicates(xseq: &XSequence, xnode: &XNodePtr,
            eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    let mut curr_xseq = xseq.clone();
    let mut curr_xnode = xnode.clone();

    while ! is_nil_xnode(&curr_xnode) {
        match get_xnode_type(&curr_xnode) {
            XNodeType::PredicateTop => {
                curr_xseq = filter_by_predicate(&curr_xseq,
                            &get_left(&curr_xnode), false, eval_env)?;
            },
            XNodeType::PredicateRevTop => {
                curr_xseq = filter_by_predicate(&curr_xseq,
                            &get_left(&curr_xnode), true, eval_env)?;
            },
            _ => {
                return Err(cant_occur!(
                    "filter_by_predicates: 述語以外のノード: {}",
                    get_xnode_type(&curr_xnode).to_string()));
            }
        }

        curr_xnode = get_right(&curr_xnode);
    }
    return Ok(curr_xseq);
}

// ---------------------------------------------------------------------
// シーケンスに属する個々のアイテムに対して、ある (ひとつの) 述語を
// 適用してしぼり込み、新しいシーケンスを返す。
//
fn filter_by_predicate(xseq: &XSequence, xnode: &XNodePtr,
        reverse_order: bool, eval_env: &mut EvalEnv)
                                    -> Result<XSequence, Box<Error>> {

    if is_nil_xnode(xnode) {
        return Err(cant_occur!("filter_by_predicate: xnode is nil"));
    }

    let mut result = new_xsequence();
    for (i, xitem) in xseq.iter().enumerate() {

        // 評価環境に文脈位置を設定する。
        let last = xseq.len();
        let position = if ! reverse_order { i + 1 } else { last - i };
                                            // 文脈位置の番号は1が起点
        let old_position = eval_env.set_position(position);
        let old_last = eval_env.set_last(last);

        // シーケンス中、i番目のアイテムに対してxnodeを適用して評価する。
        let val = evaluate_xnode(&new_singleton(xitem), xnode, eval_env)?;

        // 評価環境を元に戻しておく。
        eval_env.set_last(old_last);
        eval_env.set_position(old_position);

        // 評価結果をもとに、このアイテムを残すかどうか判定する。
        let mut do_push = false;
        match val.get_singleton_item() {
            Ok(XItem::XIInteger{value}) => {
                do_push = value == usize_to_i64(position);
            },
            Ok(XItem::XINode{value: _}) => {
                do_push = true;
            },
            Ok(XItem::XIBoolean{value}) => {
                do_push = value;
            },
            _ => {},
        }
        if do_push {
            result.push(&xitem.clone());
        }
    }
    return Ok(result);

}

// ---------------------------------------------------------------------
// インライン函数/マップ/配列に、引数を適用する。
// xseq: 引数の値を評価する際、対象とするシーケンス (文脈ノード)。
// curr_xseq: シングルトン。
//            XItem: 典型的にはInlineFunction型のXNodePtr。
//            また、MapやSquareArrayも函数であって、キーや指標を
//            引数として渡し、値を取り出すことができる。
//            このXNodePtrを取り出し、インライン函数として実行する。
// InlineFunction --- ReturnType ------- Param ------- Param ---...
//       |                |            (varname)     (varname)
//       |                |                |             |
//       |          (SequenceType)   (SequenceType)(SequenceType)
//       |
//     Expr (FunctionBody) ---...
//       |
//      ...
//                      ☆戻り値型も照合すること。
//
// curr_xnode: ArgumentListTop型のXNodePtr。インライン函数に渡す引数並び。
//      ArgumentListTop
//             |
//        ArgumentTop --- ArgumentTop ---...
//             |               | 第2引数
//             |           OpLiteral
//             | 第1引数
//            OpEQ --- (rhs)
//             |
//           (lhs)
//
// eval_env: 評価環境。
//
fn apply_argument(xseq: &XSequence, curr_xseq: &XSequence,
                curr_xnode: &XNodePtr,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    let arg_xnode = get_left(&curr_xnode);

    let mut argument_xseq: Vec<XSequence> = vec!{};
    let mut curr_arg_top = arg_xnode.clone();
    while ! is_nil_xnode(&curr_arg_top) {
        let arg_expr = get_left(&curr_arg_top);
        let val = evaluate_xnode(xseq, &arg_expr, eval_env)?;
        argument_xseq.push(val);
        curr_arg_top = get_right(&curr_arg_top);
    }

    // -----------------------------------------------------------------
    // インライン函数
    //
    if let Ok(inline_func_xnode) = curr_xseq.get_singleton_xnodeptr() {
        match get_xnode_type(&inline_func_xnode) {
            XNodeType::InlineFunction => {
                return call_inline_func(&inline_func_xnode,
                            argument_xseq, xseq, eval_env);
            },
            _ => {}
        }
    }

    // -----------------------------------------------------------------
    // マップ
    //
    if let Ok(map_item) = curr_xseq.get_singleton_map() {
        let key = argument_xseq[0].get_singleton_item()?;
        if let Some(v) = map_item.map_get(&key) {
            return Ok(v);
        } else {
            return Err(dynamic_error!(
                    "map_lookup: key = {}: 値が見つからない。", key));
        }
    }

    // -----------------------------------------------------------------
    // 配列
    //
    if let Ok(array_item) = curr_xseq.get_singleton_array() {
        let index_item = argument_xseq[0].get_singleton_item()?;
        if let Some(v) = array_item.array_get(&index_item) {
            return Ok(v);
        } else {
            return Err(dynamic_error!(
                "Array index ({}) out of bounds.", index_item));
        }
    }

    return Err(cant_occur!(
                "apply_argument: インライン函数/マップ/配列でない。"));

}

// ---------------------------------------------------------------------
//
fn call_inline_func(inline_func_xnode: &XNodePtr,
                argument_xseq: Vec<XSequence>,
                context_xseq: &XSequence,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    // -----------------------------------------------------------------
    // 函数定義の実体を取り出す。
    //
    let func_body_xnode = get_left(&inline_func_xnode);

    // -----------------------------------------------------------------
    // 仮引数名を調べる。
    // // 個数、引数型も照合すること。
    //
    let mut param_names: Vec<String> = vec!{};
    let mut sequence_types: Vec<XNodePtr> = vec!{};
    let return_type = get_right(&inline_func_xnode);
    let mut param_xnode = get_right(&return_type);
    while ! is_nil_xnode(&param_xnode) {
        param_names.push(get_xnode_name(&param_xnode));
        sequence_types.push(get_left(&param_xnode));
        param_xnode = get_right(&param_xnode);
    }

    // -----------------------------------------------------------------
    // 実引数の値を変数 (仮引数) に束縛する。
    //
    for (i, val) in argument_xseq.iter().enumerate() {
        if match_sequence_type(&val, &sequence_types[i])? == false {
            return Err(type_error!(
                    "インライン函数: 引数の型が合致していない: {}。",
                    val.to_string()));
        }
        eval_env.set_var(&param_names[i], &val);
    }

    // -----------------------------------------------------------------
    // インライン函数を実行する。
    //
    let value = evaluate_xnode(context_xseq, &func_body_xnode, eval_env)?;

    // -----------------------------------------------------------------
    // 変数 (仮引数) を削除する。
    //
    let mut i: i64 = (param_names.len() as i64) - 1;
    while 0 <= i {
        eval_env.remove_var(&param_names[i as usize]);
        i -= 1;
    }

    return Ok(value);
}

// ---------------------------------------------------------------------
// 名前付き函数参照を、引数を渡して呼び出す。
//
fn call_named_func(func_xnode: &XNodePtr,
                argument_xseq: Vec<XSequence>,
                context_xseq: &XSequence,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    // -----------------------------------------------------------------
    // 函数定義の実体を取り出す。
    //
    let func_name = get_xnode_name(&func_xnode);
    let v: Vec<&str> = func_name.split("#").collect();
    let arity = atoi(v[1]) as usize;
    if arity != argument_xseq.len() {
        return Err(type_error!(
                "名前付き函数参照 ({}): 引数の個数が合致しない: {}。",
                func_name, argument_xseq.len()));
    }

    return evaluate_function(&v[0], &argument_xseq, context_xseq, eval_env);
}

// ---------------------------------------------------------------------
// 静的函数の部分函数呼び出し。
//
// func_xnode: 部分函数呼び出しの構文木
//             引数のうちArgumentTopは、普通の静的函数と同様、
//             文脈ノードに対してleft以下を評価し、引数として函数に渡す。
//             ArgumentPlaceholderである場合はargument_xseq[i]を
//             引数として函数に渡す。
//
// PartialFunctionCall --- ArgumentTop --- ArgumentPlaceholder ---...
//    (func_name)               |                  (第2引数)
//                             ... (第1引数)
//
// argument_xseq: 長さはArgumentPlaceholderの個数と同じ。
//                argument_xseq[i]をArgumentPlaceholder部分の引数として
//                函数に渡す。
//
// context_xseq: 文脈シーケンス。
//               ArgumentTopである引数は、文脈シーケンスに対して評価する。
//               関数の評価も文脈シーケンスに対しておこなう。
//
// eval_env: 評価環境 (変数の値など)。
//
fn call_partial_func(func_xnode: &XNodePtr,
                argument_xseq: Vec<XSequence>,
                context_xseq: &XSequence,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    let mut args_array: Vec<XSequence> = vec!{};
    let mut curr_xnode = get_right(&func_xnode);
    let mut i = 0;
    while ! is_nil_xnode(&curr_xnode) {
        match get_xnode_type(&curr_xnode) {
            XNodeType::ArgumentTop => {
                let arg = evaluate_xnode(context_xseq, &get_left(&curr_xnode), eval_env)?;
                args_array.push(arg);
            },
            XNodeType::ArgumentPlaceholder => {
                if i >= argument_xseq.len() {
                    return Err(dynamic_error!("函数呼び出しの「?」に渡すべき引数が足りない。"));
                }
                args_array.push(argument_xseq[i].clone());
                i += 1;
            },
            _ => {
                return Err(cant_occur!("call_partial_func: ArgumentTopでもArgumentPlaceholderでもでない。"));
            }
        }
        curr_xnode = get_right(&curr_xnode);
    }

    return evaluate_function(&get_xnode_name(&func_xnode),
            &mut args_array, context_xseq, eval_env);

}

// ---------------------------------------------------------------------
// 函数呼び出し。
// インライン函数、名前付き函数参照、部分函数。
//
pub fn call_function(func_xnode: &XNodePtr,
                argument_xseq: Vec<XSequence>,
                context_xseq: &XSequence,
                eval_env: &mut EvalEnv) -> Result<XSequence, Box<Error>> {

    match get_xnode_type(&func_xnode) {
        XNodeType::InlineFunction => {
            return call_inline_func(func_xnode, argument_xseq, context_xseq, eval_env);
        },
        XNodeType::NamedFunctionRef => {
            return call_named_func(func_xnode, argument_xseq, context_xseq, eval_env);
        },
        XNodeType::PartialFunctionCall => {
            return call_partial_func(func_xnode, argument_xseq, context_xseq, eval_env);
        },
        _ => {
            return Err(cant_occur!("call_h_function: XNodeType = {:?}",
                    get_xnode_type(&func_xnode)));
        },
    }
}

// ---------------------------------------------------------------------
// シーケンスの型を、
// シーケンス型の定義 (XNodeType::SequenceTypeであるxnodeで表現) と照合する。
//
// [ 79] SequenceType ::= ("empty-sequence" "(" ")")
//                      | (ItemType OccurenceIndicator?)
// [ 80] OccurrenceIndicator ::= "?" | "*" | "+"
// [ 81] ItemType ::= KindTest
//                  | ("item" "(" ")")
//                  | FunctionTest
//                  | MapTest
//                  | ArrayTest
//                  | AtomicOrUnionType
//                  | ParenthesizedItemType                            ☆
// [ 82] AtomicOrUnionType ::= EQName
// [102] FunctionTest ::= AnyFunctionTest
//                      | TypedFunctionTest
//
fn match_sequence_type(xseq: &XSequence, xnode: &XNodePtr) -> Result<bool, Box<Error>> {
    if get_xnode_type(xnode) != XNodeType::SequenceType {
        return Err(cant_occur!(
                "match_sequence_type: xnodeがSequenceTypeでない: {:?}。",
                get_xnode_type(xnode)));
    }

    let type_xnode = get_left(xnode);

    // -----------------------------------------------------------------
    // empty-sequence()
    // xseqが空シーケンスか否かのみ判定する。
    //
    if get_xnode_type(&type_xnode) == XNodeType::EmptySequenceTest {
        return Ok(xseq.is_empty());
    }

    // -----------------------------------------------------------------
    // それ以外の場合、まず出現数の指定 (?、*、+、空) を照合する。
    // シーケンスに含まれるアイテムの個数が指定を満たしていなければ、
    // この時点でfalseとする。
    //
    if match_occurence(xseq, &get_xnode_name(xnode))? == false {
        return Ok(false);
    }

    // -----------------------------------------------------------------
    // ItemTypeに応じて、シーケンスの各アイテムを判定する。
    //
    match get_xnode_type(&type_xnode) {
        XNodeType::KindTest => {            // element(name) など
            return Ok(match_sequence_kind_test(xseq, &type_xnode));
        },
        XNodeType::ItemTest => {            // item()
            return Ok(match_sequence_item_test(xseq));
        },

        XNodeType::AnyFunctionTest => {     // function(*)
            return Ok(match_sequence_any_function_test(xseq));
        },

        XNodeType::TypedFunctionTest => {
            return match_sequence_typed_function_test(xseq, &type_xnode);
        },

        XNodeType::ArrayTest => {
            return match_sequence_array_test(xseq, &type_xnode);
        },

        XNodeType::MapTest => {
            return match_sequence_map_test(xseq, &type_xnode);
        },

        XNodeType::AtomicOrUnionType => {
            return Ok(match_sequence_atomic_or_union_type(xseq, &type_xnode));
        },

        XNodeType::ParenthesizedItemType => {
            // =====================================================
            return Ok(false);
        },

        _ => {
            return Err(cant_occur!(
                "match_sequence_type: xnodeの左辺値のxnode_typeが想定外: {:?}",
                type_xnode));
        },
    }
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::KindTest である場合。
//
// xseq のアイテムすべてについて、NodePtrであり、かつ、そのノード型が
// 指定どおりであることを確かめる。
//
fn match_sequence_kind_test(xseq: &XSequence, type_xnode: &XNodePtr) -> bool {
    for xitem in xseq.iter() {
        if let Some(node) = xitem.as_nodeptr() {
            if match_kind_test(&node, &type_xnode) == false {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::ItemTest である場合。
//
// xseq の要素すべてについて、アイテムであることを確かめる。
//
fn match_sequence_item_test(xseq: &XSequence) -> bool {
    for xitem in xseq.iter() {
        if ! xitem.is_item() {
            return false;
        }
    }
    return true;
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::AnyFunctionTest である場合。
//
// xseq の要素すべてについて、函数 (マップ/配列を含む) であることを確かめる。
//
fn match_sequence_any_function_test(xseq: &XSequence) -> bool {
    for xitem in xseq.iter() {
        let mut is_function = false;
        if let Ok(xnode) = xitem.get_as_raw_xnodeptr() {
            match get_xnode_type(&xnode) {
                XNodeType::InlineFunction |
                XNodeType::NamedFunctionRef |
                XNodeType::PartialFunctionCall => {
                    is_function = true;
                },
                _ => {},
            }
        }
        if let Ok(_) = xitem.get_as_raw_array() {
            is_function = true;
        }
        if let Ok(_) = xitem.get_as_raw_map() {
            is_function = true;
        }
        if ! is_function {
            return false;
        }
    }
    return true;
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::TypedFunctionTest である場合。
//
fn match_sequence_typed_function_test(xseq: &XSequence, type_xnode: &XNodePtr) -> Result<bool, Box<Error>> {

    // -----------------------------------------------------------------
    // 戻り値型および引数型の定義をsignature_xnodeに取り出す。
    // signature_xnode[0]: 戻り値型の定義。
    // signature_xnode[n]: 第n引数の型の定義。
    // signature_xnode.len() - 1: 引数の個数。
    //
    let mut signature_xnode: Vec<XNodePtr> = vec!{};
    let mut curr = get_right(&type_xnode);
    while ! is_nil_xnode(&curr) {
        signature_xnode.push(get_left(&curr));
        curr = get_right(&curr);
    }

    // -----------------------------------------------------------------
    //
    for xitem in xseq.iter() {
        let mut is_function = false;

        // -------------------------------------------------------------
        //
        if let Ok(xnode) = xitem.get_as_raw_xnodeptr() {

            let sig_xnode: XNodePtr;

            match get_xnode_type(&xnode) {

                // -----------------------------------------------------
                // (名前付き函数参照)
                // シグニチャー表を引き、テキスト形式のシグニチャーを
                // 構文木に変換する。
                //
                XNodeType::NamedFunctionRef => {
                    let func_name = get_xnode_name(&xnode);
                    let signature = get_function_signature(func_name.as_str());
                    if signature == "" {
                        return Ok(false);
                    }
                    let mut lex = Lexer::new(&signature)?;
                    sig_xnode = parse_function_test(&mut lex)?;
                },

                // -----------------------------------------------------
                // (インライン函数)
                //
                XNodeType::InlineFunction => {
                    sig_xnode = xnode.clone();
                },

                XNodeType::PartialFunctionCall => {
                    sig_xnode = xnode.clone();
                },
                _ => {
                    return Ok(false);
                },
            }

            let mut i = 0;
            let mut curr = get_right(&sig_xnode);
            while ! is_nil_xnode(&curr) {
                if signature_xnode.len() <= i {     // 引数が多い。
                    return Ok(false);
                }
                let type_xnode = get_left(&curr);
                if i == 0 {                 // 戻り値型
                    if ! subtype(&type_xnode, &signature_xnode[i]) {
                        return Ok(false);
                    }
                } else {                    // 引数型
                    if ! subtype(&signature_xnode[i], &type_xnode) {
                        return Ok(false);
                    }
                }
                curr = get_right(&curr);
                i += 1;
            }
            if signature_xnode.len() != i {         // 引数が少ない。
                return Ok(false);
            }
            // -------------------------------------------------
            //

            is_function = true;
        }

        // -------------------------------------------------------------
        // (マップの場合)
        // キーと値の型を照合する。
        //
        if let Ok(xseq_map) = xitem.get_as_raw_map() {
            if signature_xnode.len() != 2 {
                return Ok(false);
            }
            let key_type = get_xnode_name(&get_left(&signature_xnode[1]));
            if ! match_map_sequence_type(&xseq_map, &key_type, &signature_xnode[0])? {
                return Ok(false);
            }
            is_function = true;
        }

        // -------------------------------------------------------------
        // (配列の場合)
        // 第1引数がintegerであることを確かめ、値の型を照合する。
        //
        if let Ok(xseq_array) = xitem.get_as_raw_array() {
            if signature_xnode.len() != 2 {
                return Ok(false);
            }
            let xseq_int_1 = new_singleton_integer(1);
            if ! match_sequence_type(&xseq_int_1, &signature_xnode[1])? {
                return Ok(false);
            }

            if ! match_array_sequence_type(&xseq_array, &signature_xnode[0])? {
                return Ok(false);
            }
            is_function = true;
        }

        // -------------------------------------------------------------
        // 上記のどの種類にも当てはまらなければ、
        // (型付き) 函数ではないことになる。
        //
        if ! is_function {
            return Ok(false);
        }
    }
    return Ok(true);
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::ArrayTest である場合。
//
// xseq の要素すべてについて、配列であって、その要素のシーケンス型も
// 定義に合致していることを確かめる。
//
fn match_sequence_array_test(xseq: &XSequence, type_xnode: &XNodePtr) -> Result<bool, Box<Error>> {
    let element_type = get_left(&type_xnode);
    for xitem in xseq.iter() {
        if let Ok(xseq_array) = xitem.get_as_raw_array() {
            if ! match_array_sequence_type(&xseq_array, &element_type)? {
                return Ok(false);
            }
        } else {
            return Ok(false);
        }
    }
    return Ok(true);
}

// ---------------------------------------------------------------------
// 配列 (XSeqArray) の各要素の型が、element_type に合致するか否かを判定する。
//
fn match_array_sequence_type(xseq_array: &XSeqArray, element_type: &XNodePtr) -> Result<bool, Box<Error>> {
    for i in 1 ..= xseq_array.array_size() {
        let index = new_xitem_integer(i as i64);
        if let Some(item) = xseq_array.array_get(&index) {
            if ! match_sequence_type(&item, &element_type)? {
                return Ok(false);
            }
        } else {
            return Ok(false);
        }
    }
    return Ok(true);
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::MapTest である場合。
//
// xseq の要素すべてについて、マップであって、そのキーおよび値のシーケンス型も
// 定義に合致していることを確かめる。
//
fn match_sequence_map_test(xseq: &XSequence, type_xnode: &XNodePtr) -> Result<bool, Box<Error>> {
    let key_type = get_xnode_name(&get_left(&type_xnode));
    let value_type = get_right(&type_xnode);
    for xitem in xseq.iter() {
        if let Ok(xseq_map) = xitem.get_as_raw_map() {
            if ! match_map_sequence_type(&xseq_map, &key_type, &value_type)? {
                return Ok(false);
            }
        } else {
            return Ok(false);
        }
    }
    return Ok(true);
}

// ---------------------------------------------------------------------
// マップ (XSeqMap) のキーと値の型が、key_type、value_type に
// 合致するか否かを判定する。
//
fn match_map_sequence_type(xseq_map: &XSeqMap,
                key_type: &str,
                value_type: &XNodePtr) -> Result<bool, Box<Error>> {
    for key in xseq_map.map_keys().iter() {
        if ! derives_from(key.xs_type().as_str(), &key_type) {
            return Ok(false);
        }
        if let Some(val) = xseq_map.map_get(&key) {
            if ! match_sequence_type(&val, &value_type)? {
                return Ok(false);
            }
        } else {
            return Ok(false);
        }
    }
    return Ok(true);
}

// ---------------------------------------------------------------------
// シーケンス xseq の型を、シーケンス型の定義 type_xnode と照合する。
// type_xnode が XNodeType::AtomicOrUnionType である場合。
//
fn match_sequence_atomic_or_union_type(xseq: &XSequence, type_xnode: &XNodePtr) -> bool {
    let type_name = get_xnode_name(type_xnode);
    for xitem in xseq.iter() {
        if ! derives_from(xitem.xs_type().as_str(), &type_name) {
            return false;
        }
    }
    return true;
}

// ---------------------------------------------------------------------
// シーケンスに含まれるアイテムの個数 (OccurenceIndicator) を照合する。
// indicator: ? | * | + | ""
//
fn match_occurence(xseq: &XSequence, indicator: &str) -> Result<bool, Box<Error>> {
    let len = xseq.len();
    match indicator {
        "?" => return Ok(len <= 1),
        "*" => return Ok(true),
        "+" => return Ok(1 <= len),
        ""  => return Ok(len == 1),
        _   => return Err(cant_occur!(
                        "match_occurence: bad indicator \"{}\".",
                        indicator)),
    }
}

// ---------------------------------------------------------------------
// 2.5.6.1 The judgement subtype(A, B)
//
// SequenceType A が、SequenceType B のサブタイプであるか否か判定する。
//
const S_EMPTY_SEQ: usize = 0;
const S_OCCUR_Q: usize = 1;
const S_OCCUR_A: usize = 2;
const S_OCCUR_1: usize = 3;
const S_OCCUR_P: usize = 4;
const S_XS_ERROR: usize = 5;
const SUBTYPE_TBL: [ [i64; 6]; 6 ] = [
    [ 1, 1, 1, 0, 0, 0 ],
    [ 0, 9, 9, 0, 0, 0 ],
    [ 0, 0, 9, 0, 0, 0 ],
    [ 0, 9, 9, 9, 9, 0 ],
    [ 0, 0, 9, 0, 9, 0 ],
    [ 1, 1, 1, 1, 1, 1 ],
];

fn subtype(a: &XNodePtr, b: &XNodePtr) -> bool {
    match SUBTYPE_TBL[subtype_entry(a)][subtype_entry(b)] {
        0 => return false,
        1 => return true,
        _ => return subtype_itemtype(&get_left(a), &get_left(b)),
    }
}

fn subtype_entry(xnode: &XNodePtr) -> usize {
    // assert: get_xnode_type(xnode) == XNodeType::SequenceType

    let type_xnode = get_left(xnode);
    if get_xnode_type(&type_xnode) == XNodeType::EmptySequenceTest {
        return S_EMPTY_SEQ;
    }
    if get_xnode_type(&type_xnode) == XNodeType::AtomicOrUnionType &&
       get_xnode_name(&type_xnode) == "xs:error" {
        match get_xnode_name(xnode).as_str() {
            "?" => return S_EMPTY_SEQ,
            "*" => return S_EMPTY_SEQ,
            ""  => return S_XS_ERROR,
            "+" => return S_XS_ERROR,
            _   => return S_XS_ERROR,
        }
    } else {
        match get_xnode_name(xnode).as_str() {
            "?" => return S_OCCUR_Q,
            "*" => return S_OCCUR_A,
            ""  => return S_OCCUR_1,
            "+" => return S_OCCUR_P,
            _   => return S_OCCUR_1,
        }
    }
}

// ---------------------------------------------------------------------
// 2.5.6.2 The judgement subtype-itemtype(Ai, Bi)
//
// ItemType Ai が、ItemType Bi のサブタイプであるか否か判定する。
// 
// ai, bi: xnode (XNodeType::SequenceType) の左辺値であるXNodePtr。
//
fn subtype_itemtype(ai: &XNodePtr, bi: &XNodePtr) -> bool {

    // -----------------------------------------------------------------
    // 1. Ai and Bi are AtomicOrUnionTypes, and derives-from(Ai, Bi) returns true.
    // 2. Ai is a pure union type, and every type t in the transitive membership of Ai satisfies subtype-itemType(t, Bi).
    // 3. Ai is xs:error and Bi is a generalized atomic type.
    //
    if get_xnode_type(ai) == XNodeType::AtomicOrUnionType &&
       get_xnode_type(bi) == XNodeType::AtomicOrUnionType {
        if derives_from(&get_xnode_name(ai), &get_xnode_name(bi)) { // 1. 2.
            return true;
        }
        if get_xnode_name(ai) == "xs:error" {               // 3.
            return true;
        }
    }

    // -----------------------------------------------------------------
    // 4. Bi is item().
    //
    if get_xnode_type(bi) == XNodeType::ItemTest {
        return true;
    }

    // -----------------------------------------------------------------
    // BiがKindTestのとき:
    // 5. Bi is node() and Ai is a KindTest.
    // 6. Bi is text() and Ai is also text().
    // 7. Bi is comment() and Ai is also comment().
    // 8. Bi is namespace-node() and Ai is also namespace-node().
    // 9. Bi is processing-instruction() and
    //    Ai is either processing-instruction() or
    //                 processing-instruction(N) for any name N.
    // 10. Bi is processing-instruction(Bn), and
    //     Ai is also processing-instruction(Bn).
    //
//  --------------------------------------------------------------------- ☆
    // 11. Bi is document-node() and Ai is either document-node() or document-node(E) for any ElementTest E.
    // 12. Bi is document-node(Be) and Ai is document-node(Ae), and subtype-itemtype(Ae, Be).

    // 13. Bi is either element() or element(*), and Ai is an ElementTest.
    // 14. Bi is either element(Bn) or element(Bn, xs:anyType?),
    //     the expanded QName of An equals the expanded QName of Bn,
    //     and Ai is either element(An) or element(An, T) or element(An, T?) for any type T.
    // 15. Bi is element(Bn, Bt), the expanded QName of An equals the expanded QName of Bn, Ai is element(An, At), and derives-from(At, Bt) returns true.
    // 16. Bi is element(Bn, Bt?), the expanded QName of An equals the expanded QName of Bn, Ai is either element(An, At) or element(An, At?), and derives-from(At, Bt) returns true.
    // 17. Bi is element(*, Bt), Ai is either element(*, At) or element(N, At) for any name N, and derives-from(At, Bt) returns true.
    // 18. Bi is element(*, Bt?), Ai is either element(*, At), element(*, At?), element(N, At), or element(N, At?) for any name N, and derives-from(At, Bt) returns true.

    // 19. Bi is schema-element(Bn), Ai is schema-element(An), and every element declaration that is an actual member of the substitution group of An is also an actual member of the substitution group of Bn.
    //    Note:
    //    The fact that P is a member of the substitution group of Q does not mean that every element declaration in the substitution group of P is also in the substitution group of Q. For example, Q might block substitution of elements whose type is derived by extension, while P does not.

    // 20. Bi is either attribute() or attribute(*), and Ai is an AttributeTest.
    // 21. Bi is either attribute(Bn) or attribute(Bn, xs:anyType), the expanded QName of An equals the expanded QName of Bn, and Ai is either attribute(An), or attribute(An, T) for any type T.
    // 22. Bi is attribute(Bn, Bt), the expanded QName of An equals the expanded QName of Bn, Ai is attribute(An, At), and derives-from(At, Bt) returns true.
    // 23. Bi is attribute(*, Bt), Ai is either attribute(*, At), or attribute(N, At) for any name N, and derives-from(At, Bt) returns true.

    // 24. Bi is schema-attribute(Bn), the expanded QName of An equals the expanded QName of Bn, and Ai is schema-attribute(An).
    //
    if get_xnode_type(bi) == XNodeType::KindTest {
        match get_xnode_type(&get_left(bi)) {
            XNodeType::AnyKindTest => {                     // 5.
                if get_xnode_type(ai) == XNodeType::KindTest {
                    return true;
                }
            },
            XNodeType::TextTest => {                        // 6.
                if get_xnode_type(ai) == XNodeType::KindTest &&
                   get_xnode_type(&get_left(ai)) == XNodeType::TextTest {
                    return true;
                }
            },
            XNodeType::CommentTest => {                     // 7.
                if get_xnode_type(ai) == XNodeType::KindTest &&
                   get_xnode_type(&get_left(ai)) == XNodeType::CommentTest {
                    return true;
                }
            },
            XNodeType::NamespaceNodeTest => {               // 8.
                if get_xnode_type(ai) == XNodeType::KindTest &&
                   get_xnode_type(&get_left(ai)) == XNodeType::NamespaceNodeTest {
                    return true;
                }
            },
            XNodeType::PITest => {                          // 9. 10.
                let pi_name = get_xnode_name(bi);
                if pi_name == "" {
                    if get_xnode_type(ai) == XNodeType::KindTest &&
                       get_xnode_type(&get_left(ai)) == XNodeType::PITest {
                        return true;
                    }
                } else {
                    if get_xnode_type(ai) == XNodeType::KindTest &&
                       get_xnode_type(&get_left(ai)) == XNodeType::PITest &&
                       get_xnode_name(&get_left(ai)) == pi_name {
                        return true;
                    }
                }
            },
            _ => {},
        }
    }

    // -----------------------------------------------------------------
    // 25. Bi is function(*), Ai is a FunctionTest.
    //
    if get_xnode_type(bi) == XNodeType::AnyFunctionTest {
        match get_xnode_type(ai) {
            XNodeType::AnyFunctionTest |
            XNodeType::TypedFunctionTest => {
                return true;
            },
            _ => {},
        }
    }

    // -----------------------------------------------------------------
    // 26. Bi is function(Ba_1, Ba_2, ... Ba_N) as Br,
    //     Ai is function(Aa_1, Aa_2, ... Aa_M) as Ar,
    //   where
    //     N (arity of Bi) equals M (arity of Ai);
    //     subtype(Ar, Br);
    //     and for values of I between 1 and N, subtype(Ba_I, Aa_I). 
    //
    if get_xnode_type(bi) == XNodeType::TypedFunctionTest &&
       get_xnode_type(ai) == XNodeType::TypedFunctionTest {

        let mut bi_signature_xnode: Vec<XNodePtr> = vec!{};
        let mut curr = get_right(&bi);
        while ! is_nil_xnode(&curr) {
            bi_signature_xnode.push(get_left(&curr));
            curr = get_right(&curr);
        }

        let mut ai_signature_xnode: Vec<XNodePtr> = vec!{};
        let mut curr = get_right(&ai);
        while ! is_nil_xnode(&curr) {
            ai_signature_xnode.push(get_left(&curr));
            curr = get_right(&curr);
        }

        if bi_signature_xnode.len() == ai_signature_xnode.len() {
            let mut is_fail = false;
            if ! subtype(&ai_signature_xnode[0], &bi_signature_xnode[0]) {
                is_fail = true;
            }
            for i in 1 .. bi_signature_xnode.len() {
                if ! subtype(&bi_signature_xnode[i], &ai_signature_xnode[i]) {
                    is_fail = true;
                }
            }
            if ! is_fail {
                return true;
            }
        }
    }

    // -----------------------------------------------------------------
    // 27. Ai is map(K, V), for any K and V and Bi is map(*).
    // 28. Ai is map(Ka, Va) and Bi is map(Kb, Vb),
    //     where subtype-itemtype(Ka, Kb) and subtype(Va, Vb).
    //
    // 27のBiは、map(xs:anyAtomicType, item()*) と同等として
    // 構文木が作られているので、28.の場合のみ考えればよい。
    // AiとBiそれぞれについて、キーと値のxnodeを取り出し、subtype関係を調べる。
    //
    //     MapTest ------- SequenceType ({ai,bi}_val)
    //        |                ...
    //        | ({ai,bi}_key)
    // AtomicOrUnionType
    //      (...)
    //
    if get_xnode_type(ai) == XNodeType::MapTest &&
       get_xnode_type(bi) == XNodeType::MapTest {
        let ai_key = get_left(&ai);
        let ai_val = get_right(&ai);
        let bi_key = get_left(&bi);
        let bi_val = get_right(&bi);
        if subtype_itemtype(&ai_key, &bi_key) &&
           subtype(&ai_val, &bi_val) {
            return true;
        }
    }

    // -----------------------------------------------------------------
    // 29. Ai is map(*)
    //       (or, because of the transitivity rules, any other map type),
    //     and Bi is function(*).
    //
    if get_xnode_type(ai) == XNodeType::MapTest &&
       get_xnode_type(bi) == XNodeType::AnyFunctionTest {
        return true;
    }

    // -----------------------------------------------------------------
    // 30. Ai is map(*)
    //       (or, because of the transitivity rules, any other map type),
    //     and Bi is function(xs:anyAtomicType) as item()*.
    // 35. Ai is map(K, V), and Bi is function(xs:anyAtomicType) as V?.
    //
    // 30のAiがmap(*)の場合、map(xs:anyAtomicType, item()*) と同等として
    // 構文木が作られているので、特別扱いする必要はない。
    // AiとBiそれぞれについて、キーと値のxnodeを取り出し、subtype関係を調べる。
    //
    //     MapTest ------- SequenceType (ai_val)
    //        |                ...
    //        | (ai_key)
    // AtomicOrUnionType
    //      (...)
    //
    // TypedFunctionTest --- ReturnType ---------------- Param
    //                           |                         |
    //                      SequenceType (bi_val)     SequenceType
    //                                                     |
    //                                                     |   (bi_key)
    //                                              AtomicOrUnionType
    //                                                   (...)
    //
    if get_xnode_type(ai) == XNodeType::MapTest &&
       get_xnode_type(bi) == XNodeType::TypedFunctionTest {
        let ai_key = get_left(&ai);                 // AtomicOrUnionType
        let ai_val = get_right(&ai);                // SequenceType
        let ret_xnode = get_right(&bi);           // ReturnType
        let bi_val = get_left(&ret_xnode);          // SequenceType
        let param_xnode = get_right(&ret_xnode);  // Param
        let seq_xnode = get_left(&param_xnode);   // SequenceType
        let bi_key = get_left(&seq_xnode);          // AtomicOrUnionType
        if subtype_itemtype(&ai_key, &bi_key) &&
           subtype(&ai_val, &bi_val) {
            return true;
        }
    }

    // -----------------------------------------------------------------
    // 31. Ai is array(X) and Bi is array(*).
    // 32. Ai is array(X) and Bi is array(Y), and subtype(X, Y) is true.
    //
    // 31.のarray(*)はarray(item()*) と同等として
    // 構文木が作られているので、32の場合のみ考えればよい。
    // AiとBiそれぞれについて、要素のxnodeを取り出し、subtype関係を調べる。
    //
    //   ArrayTest
    //       |
    //       | ({ai,bi}_elem)
    //  SequenceType
    //      ...
    //
    if get_xnode_type(ai) == XNodeType::ArrayTest &&
       get_xnode_type(bi) == XNodeType::ArrayTest {
        let ai_elem = get_left(&ai);
        let bi_elem = get_left(&bi);
        if subtype(&ai_elem, &bi_elem) {
            return true;
        }
    }

    // -----------------------------------------------------------------
    // 33. Ai is array(*)
    //       (or, because of the transitivity rules, any other array type)
    //     and Bi is function(*).
    //
    if get_xnode_type(ai) == XNodeType::ArrayTest &&
       get_xnode_type(bi) == XNodeType::AnyFunctionTest {
        return true;
    }

    // -----------------------------------------------------------------
    // 34. Ai is array(*)
    //       (or, because of the transitivity rules, any other array type)
    //     and Bi is function(xs:integer) as item()*.
    // 36. Ai is array(X) and Bi is function(xs:integer) as X.
    //
    // 34のAiがarray(*)の場合、array(item()*) と同等として
    // 構文木が作られているので、特別扱いする必要はない。
    // AiとBiそれぞれについて、要素のxnodeを取り出し、subtype関係を調べる。
    // 
    if get_xnode_type(ai) == XNodeType::ArrayTest &&
       get_xnode_type(bi) == XNodeType::TypedFunctionTest {
        let ai_elem = get_left(&ai);                // SequenceType
        let ret_xnode = get_right(&bi);           // ReturnType
        let bi_elem = get_left(&ret_xnode);         // SequenceType
        let param_xnode = get_right(&ret_xnode);  // Param
        let seq_xnode = get_left(&param_xnode);   // SequenceType
        let bi_arg = get_left(&seq_xnode);          // AtomicOrUnitonType
        if derives_from("xs:integer", &get_xnode_name(&bi_arg)) &&
           subtype(&ai_elem, &bi_elem) {
            return true;
        }
    }

    // -----------------------------------------------------------------
    // 以上のどの条件も満たさない場合: サブタイプではない。
    //
    return false;
}

// ---------------------------------------------------------------------
//
fn derives_from(ai: &str, bi: &str) -> bool {
    let derives_from_map: HashMap<&str, &str> = [
        ( "xs:integer",       "xs:decimal" ),
        ( "xs:decimal",       "xs:numeric" ),
        ( "xs:double",        "xs:numeric" ),
                // numericは、実際には union {decimal, float, double}
        ( "xs:numeric",       "xs:anyAtomicType" ),
        ( "xs:string",        "xs:anyAtomicType" ),
        ( "xs:anyURI",        "xs:string" ),
                // anyURIは常にstringに昇格可能
        ( "xs:boolean",       "xs:anyAtomicType" ),
        ( "xs:untypedAtomic", "xs:anyAtomicType" ),
        ( "xs:anyAtomicType", "xs:anySimpleType" ),
        ( "xs:anySimpleType", "xs:anyType" ),
        ( "xs:untyped",       "xs:anyType" ),
    ].iter().cloned().collect();

    let mut t_type = String::from(ai);
    loop {
        if t_type.as_str() == bi {
            return true;
        }
        match derives_from_map.get(t_type.as_str()) {
            Some(s) => t_type = String::from(*s),
            None => return false,
        }
    }
}

// ---------------------------------------------------------------------
// XNodeType::{Map,SquareArray,CurlyArray} が指す内容を
// XItem::{XIMap,XIArray} に変換する。
//
fn convert_xnode_to_map_array(xnode: &XNodePtr,
                context_xseq: &XSequence,
                eval_env: &mut EvalEnv) -> Result<XItem, Box<Error>> {
    match get_xnode_type(&xnode) {
        XNodeType::Map => {
            // ---------------------------------------------------------
            // マップの実体を取り出す。
            //    Map
            //     |
            // MapConsruct -------------- MapConstruct ---...
            //     |                          |
            //  MapEntry --- (value)       MapEntry --- (value)
            //     |                          |
            //   (key)                      (key)
            //
            let map_construct_xnode = get_left(&xnode);
            let mut curr = map_construct_xnode.clone();
            let mut vec_item: Vec<(XItem, XSequence)> = vec!{};
            while ! is_nil_xnode(&curr) {
                if get_xnode_type(&curr) != XNodeType::MapConstruct {
                    return Err(cant_occur!(
                    "convert_xnode_to_map_array[Map]: xnode = {}, not MapConstruct",
                        get_xnode_type(&curr)));
                }

                let map_entry_xnode = get_left(&curr);
                if get_xnode_type(&map_entry_xnode) != XNodeType::MapEntry {
                    return Err(cant_occur!(
                    "convert_xnode_to_map_array[Map]: xnode = {}, not MapEntry",
                        get_xnode_type(&map_entry_xnode)));
                }

                let map_key_xnode = get_left(&map_entry_xnode);
                let key = evaluate_xnode(context_xseq, &map_key_xnode, eval_env)?.get_singleton_item()?;

                let map_value_xnode = get_right(&map_entry_xnode);
                let val = evaluate_xnode(context_xseq, &map_value_xnode, eval_env)?;
                vec_item.push((key, val));

                curr = get_right(&curr);
            }
            return Ok(new_xitem_map(&vec_item));

        },

        XNodeType::SquareArray => {
            // ---------------------------------------------------------
            // 配列の実体を取り出す。
            // SquareArray
            //     |
            //  ArrayEntry --- ArrayEntry ---...
            //     |              |
            //   (expr)         (expr)
            //
            let array_entry_xnode = get_left(&xnode);
            let mut curr = array_entry_xnode.clone();
            let mut vec_item: Vec<XSequence> = vec!{};
            while ! is_nil_xnode(&curr) {
                if get_xnode_type(&curr) != XNodeType::ArrayEntry {
                    return Err(cant_occur!(
                    "convert_xnode_to_map_array[SquareArray]: xnode = {}, not ArrayEntry",
                        get_xnode_type(&curr)));
                }

                let value_xnode = get_left(&curr);
                let val = evaluate_xnode(context_xseq, &value_xnode, eval_env)?;
                vec_item.push(val);
                curr = get_right(&curr);
            }
            return Ok(new_xitem_array(&vec_item));
        },

        XNodeType::CurlyArray => {
            // ---------------------------------------------------------
            // 配列の実体を取り出す。
            //
            let array_entry_xnode = get_left(&xnode);
            let val_xseq = evaluate_xnode(context_xseq, &array_entry_xnode, eval_env)?;
            let mut vec_item: Vec<XSequence> = vec!{};
            for item in val_xseq.iter() {
                vec_item.push(new_singleton(item));
            }
            return Ok(new_xitem_array(&vec_item));
        },

        _ => {
            return Err(cant_occur!(
                "convert_xnode_to_map_array: xnode = {}",
                get_xnode_type(&xnode)));
        },
    }
}

// =====================================================================
//
#[cfg(test)]
mod test {
//    use super::*;

    use xpath_impl::helpers::compress_spaces;
    use xpath_impl::helpers::subtest_eval_xpath;
    use xpath_impl::helpers::subtest_xpath;


    // -----------------------------------------------------------------
    // Comma
    //
    #[test]
    fn test_comma() {
        let xml = compress_spaces(r#"
<root img="basic" base="base">
    <a img="a" />
    <b img="b1" />
    <b img="b2" />
    <c img="c" />
    <d img="d" />
</root>
        "#);

        subtest_eval_xpath("comma", &xml, &[
            ( "//a, //c", r#"(<a img="a">, <c img="c">)"# ),
            ( "2, 3", r#"(2, 3)"# ),
            ( "(2, 3)", r#"(2, 3)"# ),
            ( "2, 1 + 3", r#"(2, 4)"# ),
            ( "(2, (3, 4))", r#"(2, 3, 4)"# ),
        ]);
    }

    // -----------------------------------------------------------------
    // if ( Expr ) then ExprSingle else ExprSingle
    //
    #[test]
    fn test_if_expr() {
        let xml = compress_spaces(r#"
<root base="base">
    <prod discount="discount">
        <wholesale id="wa">wholesaled apple</wholesale>
        <wholesale id="wb">wholesaled banana</wholesale>
        <retail id="ra">retailed apple</retail>
        <retail id="rb">retailed banana</retail>
    </prod>
    <item>
        <wholesale id="wa">wholesaled apple</wholesale>
        <wholesale id="wb">wholesaled banana</wholesale>
        <retail id="ra">retailed apple</retail>
        <retail id="rb">retailed banana</retail>
    </item>
</root>
        "#);

        subtest_eval_xpath("if_expr", &xml, &[
            ( "if (1 = 1) then 3 else 5", "3" ),
            ( "if (1 = 9) then 3 else 5", "5" ),
            ( "if (prod/@discount) then prod/wholesale else prod/retail",
              r#"(<wholesale id="wa">, <wholesale id="wb">)"# ),
            ( "if (item/@discount) then item/wholesale else item/retail",
              r#"(<retail id="ra">, <retail id="rb">)"# ),
                ]);
    }

    // -----------------------------------------------------------------
    // for $VarName in ExprSingle return ExprSingle
    //
    #[test]
    fn test_for_expr() {
        let xml = compress_spaces(r#"
<root base="base">
    <a v="x"/>
    <a v="y"/>
    <a v="z"/>
</root>
        "#);

        subtest_eval_xpath("for_expr", &xml, &[
            ( "for $x in 3 to 5 return $x * 2", "(6, 8, 10)" ),
            ( "for $x in 3 to 5, $y in 2 to 3 return $x * $y", "(6, 9, 8, 12, 10, 15)" ),
            ( "/root/a/@v", r#"(v="x", v="y", v="z")"# ),
            ( "for $aa in /root/a return $aa", r#"(<a v="x">, <a v="y">, <a v="z">)"# ),
            ( "for $aa in /root/a return $aa/@v", r#"(v="x", v="y", v="z")"# ),
        ]);
    }

    // -----------------------------------------------------------------
    // some $VarName in ExprSingle satisfies ExprSingle
    //
    #[test]
    fn test_some_expr() {
        let xml = compress_spaces(r#"
<root base="base">
    <a v="x"/>
    <a v="y"/>
    <a v="z"/>
</root>
        "#);

        subtest_eval_xpath("some_expr", &xml, &[
            ( "some $x in 3 to 5 satisfies $x mod 2 = 0", "true" ),
            ( "some $x in 3 to 5 satisfies $x mod 6 = 0", "false" ),
            ( "some $x in 1 to 2, $y in 2 to 3 satisfies $x + $y = 5", "true" ),
            ( "some $x in 1 to 2, $y in 2 to 3 satisfies $x + $y = 7", "false" ),
            ( r#"some $a in /root/a satisfies $a/@v = "y""#, "true" ),
            ( r#"some $a in /root/a satisfies $a/@v = "w""#, "false" ),
        ]);
    }

    // -----------------------------------------------------------------
    // every $VarName in ExprSingle satisfies ExprSingle
    //
    #[test]
    fn test_every_expr() {
        let xml = compress_spaces(r#"
<root base="base">
    <a v="x"/>
    <a v="y"/>
    <a v="z"/>
</root>
        "#);

        subtest_eval_xpath("every_expr", &xml, &[
            ( "every $x in 3 to 5 satisfies $x > 2", "true" ),
            ( "every $x in 3 to 5 satisfies $x > 3", "false" ),
            ( "every $x in 1 to 2, $y in 2 to 3 satisfies $x + $y > 2", "true" ),
            ( "every $x in 1 to 2, $y in 2 to 3 satisfies $x + $y > 4", "false" ),
            ( r#"every $a in /root/a satisfies $a/@v != "w""#, "true" ),
            ( r#"every $a in /root/a satisfies $a/@v = "y""#, "false" ),
        ]);
    }

    // -----------------------------------------------------------------
    // castable as
    //
    #[test]
    fn test_castable_as() {
        let xml = compress_spaces(r#"
<root base="base">
    <a v="x"/>
    <a v="y"/>
    <a v="z"/>
</root>
        "#);

        subtest_eval_xpath("castable_as", &xml, &[
            ( "100 castable as string", "true" ),
            ( "100 castable as string?", "true" ),
            ( r#"/root/empty castable as string"#, "false" ),
            ( r#"/root/empty castable as string?"#, "true" ),
            ( r#"/root/a[@v="x"] castable as string"#, "true" ),
            ( r#"/root/a[@v="x"] castable as string?"#, "true" ),
            ( r#"/root/a castable as string"#, "false" ),
            ( r#"/root/a castable as string?"#, "false" ),
        ]);
    }

    // -----------------------------------------------------------------
    // cast as
    //
    #[test]
    fn test_cast_as() {
        let xml = compress_spaces(r#"
<root base="base">
    <a v="x"/>
    <a v="y"/>
    <a v="z"/>
</root>
        "#);

        subtest_eval_xpath("cast_as", &xml, &[
            ( r#"/root/empty cast as string?"#, "()" ),
            ( r#"/root/a[@v="x"] castable as string"#, "true" ),
        ]);
    }

    // -----------------------------------------------------------------
    // 軸: following
    //
    #[test]
    fn test_axis_following() {
        let xml = compress_spaces(r#"
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <foo img="上">
        <foo img="甲"/>
        <baa img="乙"/>
        <foo img="上上" base="base">
            <foo img="丙"/>
            <baa img="丁"/>
        </foo>
        <foo img="戊"/>
    </foo>
    <foo img="下">
        <baa img="己"/>
        <foo img="庚"/>
        <foo img="下下">
            <baa img="辛"/>
            <foo img="壬"/>
        </foo>
        <baa img="癸"/>
    </foo>
</root>
        "#);

        subtest_xpath("axis_following", &xml, false, &[
            ( "following::*", "戊下己庚下下辛壬癸" ),
            ( "following::foo", "戊下庚下下壬" ),
            ( "following::foo[1]", "" ),
            ( "following::baa", "己辛癸" ),
            ( "following::baa[1]", "" ),
        ]);
    }

    // -----------------------------------------------------------------
    // 軸: preceding
    //
    #[test]
    fn test_axis_preceding() {
        let xml = compress_spaces(r#"
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <foo img="上">
        <foo img="甲"/>
        <baa img="乙"/>
        <foo img="上上">
            <foo img="丙"/>
            <baa img="丁"/>
        </foo>
        <foo img="戊"/>
    </foo>
    <foo img="下">
        <baa img="己"/>
        <foo img="庚" base="base"/>
        <baa img="辛"/>
        <foo img="壬"/>
        <baa img="癸"/>
    </foo>
</root>
        "#);

        subtest_xpath("axis_preceding", &xml, false, &[
            ( "preceding::*", "上甲乙上上丙丁戊己" ),
            ( "preceding::foo", "上甲上上丙戊" ),
            ( "preceding::foo[1]", "" ),
            ( "preceding::baa", "乙丁己" ),
            ( "preceding::baa[1]", "" ),
        ]);
    }

    // -----------------------------------------------------------------
    // element() | element(*) | element(sel)
    // element(sel, type_anno) | element(sel, type_anno?)
    //
    #[test]
    fn test_kind_test_element() {
        let xml = compress_spaces(r#"
<root>
    <a base="base">
        <sel img="z0"/>
        <sel img="z1"/>
        <sel img="z2" xsi:nil="true" />
        <alt img="a0"/>
        <alt img="a1"/>
    </a>
</root>
        "#);

        subtest_eval_xpath("kind_test_element", &xml, &[
            ( "count(child::element())", "5" ),
            ( "count(child::element(*))", "5" ),
            ( "count(child::element(sel))", "3" ),
            ( "count(child::element(sel, anyType))", "2" ),
            ( "count(child::element(sel, anyType?))", "3" ),
            ( "count(child::element(sel, bad))", "0" ),
            ( "count(child::element(sel, bad?))", "0" ),
        ]);
    }

    // -----------------------------------------------------------------
    // attribute() | attribute(*) | attribute(a)
    // attribute(sel, type_anno)
    //
    #[test]
    fn test_kind_test_attribute() {
        let xml = compress_spaces(r#"
<root>
    <a base="base">
        <sel a="1" b="2"/>
    </a>
</root>
        "#);

        subtest_eval_xpath("kind_test_attribute", &xml, &[
            ( "sel/attribute::attribute()", r#"(a="1", b="2")"# ),
            ( "sel/attribute::attribute(*)", r#"(a="1", b="2")"# ),
            ( "sel/attribute::attribute(a)", r#"a="1""# ),
            ( "sel/attribute::attribute(a, anyType)", r#"a="1""# ),
            ( "sel/attribute::attribute(a, BAD)", r#"()"# ),
        ]);
    }

    // -----------------------------------------------------------------
    // processing-instruction()
    //
    #[test]
    fn test_kind_test_processing_instruction() {
        let xml = compress_spaces(r#"
<?xml version='1.0' encoding='UTF-8'?>
<?style-sheet alt="1" src="sample.css"?>
<?style-sheet alt="2" src="default.css"?>
<?pseudo-style-sheet src="sample.css"?>
<xroot>
    <a base="base">
        <sel img="z0" ans="0" />
        <sel img="z1" ans="1" />
        <sel img="z2" ans="2" />
        <sel img="z3" ans="3" />
        <sel img="z4" ans="4" />
    </a>
</xroot>
        "#);

        subtest_eval_xpath("kind_test_processing_instruction", &xml, &[
            ( "count(/child::processing-instruction())", "3" ),
            ( "count(/child::processing-instruction('style-sheet'))", "2" ),
        ]);
    }

    // -----------------------------------------------------------------
    // ContextItemExpr
    //
    #[test]
    fn test_context_item() {
        let xml = compress_spaces(r#"
<root>
    <a base="base">
        <b id="b"/>
    </a>
</root>
        "#);

        subtest_eval_xpath("context_item", &xml, &[
            ( ".", r#"<a base="base">"# ),
            ( "./b", r#"<b id="b">"# ),
            ( "self::a", r#"<a base="base">"# ),
            ( r#"self::a[@base="base"]"#, r#"<a base="base">"# ),
            ( "self::b", "()" ),
                    // 「self」と明記した場合はAxisSelfであり、
                    // NodeTestを記述できる。
            ( ".::a", "Syntax Error in XPath" ),
            ( ".a", "Syntax Error in XPath" ),
                    // 「.」と書き、さらにNodeTestを記述する構文はない。
            ( r#".[name()="a"]"#, r#"<a base="base">"# ),
            ( r#".[@base="base"]"#, r#"<a base="base">"# ),
                    // しかし述語は記述できる。

            ( "(1 to 20)[. mod 5 eq 0]", "(5, 10, 15, 20)" ),
        ]);
    }

    // -----------------------------------------------------------------
    // OperatorConcat
    //
    #[test]
    fn test_operator_concat() {
        let xml = compress_spaces(r#"
<root>
    <a base="base">
        <b id="b"/>
    </a>
</root>
        "#);

        subtest_eval_xpath("operator_concat", &xml, &[
            ( r#" "あい" || "うえ" "#, r#""あいうえ""# ),
            ( r#" 123 || 456 || 789 "#, r#""123456789""# ),
        ]);
    }

    // -----------------------------------------------------------------
    // OperatorMap
    //
    #[test]
    fn test_operator_map() {
        let xml = compress_spaces(r#"
<root>
    <z>
        <a base="base">
            <b>b1</b>
            <b>b2</b>
        </a>
    </z>
</root>
        "#);

        subtest_eval_xpath("operator_map", &xml, &[
            ( r#"sum((1, 3, 5)!(.*.)) "#, r#"35"# ),
            ( r#"string-join((1 to 4) ! "*") "#, r#""****""# ),
            ( r#"string-join((1 to 4) ! "*", ".") "#, r#""*.*.*.*""# ),
            ( r#"child::b/string()!concat("id-", .)"#, r#"("id-b1", "id-b2")"# ),
            ( r#"string-join(ancestor::*!name(), '/')"#, r#""root/z""# ),
        ]);
    }

    // -----------------------------------------------------------------
    // ArrowExpr
    //
    #[test]
    fn test_arrow_expr() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("arrow_expr", &xml, &[
            ( r#" 'aBcDe' => upper-case() => substring(2, 3)"#, r#""BCD""# ),
            ( "let $f := function($a) { $a * $a } return 5 => $f() ", "25" ),

        ]);
    }

    // -----------------------------------------------------------------
    // LetExpr
    //
    #[test]
    fn test_let_expr() {
        let xml = compress_spaces(r#"
<root>
    <z>
        <a base="base">
            <b>b1</b>
            <b>b2</b>
        </a>
    </z>
</root>
        "#);

        subtest_eval_xpath("let_expr", &xml, &[
            ( r#"let $x := 4, $y := 3 return $x + $y"#, r#"7"# ),
            ( r#"let $x := 4, $y := $x * 2 return $x + $y"#, r#"12"# ),
        ]);
    }

    // -----------------------------------------------------------------
    // InlineFunction
    //
    #[test]
    fn test_inline_function() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("inline_function", &xml, &[
            ( "let $f := function() { 4 } return $f() ", "4" ),
            ( "let $f := function($n as xs:integer) { $n * 3 } return $f(5) ", "15" ),
            ( r#"let $x := function ($m as integer, $n as integer) { ($m + $n) * 3 } return $x(2, 4) "#, r#"18"# ),
            ( "for-each(1 to 4, function($x as xs:integer) { $x * $x })", "(1, 4, 9, 16)" ),
            ( "for-each(1 to 4, function($x as node()) { $x })", "Type Error" ),
        ]);
    }

    // -----------------------------------------------------------------
    // NamedFunctionRef
    //
    #[test]
    fn test_named_function_ref() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("named_function_ref", &xml, &[
            ( r#"for-each(("john", "jane"), fn:string-to-codepoints#1)"#,
                        "(106, 111, 104, 110, 106, 97, 110, 101)" ),
        ]);
    }

    // -----------------------------------------------------------------
    // PartialFunctionCall / ArgumentPlaceholder
    //
    #[test]
    fn test_partial_function_call() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("partial_function_call", &xml, &[
            ( r#"for-each(("a", "b"), fn:starts-with(?, "a")) "#,
                        "(true, false)" ),
        ]);
    }

    // -----------------------------------------------------------------
    // Map
    //
    #[test]
    fn test_map_lookup() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("map_lookup", &xml, &[
            ( r#"
                let $week := map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } return $week("Su")
              "#, r#""Sunday""# ),
            ( r#"
                let $bk := map {
                    "a" : map {
                        "a1" : "A1",
                        "a2" : "A2"
                    },
                    "b" : map {
                        "b1" : "B1",
                        "b2" : "B2"
                    }
                } return $bk("a")("a2")
              "#, r#""A2""# ),
        ]);
    }

    // -----------------------------------------------------------------
    // Array
    //
    #[test]
    fn test_array_lookup() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("array_lookup", &xml, &[
            ( r#"[ 1, 3, 5, 7 ](4)"#, "7" ),
            ( r#"[ [1, 2, 3], [4, 5, 6]](2)"#, "[4, 5, 6]" ),
            ( r#"[ [1, 2, 3], [4, 5, 6]](2)(2)"#, "5" ),
            ( r#"array{ (1), (2, 3), (4, 5) }(4)"#, "4" ),
        ]);
    }

    // -----------------------------------------------------------------
    // UnaryLookup
    //
    #[test]
    fn test_unary_lookup() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("unary_lookup", &xml, &[
            // NCName
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                }[.("Su") = "Sunday"]
              "#, r#"{"Su" => "Sunday", "Mo" => "Monday"}"# ),
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                }[?Su = "Sunday"]
              "#, r#"{"Su" => "Sunday", "Mo" => "Monday"}"# ),

            // NCName
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } ! ?Su = "Sunday"
              "#, r#"true"# ),
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } ! ?Su = "Monday"
              "#, r#"false"# ),

            // Wildcard
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } ! (for $k in map:keys(.) return .($k))
              "#, r#"("Sunday", "Monday")"# ),
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } ! ?*
              "#, r#"("Sunday", "Monday")"# ),
                                    // map {...} ! ?*

            // ParenthesizedExpr
            ( r#"
                map {
                    "Su" : "Sunday",
                    "Mo" : "Monday"
                } ! ?("Mo", "Su")
              "#, r#"("Monday", "Sunday")"# ),

            // IntegerLiteral
            ( r#"[ 1, 3, 5, 7 ][?3 = 5]"#, "[1, 3, 5, 7]" ),
            ( r#"[ 1, 3, 5, 7 ][?3 = 10]"#, "()" ),

            // Wildcard
            ( r#"[ 1, 3, 5, 7 ] ! ?*"#, "(1, 3, 5, 7)" ),

            // ParenthesizedExpr
            ( r#"[ 1, 3, 5, 7 ] ! ?(2 to 4)"#, "(3, 5, 7)" ),
        ]);
    }

    // -----------------------------------------------------------------
    // Postfix Lookup
    //
    #[test]
    fn test_postfix_lookup() {
        let xml = compress_spaces(r#"
<root>
</root>
        "#);

        subtest_eval_xpath("postfix_lookup", &xml, &[
            ( r#"map { "Su" : "Sunday", "Mo" : "Monday" }?Su"#, r#""Sunday""# ),
            ( r#"map { 0: "F", 1: "T" }?1 "#, r#""T""# ),

            ( r#"[4, 5, 6]?2"#, r#"5"# ),
            ( r#"[4, 5, 6]?*"#, r#"(4, 5, 6)"# ),
            ( r#" ([1, 2, 3], [4, 5, 6])?2 "#, "(2, 5)" ),
        ]);
    }

    // -----------------------------------------------------------------
    // instance of
    //
    #[test]
    fn test_instance_of() {
        let xml = compress_spaces(r#"
<root>
    <elem base="base"/>
</root>
        "#);

        subtest_eval_xpath("instance_of", &xml, &[
            ( r#"() instance of empty-sequence() "#, "true" ),
            ( r#"7 instance of empty-sequence() "#, "false" ),

            // AtomicOrUnionType
            ( r#"5 instance of xs:integer "#, "true" ),
            ( r#"(5, 7) instance of xs:integer+ "#, "true" ),
            ( r#"(5, 7.3) instance of xs:decimal+ "#, "true" ),
            ( r#"(5, 7) instance of xs:numeric+ "#, "true" ),
            ( r#"(5, "ss") instance of xs:anyAtomicType+ "#, "true" ),
            ( r#" . instance of element() + "#, "true" ),

            // ArrayTest
            ( r#" [1, 2] instance of array(*) "#, "true" ),
            ( r#" [1, 2] instance of array(xs:integer) "#, "true" ),
            ( r#" [1, 2] instance of array(xs:string) "#, "false" ),
            ( r#" [(1, 2), 2] instance of array(xs:integer) "#, "false" ),
            ( r#" [(1, 2), 2] instance of array(xs:integer+) "#, "true" ),
            ( r#" [[1, 2], [2]] instance of array(array(xs:integer)) "#, "true" ),

            // MapTest
            ( r#" map{"a": 1, "b": "x"} instance of map(*) "#, "true" ),
            ( r#" map{"a": 1, "b": 2} instance of map(string, integer) "#, "true" ),
            ( r#" map{"a": 1, "b": 2} instance of map(string, string) "#, "false" ),
            ( r#" map{"a": [1], "b": [2]} instance of map(string, array(integer)) "#, "true" ),
        ]);
    }

    // -----------------------------------------------------------------
    // instance of function()
    //
    #[test]
    fn test_instance_of_function() {
        let xml = compress_spaces(r#"
<root>
    <elem base="base"/>
</root>
        "#);

        subtest_eval_xpath("instance_of_function", &xml, &[
            // AnyFunctionTest
            ( "7 instance of function(*) ", "false" ),
            ( "[1, 2] instance of function(*) ", "true" ),
            ( r#"map{"a": 1} instance of function(*) "#, "true" ),
            ( "fn:string-to-codepoints#1 instance of function(*)", "true" ),
            ( "function($n as xs:integer) { $n * 3 } instance of function(*) ", "true" ),
            ( "let $f := function($n as xs:integer) { $n * 3 } return $f instance of function(*) ", "true" ),
            ( r#"fn:starts-with(?, "a") instance of function(*) "#, "true" ),

            // TypedFunctionTest
            ( "7 instance of function(integer) as integer", "false" ),
            ( "[1, 2] instance of function(integer) as integer", "true" ),
            ( "[1, 2] instance of function(integer) as string", "false" ),
            ( r#"map{"a": 1} instance of function(string) as integer"#, "true" ),
            ( r#"map{"a": 1} instance of function(string) as string"#, "false" ),
            ( r#"map{"a": 1} instance of function(integer) as integer"#, "false" ),

            // TypedFunctionTest (InlineFunction)
            ( "function($n as xs:integer) as xs:integer { $n * 3 } instance of function(xs:integer) as xs:integer", "true" ),
            ( "function($n as xs:integer) as xs:integer { $n * 3 } instance of function(xs:integer) as xs:anyAtomicType", "true" ),

            // TypedFunctionTest (InlineFunction): 引数の不一致
            ( "function($n as xs:numeric) as xs:numeric { $n * 3 } instance of function(xs:anyAtomicType) as xs:integer", "false" ),

            // TypedFunctionTest (InlineFunction): 引数の個数の不一致
            ( "function($n as xs:numeric) as xs:numeric { $n * 3 } instance of function(xs:integer, xs:integer) as xs:integer", "false" ),
            ( "function($n as xs:numeric) as xs:numeric { $n * 3 } instance of function() as xs:integer", "false" ),

            // TypedFunctionTest (NamedFunctionRef):
            ( "fn:abs#1 instance of function(numeric) as numeric?", "true" ),
            ( "fn:abs#1 instance of function(integer) as numeric?", "true" ),
            ( "fn:abs#1 instance of function(numeric) as integer?", "false" ),
            // TypedFunctionTest (NamedFunctionRef): FunctionTest
            ( "fn:filter#2 instance of function(item()*, function(item()) as boolean) as item()*", "true" ),
            ( "fn:filter#2 instance of function(integer*, function(item()) as boolean) as item()*", "true" ),
            ( "fn:filter#2 instance of function(item()*, function(integer) as integer) as item()*", "false" ),

            // TypedFunctionTest (NamedFunctionRef): MapTest
            ( "map:size#1 instance of function(map(*)) as integer", "true" ),
            ( "map:size#1 instance of function(map(string, integer)) as integer", "true" ),

            // TypedFunctionTest (NamedFunctionRef): ArrayTest
            ( "array:size#1 instance of function(array(*)) as integer", "true" ),
        ]);
    }


    // -----------------------------------------------------------------
    // subtype_itemtype (map)
    //
    #[test]
    fn test_subtype_itemtype_map() {
        let xml = compress_spaces(r#"
<root>
    <elem base="base"/>
</root>
        "#);

        subtest_eval_xpath("subtype_itemtype_map", &xml, &[

            // 27. Ai is map(K, V), for any K and V and Bi is map(*).
            ( r#"function() as map(*) { "a" }
                     instance of
                 function() as map(*)"#, "true" ),
            ( r#"function() as map(string, string) { "a" }
                     instance of
                 function() as map(*)"#, "true" ),

            // 28. Ai is map(Ka, Va) and Bi is map(Kb, Vb),
            //     where subtype-itemtype(Ka, Kb) and subtype(Va, Vb).
            ( r#"function() as map(string, integer) { "a" }
                     instance of
                 function() as map(anyAtomicType, decimal)"#, "true" ),
            ( r#"function() as map(anyAtomicType, decimal) { "a" }
                     instance of
                 function() as map(string, integer)"#, "false" ),

            // 29. Ai is map(*) (or any other map type),
            //     and Bi is function(*).
            ( r#"function() as map(*) { "a" }
                     instance of
                 function() as function(*)"#, "true" ),
            ( r#"function() as map(string, integer) { "a" }
                     instance of
                 function() as function(*)"#, "true" ),

            // 30. Ai is map(*) (or any other map type),
            //     and Bi is function(xs:anyAtomicType) as item()*.
            ( r#"function() as map(*) { "a" }
                     instance of
                 function() as function(xs:anyAtomicType) as item()*"#, "true" ),
            ( r#"function() as map(string, string) { "a" }
                     instance of
                 function() as function(xs:anyAtomicType) as item()*"#, "true" ),
            ( r#"function() as map(string, string) { "a" }
                     instance of
                 function() as function(string) as string"#, "true" ),

            // 35. Ai is map(K, V), and Bi is function(xs:anyAtomicType) as V?.
            ( r#"function() as map(integer, string) { "a" }
                     instance of
                 function() as function(xs:anyAtomicType) as string?"#, "true" ),

        ]);
    }

    // -----------------------------------------------------------------
    // subtype_itemtype (array)
    //
    #[test]
    fn test_subtype_itemtype_array() {
        let xml = compress_spaces(r#"
<root>
    <elem base="base"/>
</root>
        "#);

        subtest_eval_xpath("subtype_itemtype_array", &xml, &[

            // 31. Ai is array(X) and Bi is array(*).
            ( r#"function() as array(integer) { "a" }
                     instance of
                 function() as array(*)"#, "true" ),

            // 32. Ai is array(X) and Bi is array(Y), and subtype(X, Y) is true.
            ( r#"function() as array(integer) { "a" }
                     instance of
                 function() as array(decimal)"#, "true" ),

            // 33. Ai is array(*) (or any other array type),
            //     and Bi is function(*).
            ( r#"function() as array(*) { "a" }
                     instance of
                 function() as function(*)"#, "true" ),

            // 34. Ai is array(*) (or any other array type)
            //     and Bi is function(xs:integer) as item()*.
            // 36. Ai is array(X) and Bi is function(xs:integer) as X.
            ( r#"function() as array(*) { "a" }
                     instance of
                 function() as function(integer) as item()*"#, "true" ),
            ( r#"function() as array(string) { "a" }
                     instance of
                 function() as function(integer) as string"#, "true" ),
        ]);
    }

    // -----------------------------------------------------------------
    // instance of ( ParenthesizedItemType )
    //
    #[test]
    fn test_instance_of_parenthesized_item_type() {
        let xml = compress_spaces(r#"
<root>
    <elem base="base"/>
</root>
        "#);

        subtest_eval_xpath("instance_of_parenthesized_item_type", &xml, &[
//            ( " (1) instance of (xs:integer) ", "true" ),
        ]);
    }
}