1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478
|
/*
* transform.c: Implementation of the XSL Transformation 1.0 engine
* transform part, i.e. applying a Stylesheet to a document
*
* References:
* http://www.w3.org/TR/1999/REC-xslt-19991116
*
* Michael Kay "XSLT Programmer's Reference" pp 637-643
* Writing Multiple Output Files
*
* XSLT-1.1 Working Draft
* http://www.w3.org/TR/xslt11#multiple-output
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define IN_LIBXSLT
#include "libxslt.h"
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/valid.h>
#include <libxml/hash.h>
#include <libxml/encoding.h>
#include <libxml/xmlerror.h>
#include <libxml/xpath.h>
#include <libxml/parserInternals.h>
#include <libxml/xpathInternals.h>
#include <libxml/HTMLtree.h>
#include <libxml/debugXML.h>
#include <libxml/uri.h>
#include "xslt.h"
#include "xsltInternals.h"
#include "xsltutils.h"
#include "pattern.h"
#include "transform.h"
#include "variables.h"
#include "numbersInternals.h"
#include "namespaces.h"
#include "attributes.h"
#include "templates.h"
#include "imports.h"
#include "keys.h"
#include "documents.h"
#include "extensions.h"
#include "extra.h"
#include "preproc.h"
#include "security.h"
#ifdef WITH_XSLT_DEBUG
#define WITH_XSLT_DEBUG_EXTRA
#define WITH_XSLT_DEBUG_PROCESS
#define WITH_XSLT_DEBUG_VARIABLE
#endif
#define XSLT_GENERATE_HTML_DOCTYPE
#ifdef XSLT_GENERATE_HTML_DOCTYPE
static int xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
const xmlChar **systemID);
#endif
int xsltMaxDepth = 3000;
int xsltMaxVars = 15000;
/*
* Useful macros
*/
#ifndef FALSE
# define FALSE (0 == 1)
# define TRUE (!FALSE)
#endif
#define IS_BLANK_NODE(n) \
(((n)->type == XML_TEXT_NODE) && (xsltIsBlank((n)->content)))
/*
* Forward declarations
*/
static xmlNsPtr
xsltCopyNamespaceListInternal(xmlNodePtr node, xmlNsPtr cur);
static xmlNodePtr
xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
xmlNodePtr node, xmlNodePtr insert, int isLRE,
int topElemVisited);
static void
xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode, xmlNodePtr list,
xsltTemplatePtr templ);
static void
xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode,
xmlNodePtr list,
xsltTemplatePtr templ,
xsltStackElemPtr withParams);
/**
* templPush:
* @ctxt: the transformation context
* @value: the template to push on the stack
*
* Push a template on the stack
*
* Returns the new index in the stack or 0 in case of error
*/
static int
templPush(xsltTransformContextPtr ctxt, xsltTemplatePtr value)
{
if (ctxt->templMax == 0) {
ctxt->templMax = 4;
ctxt->templTab =
(xsltTemplatePtr *) xmlMalloc(ctxt->templMax *
sizeof(ctxt->templTab[0]));
if (ctxt->templTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
return (0);
}
}
else if (ctxt->templNr >= ctxt->templMax) {
ctxt->templMax *= 2;
ctxt->templTab =
(xsltTemplatePtr *) xmlRealloc(ctxt->templTab,
ctxt->templMax *
sizeof(ctxt->templTab[0]));
if (ctxt->templTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
return (0);
}
}
ctxt->templTab[ctxt->templNr] = value;
ctxt->templ = value;
return (ctxt->templNr++);
}
/**
* templPop:
* @ctxt: the transformation context
*
* Pop a template value from the stack
*
* Returns the stored template value
*/
static xsltTemplatePtr
templPop(xsltTransformContextPtr ctxt)
{
xsltTemplatePtr ret;
if (ctxt->templNr <= 0)
return (0);
ctxt->templNr--;
if (ctxt->templNr > 0)
ctxt->templ = ctxt->templTab[ctxt->templNr - 1];
else
ctxt->templ = (xsltTemplatePtr) 0;
ret = ctxt->templTab[ctxt->templNr];
ctxt->templTab[ctxt->templNr] = 0;
return (ret);
}
/**
* xsltLocalVariablePop:
* @ctxt: the transformation context
* @limitNr: number of variables which should remain
* @level: the depth in the xsl:template's tree
*
* Pops all variable values at the given @depth from the stack.
*
* Returns the stored variable value
* **NOTE:**
* This is an internal routine and should not be called by users!
*/
void
xsltLocalVariablePop(xsltTransformContextPtr ctxt, int limitNr, int level)
{
xsltStackElemPtr variable;
if (ctxt->varsNr <= 0)
return;
do {
if (ctxt->varsNr <= limitNr)
break;
variable = ctxt->varsTab[ctxt->varsNr - 1];
if (variable->level <= level)
break;
if (variable->level >= 0)
xsltFreeStackElemList(variable);
ctxt->varsNr--;
} while (ctxt->varsNr != 0);
if (ctxt->varsNr > 0)
ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
else
ctxt->vars = NULL;
}
/**
* xsltTemplateParamsCleanup:
*
* Removes xsl:param and xsl:with-param items from the
* variable-stack. Only xsl:with-param items are not freed.
*/
static void
xsltTemplateParamsCleanup(xsltTransformContextPtr ctxt)
{
xsltStackElemPtr param;
for (; ctxt->varsNr > ctxt->varsBase; ctxt->varsNr--) {
param = ctxt->varsTab[ctxt->varsNr -1];
/*
* Free xsl:param items.
* xsl:with-param items will have a level of -1 or -2.
*/
if (param->level >= 0) {
xsltFreeStackElemList(param);
}
}
if (ctxt->varsNr > 0)
ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
else
ctxt->vars = NULL;
}
/**
* profPush:
* @ctxt: the transformation context
* @value: the profiling value to push on the stack
*
* Push a profiling value on the stack
*
* Returns the new index in the stack or 0 in case of error
*/
static int
profPush(xsltTransformContextPtr ctxt, long value)
{
if (ctxt->profMax == 0) {
ctxt->profMax = 4;
ctxt->profTab =
(long *) xmlMalloc(ctxt->profMax * sizeof(ctxt->profTab[0]));
if (ctxt->profTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
return (0);
}
}
else if (ctxt->profNr >= ctxt->profMax) {
ctxt->profMax *= 2;
ctxt->profTab =
(long *) xmlRealloc(ctxt->profTab,
ctxt->profMax * sizeof(ctxt->profTab[0]));
if (ctxt->profTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
return (0);
}
}
ctxt->profTab[ctxt->profNr] = value;
ctxt->prof = value;
return (ctxt->profNr++);
}
/**
* profPop:
* @ctxt: the transformation context
*
* Pop a profiling value from the stack
*
* Returns the stored profiling value
*/
static long
profPop(xsltTransformContextPtr ctxt)
{
long ret;
if (ctxt->profNr <= 0)
return (0);
ctxt->profNr--;
if (ctxt->profNr > 0)
ctxt->prof = ctxt->profTab[ctxt->profNr - 1];
else
ctxt->prof = (long) 0;
ret = ctxt->profTab[ctxt->profNr];
ctxt->profTab[ctxt->profNr] = 0;
return (ret);
}
static void
profCallgraphAdd(xsltTemplatePtr templ, xsltTemplatePtr parent)
{
int i;
if (templ->templMax == 0) {
templ->templMax = 4;
templ->templCalledTab =
(xsltTemplatePtr *) xmlMalloc(templ->templMax *
sizeof(templ->templCalledTab[0]));
templ->templCountTab =
(int *) xmlMalloc(templ->templMax *
sizeof(templ->templCountTab[0]));
if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
return;
}
}
else if (templ->templNr >= templ->templMax) {
templ->templMax *= 2;
templ->templCalledTab =
(xsltTemplatePtr *) xmlRealloc(templ->templCalledTab,
templ->templMax *
sizeof(templ->templCalledTab[0]));
templ->templCountTab =
(int *) xmlRealloc(templ->templCountTab,
templ->templMax *
sizeof(templ->templCountTab[0]));
if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
return;
}
}
for (i = 0; i < templ->templNr; i++) {
if (templ->templCalledTab[i] == parent) {
templ->templCountTab[i]++;
break;
}
}
if (i == templ->templNr) {
/* not found, add new one */
templ->templCalledTab[templ->templNr] = parent;
templ->templCountTab[templ->templNr] = 1;
templ->templNr++;
}
}
/**
* xsltPreCompEval:
* @ctxt: transform context
* @node: context node
* @comp: precompiled expression
*
* Evaluate a precompiled XPath expression.
*/
static xmlXPathObjectPtr
xsltPreCompEval(xsltTransformContextPtr ctxt, xmlNodePtr node,
xsltStylePreCompPtr comp) {
xmlXPathObjectPtr res;
xmlXPathContextPtr xpctxt;
xmlNodePtr oldXPContextNode;
xmlNsPtr *oldXPNamespaces;
int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
xpctxt = ctxt->xpathCtxt;
oldXPContextNode = xpctxt->node;
oldXPProximityPosition = xpctxt->proximityPosition;
oldXPContextSize = xpctxt->contextSize;
oldXPNsNr = xpctxt->nsNr;
oldXPNamespaces = xpctxt->namespaces;
xpctxt->node = node;
#ifdef XSLT_REFACTORED
if (comp->inScopeNs != NULL) {
xpctxt->namespaces = comp->inScopeNs->list;
xpctxt->nsNr = comp->inScopeNs->xpathNumber;
} else {
xpctxt->namespaces = NULL;
xpctxt->nsNr = 0;
}
#else
xpctxt->namespaces = comp->nsList;
xpctxt->nsNr = comp->nsNr;
#endif
res = xmlXPathCompiledEval(comp->comp, xpctxt);
xpctxt->node = oldXPContextNode;
xpctxt->proximityPosition = oldXPProximityPosition;
xpctxt->contextSize = oldXPContextSize;
xpctxt->nsNr = oldXPNsNr;
xpctxt->namespaces = oldXPNamespaces;
return(res);
}
/**
* xsltPreCompEvalToBoolean:
* @ctxt: transform context
* @node: context node
* @comp: precompiled expression
*
* Evaluate a precompiled XPath expression as boolean.
*/
static int
xsltPreCompEvalToBoolean(xsltTransformContextPtr ctxt, xmlNodePtr node,
xsltStylePreCompPtr comp) {
int res;
xmlXPathContextPtr xpctxt;
xmlNodePtr oldXPContextNode;
xmlNsPtr *oldXPNamespaces;
int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
xpctxt = ctxt->xpathCtxt;
oldXPContextNode = xpctxt->node;
oldXPProximityPosition = xpctxt->proximityPosition;
oldXPContextSize = xpctxt->contextSize;
oldXPNsNr = xpctxt->nsNr;
oldXPNamespaces = xpctxt->namespaces;
xpctxt->node = node;
#ifdef XSLT_REFACTORED
if (comp->inScopeNs != NULL) {
xpctxt->namespaces = comp->inScopeNs->list;
xpctxt->nsNr = comp->inScopeNs->xpathNumber;
} else {
xpctxt->namespaces = NULL;
xpctxt->nsNr = 0;
}
#else
xpctxt->namespaces = comp->nsList;
xpctxt->nsNr = comp->nsNr;
#endif
res = xmlXPathCompiledEvalToBoolean(comp->comp, xpctxt);
xpctxt->node = oldXPContextNode;
xpctxt->proximityPosition = oldXPProximityPosition;
xpctxt->contextSize = oldXPContextSize;
xpctxt->nsNr = oldXPNsNr;
xpctxt->namespaces = oldXPNamespaces;
return(res);
}
/************************************************************************
* *
* XInclude default settings *
* *
************************************************************************/
static int xsltDoXIncludeDefault = 0;
/**
* xsltSetXIncludeDefault:
* @xinclude: whether to do XInclude processing
*
* Set whether XInclude should be processed on document being loaded by default
*/
void
xsltSetXIncludeDefault(int xinclude) {
xsltDoXIncludeDefault = (xinclude != 0);
}
/**
* xsltGetXIncludeDefault:
*
* Provides the default state for XInclude processing
*
* Returns 0 if there is no processing 1 otherwise
*/
int
xsltGetXIncludeDefault(void) {
return(xsltDoXIncludeDefault);
}
static unsigned long xsltDefaultTrace = (unsigned long) XSLT_TRACE_ALL;
/**
* xsltDebugSetDefaultTrace:
* @val: tracing level mask
*
* Set the default debug tracing level mask
*/
void xsltDebugSetDefaultTrace(xsltDebugTraceCodes val) {
xsltDefaultTrace = val;
}
/**
* xsltDebugGetDefaultTrace:
*
* Get the current default debug tracing level mask
*
* Returns the current default debug tracing level mask
*/
xsltDebugTraceCodes xsltDebugGetDefaultTrace() {
return xsltDefaultTrace;
}
/************************************************************************
* *
* Handling of Transformation Contexts *
* *
************************************************************************/
static xsltTransformCachePtr
xsltTransformCacheCreate(void)
{
xsltTransformCachePtr ret;
ret = (xsltTransformCachePtr) xmlMalloc(sizeof(xsltTransformCache));
if (ret == NULL) {
xsltTransformError(NULL, NULL, NULL,
"xsltTransformCacheCreate : malloc failed\n");
return(NULL);
}
memset(ret, 0, sizeof(xsltTransformCache));
return(ret);
}
static void
xsltTransformCacheFree(xsltTransformCachePtr cache)
{
if (cache == NULL)
return;
/*
* Free tree fragments.
*/
if (cache->RVT) {
xmlDocPtr tmp, cur = cache->RVT;
while (cur) {
tmp = cur;
cur = (xmlDocPtr) cur->next;
if (tmp->_private != NULL) {
/*
* Tree the document info.
*/
xsltFreeDocumentKeys((xsltDocumentPtr) tmp->_private);
xmlFree(tmp->_private);
}
xmlFreeDoc(tmp);
}
}
/*
* Free vars/params.
*/
if (cache->stackItems) {
xsltStackElemPtr tmp, cur = cache->stackItems;
while (cur) {
tmp = cur;
cur = cur->next;
/*
* REVISIT TODO: Should be call a destruction-function
* instead?
*/
xmlFree(tmp);
}
}
xmlFree(cache);
}
/**
* xsltNewTransformContext:
* @style: a parsed XSLT stylesheet
* @doc: the input document
*
* Create a new XSLT TransformContext
*
* Returns the newly allocated xsltTransformContextPtr or NULL in case of error
*/
xsltTransformContextPtr
xsltNewTransformContext(xsltStylesheetPtr style, xmlDocPtr doc) {
xsltTransformContextPtr cur;
xsltDocumentPtr docu;
int i;
xsltInitGlobals();
cur = (xsltTransformContextPtr) xmlMalloc(sizeof(xsltTransformContext));
if (cur == NULL) {
xsltTransformError(NULL, NULL, (xmlNodePtr)doc,
"xsltNewTransformContext : malloc failed\n");
return(NULL);
}
memset(cur, 0, sizeof(xsltTransformContext));
cur->cache = xsltTransformCacheCreate();
if (cur->cache == NULL)
goto internal_err;
/*
* setup of the dictionary must be done early as some of the
* processing later like key handling may need it.
*/
cur->dict = xmlDictCreateSub(style->dict);
cur->internalized = ((style->internalized) && (cur->dict != NULL));
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"Creating sub-dictionary from stylesheet for transformation\n");
#endif
/*
* initialize the template stack
*/
cur->templTab = (xsltTemplatePtr *)
xmlMalloc(10 * sizeof(xsltTemplatePtr));
if (cur->templTab == NULL) {
xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
"xsltNewTransformContext: out of memory\n");
goto internal_err;
}
cur->templNr = 0;
cur->templMax = 5;
cur->templ = NULL;
cur->maxTemplateDepth = xsltMaxDepth;
/*
* initialize the variables stack
*/
cur->varsTab = (xsltStackElemPtr *)
xmlMalloc(10 * sizeof(xsltStackElemPtr));
if (cur->varsTab == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xsltNewTransformContext: out of memory\n");
goto internal_err;
}
cur->varsNr = 0;
cur->varsMax = 10;
cur->vars = NULL;
cur->varsBase = 0;
cur->maxTemplateVars = xsltMaxVars;
/*
* the profiling stack is not initialized by default
*/
cur->profTab = NULL;
cur->profNr = 0;
cur->profMax = 0;
cur->prof = 0;
cur->style = style;
xmlXPathInit();
cur->xpathCtxt = xmlXPathNewContext(doc);
if (cur->xpathCtxt == NULL) {
xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
"xsltNewTransformContext : xmlXPathNewContext failed\n");
goto internal_err;
}
/*
* Create an XPath cache.
*/
if (xmlXPathContextSetCache(cur->xpathCtxt, 1, -1, 0) == -1)
goto internal_err;
/*
* Initialize the extras array
*/
if (style->extrasNr != 0) {
cur->extrasMax = style->extrasNr + 20;
cur->extras = (xsltRuntimeExtraPtr)
xmlMalloc(cur->extrasMax * sizeof(xsltRuntimeExtra));
if (cur->extras == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xsltNewTransformContext: out of memory\n");
goto internal_err;
}
cur->extrasNr = style->extrasNr;
for (i = 0;i < cur->extrasMax;i++) {
cur->extras[i].info = NULL;
cur->extras[i].deallocate = NULL;
cur->extras[i].val.ptr = NULL;
}
} else {
cur->extras = NULL;
cur->extrasNr = 0;
cur->extrasMax = 0;
}
XSLT_REGISTER_VARIABLE_LOOKUP(cur);
XSLT_REGISTER_FUNCTION_LOOKUP(cur);
cur->xpathCtxt->nsHash = style->nsHash;
/*
* Initialize the registered external modules
*/
xsltInitCtxtExts(cur);
/*
* Setup document element ordering for later efficiencies
* (bug 133289)
*/
if (xslDebugStatus == XSLT_DEBUG_NONE)
xmlXPathOrderDocElems(doc);
/*
* Must set parserOptions before calling xsltNewDocument
* (bug 164530)
*/
cur->parserOptions = XSLT_PARSE_OPTIONS;
docu = xsltNewDocument(cur, doc);
if (docu == NULL) {
xsltTransformError(cur, NULL, (xmlNodePtr)doc,
"xsltNewTransformContext : xsltNewDocument failed\n");
goto internal_err;
}
docu->main = 1;
cur->document = docu;
cur->inst = NULL;
cur->outputFile = NULL;
cur->sec = xsltGetDefaultSecurityPrefs();
cur->debugStatus = xslDebugStatus;
cur->traceCode = (unsigned long*) &xsltDefaultTrace;
cur->xinclude = xsltGetXIncludeDefault();
cur->keyInitLevel = 0;
cur->nextid = 0;
return(cur);
internal_err:
if (cur != NULL)
xsltFreeTransformContext(cur);
return(NULL);
}
/**
* xsltFreeTransformContext:
* @ctxt: an XSLT parser context
*
* Free up the memory allocated by @ctxt
*/
void
xsltFreeTransformContext(xsltTransformContextPtr ctxt) {
if (ctxt == NULL)
return;
/*
* Shutdown the extension modules associated to the stylesheet
* used if needed.
*/
xsltShutdownCtxtExts(ctxt);
if (ctxt->xpathCtxt != NULL) {
ctxt->xpathCtxt->nsHash = NULL;
xmlXPathFreeContext(ctxt->xpathCtxt);
}
if (ctxt->templTab != NULL)
xmlFree(ctxt->templTab);
if (ctxt->varsTab != NULL)
xmlFree(ctxt->varsTab);
if (ctxt->profTab != NULL)
xmlFree(ctxt->profTab);
if ((ctxt->extrasNr > 0) && (ctxt->extras != NULL)) {
int i;
for (i = 0;i < ctxt->extrasNr;i++) {
if ((ctxt->extras[i].deallocate != NULL) &&
(ctxt->extras[i].info != NULL))
ctxt->extras[i].deallocate(ctxt->extras[i].info);
}
xmlFree(ctxt->extras);
}
xsltFreeGlobalVariables(ctxt);
xsltFreeDocuments(ctxt);
xsltFreeCtxtExts(ctxt);
xsltFreeRVTs(ctxt);
xsltTransformCacheFree(ctxt->cache);
xmlDictFree(ctxt->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"freeing transformation dictionary\n");
#endif
memset(ctxt, -1, sizeof(xsltTransformContext));
xmlFree(ctxt);
}
/************************************************************************
* *
* Copy of Nodes in an XSLT fashion *
* *
************************************************************************/
/**
* xsltAddChild:
* @parent: the parent node
* @cur: the child node
*
* Wrapper version of xmlAddChild with a more consistent behaviour on
* error. One expect the use to be child = xsltAddChild(parent, child);
* and the routine will take care of not leaking on errors or node merge
*
* Returns the child is successfully attached or NULL if merged or freed
*/
static xmlNodePtr
xsltAddChild(xmlNodePtr parent, xmlNodePtr cur) {
xmlNodePtr ret;
if (cur == NULL)
return(NULL);
if (parent == NULL) {
xmlFreeNode(cur);
return(NULL);
}
ret = xmlAddChild(parent, cur);
return(ret);
}
/**
* xsltAddTextString:
* @ctxt: a XSLT process context
* @target: the text node where the text will be attached
* @string: the text string
* @len: the string length in byte
*
* Extend the current text node with the new string, it handles coalescing
*
* Returns: the text node
*/
static xmlNodePtr
xsltAddTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
const xmlChar *string, int len) {
/*
* optimization
*/
if ((len <= 0) || (string == NULL) || (target == NULL))
return(target);
if (ctxt->lasttext == target->content) {
int minSize;
/* Check for integer overflow accounting for NUL terminator. */
if (len >= INT_MAX - ctxt->lasttuse) {
xsltTransformError(ctxt, NULL, target,
"xsltCopyText: text allocation failed\n");
return(NULL);
}
minSize = ctxt->lasttuse + len + 1;
if (ctxt->lasttsize < minSize) {
xmlChar *newbuf;
int size;
int extra;
/* Double buffer size but increase by at least 100 bytes. */
extra = minSize < 100 ? 100 : minSize;
/* Check for integer overflow. */
if (extra > INT_MAX - ctxt->lasttsize) {
size = INT_MAX;
}
else {
size = ctxt->lasttsize + extra;
}
newbuf = (xmlChar *) xmlRealloc(target->content,size);
if (newbuf == NULL) {
xsltTransformError(ctxt, NULL, target,
"xsltCopyText: text allocation failed\n");
return(NULL);
}
ctxt->lasttsize = size;
ctxt->lasttext = newbuf;
target->content = newbuf;
}
memcpy(&(target->content[ctxt->lasttuse]), string, len);
ctxt->lasttuse += len;
target->content[ctxt->lasttuse] = 0;
} else {
xmlNodeAddContent(target, string);
ctxt->lasttext = target->content;
len = xmlStrlen(target->content);
ctxt->lasttsize = len;
ctxt->lasttuse = len;
}
return(target);
}
/**
* xsltCopyTextString:
* @ctxt: a XSLT process context
* @target: the element where the text will be attached
* @string: the text string
* @noescape: should disable-escaping be activated for this text node.
*
* Adds @string to a newly created or an existent text node child of
* @target.
*
* Returns: the text node, where the text content of @cur is copied to.
* NULL in case of API or internal errors.
*/
xmlNodePtr
xsltCopyTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
const xmlChar *string, int noescape)
{
xmlNodePtr copy;
int len;
if (string == NULL)
return(NULL);
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyTextString: copy text %s\n",
string));
#endif
/*
* Play safe and reset the merging mechanism for every new
* target node.
*/
if ((target == NULL) || (target->children == NULL)) {
ctxt->lasttext = NULL;
}
/* handle coalescing of text nodes here */
len = xmlStrlen(string);
if ((ctxt->type == XSLT_OUTPUT_XML) &&
(ctxt->style->cdataSection != NULL) &&
(target != NULL) &&
(target->type == XML_ELEMENT_NODE) &&
(((target->ns == NULL) &&
(xmlHashLookup2(ctxt->style->cdataSection,
target->name, NULL) != NULL)) ||
((target->ns != NULL) &&
(xmlHashLookup2(ctxt->style->cdataSection,
target->name, target->ns->href) != NULL))))
{
/*
* Process "cdata-section-elements".
*/
if ((target->last != NULL) &&
(target->last->type == XML_CDATA_SECTION_NODE))
{
return(xsltAddTextString(ctxt, target->last, string, len));
}
copy = xmlNewCDataBlock(ctxt->output, string, len);
} else if (noescape) {
/*
* Process "disable-output-escaping".
*/
if ((target != NULL) && (target->last != NULL) &&
(target->last->type == XML_TEXT_NODE) &&
(target->last->name == xmlStringTextNoenc))
{
return(xsltAddTextString(ctxt, target->last, string, len));
}
copy = xmlNewTextLen(string, len);
if (copy != NULL)
copy->name = xmlStringTextNoenc;
} else {
/*
* Default processing.
*/
if ((target != NULL) && (target->last != NULL) &&
(target->last->type == XML_TEXT_NODE) &&
(target->last->name == xmlStringText)) {
return(xsltAddTextString(ctxt, target->last, string, len));
}
copy = xmlNewTextLen(string, len);
}
if (copy != NULL && target != NULL)
copy = xsltAddChild(target, copy);
if (copy != NULL) {
ctxt->lasttext = copy->content;
ctxt->lasttsize = len;
ctxt->lasttuse = len;
} else {
xsltTransformError(ctxt, NULL, target,
"xsltCopyTextString: text copy failed\n");
ctxt->lasttext = NULL;
}
return(copy);
}
/**
* xsltCopyText:
* @ctxt: a XSLT process context
* @target: the element where the text will be attached
* @cur: the text or CDATA node
* @interned: the string is in the target doc dictionary
*
* Copy the text content of @cur and append it to @target's children.
*
* Returns: the text node, where the text content of @cur is copied to.
* NULL in case of API or internal errors.
*/
static xmlNodePtr
xsltCopyText(xsltTransformContextPtr ctxt, xmlNodePtr target,
xmlNodePtr cur, int interned)
{
xmlNodePtr copy;
if ((cur->type != XML_TEXT_NODE) &&
(cur->type != XML_CDATA_SECTION_NODE))
return(NULL);
if (cur->content == NULL)
return(NULL);
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->type == XML_CDATA_SECTION_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyText: copy CDATA text %s\n",
cur->content));
} else if (cur->name == xmlStringTextNoenc) {
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyText: copy unescaped text %s\n",
cur->content));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyText: copy text %s\n",
cur->content));
}
#endif
/*
* Play save and reset the merging mechanism for every new
* target node.
*/
if ((target == NULL) || (target->children == NULL)) {
ctxt->lasttext = NULL;
}
if ((ctxt->style->cdataSection != NULL) &&
(ctxt->type == XSLT_OUTPUT_XML) &&
(target != NULL) &&
(target->type == XML_ELEMENT_NODE) &&
(((target->ns == NULL) &&
(xmlHashLookup2(ctxt->style->cdataSection,
target->name, NULL) != NULL)) ||
((target->ns != NULL) &&
(xmlHashLookup2(ctxt->style->cdataSection,
target->name, target->ns->href) != NULL))))
{
/*
* Process "cdata-section-elements".
*/
/*
* OPTIMIZE TODO: xsltCopyText() is also used for attribute content.
*/
/*
* TODO: Since this doesn't merge adjacent CDATA-section nodes,
* we'll get: <![CDATA[x]]><!CDATA[y]]>.
* TODO: Reported in #321505.
*/
if ((target->last != NULL) &&
(target->last->type == XML_CDATA_SECTION_NODE))
{
/*
* Append to existing CDATA-section node.
*/
copy = xsltAddTextString(ctxt, target->last, cur->content,
xmlStrlen(cur->content));
goto exit;
} else {
unsigned int len;
len = xmlStrlen(cur->content);
copy = xmlNewCDataBlock(ctxt->output, cur->content, len);
if (copy == NULL)
goto exit;
ctxt->lasttext = copy->content;
ctxt->lasttsize = len;
ctxt->lasttuse = len;
}
} else if ((target != NULL) &&
(target->last != NULL) &&
/* both escaped or both non-escaped text-nodes */
(((target->last->type == XML_TEXT_NODE) &&
(target->last->name == cur->name)) ||
/* non-escaped text nodes and CDATA-section nodes */
(((target->last->type == XML_CDATA_SECTION_NODE) &&
(cur->name == xmlStringTextNoenc)))))
{
/*
* we are appending to an existing text node
*/
copy = xsltAddTextString(ctxt, target->last, cur->content,
xmlStrlen(cur->content));
goto exit;
} else if ((interned) && (target != NULL) &&
(target->doc != NULL) &&
(target->doc->dict == ctxt->dict))
{
/*
* TODO: DO we want to use this also for "text" output?
*/
copy = xmlNewTextLen(NULL, 0);
if (copy == NULL)
goto exit;
if (cur->name == xmlStringTextNoenc)
copy->name = xmlStringTextNoenc;
/*
* Must confirm that content is in dict (bug 302821)
* TODO: This check should be not needed for text coming
* from the stylesheets
*/
if (xmlDictOwns(ctxt->dict, cur->content))
copy->content = cur->content;
else {
if ((copy->content = xmlStrdup(cur->content)) == NULL)
return NULL;
}
ctxt->lasttext = NULL;
} else {
/*
* normal processing. keep counters to extend the text node
* in xsltAddTextString if needed.
*/
unsigned int len;
len = xmlStrlen(cur->content);
copy = xmlNewTextLen(cur->content, len);
if (copy == NULL)
goto exit;
if (cur->name == xmlStringTextNoenc)
copy->name = xmlStringTextNoenc;
ctxt->lasttext = copy->content;
ctxt->lasttsize = len;
ctxt->lasttuse = len;
}
if (copy != NULL) {
if (target != NULL) {
copy->doc = target->doc;
/*
* MAYBE TODO: Maybe we should reset the ctxt->lasttext here
* to ensure that the optimized text-merging mechanism
* won't interfere with normal node-merging in any case.
*/
copy = xsltAddChild(target, copy);
}
} else {
xsltTransformError(ctxt, NULL, target,
"xsltCopyText: text copy failed\n");
}
exit:
if ((copy == NULL) || (copy->content == NULL)) {
xsltTransformError(ctxt, NULL, target,
"Internal error in xsltCopyText(): "
"Failed to copy the string.\n");
ctxt->state = XSLT_STATE_STOPPED;
}
return(copy);
}
/**
* xsltShallowCopyAttr:
* @ctxt: a XSLT process context
* @invocNode: responsible node in the stylesheet; used for error reports
* @target: the element where the attribute will be grafted
* @attr: the attribute to be copied
*
* Do a copy of an attribute.
* Called by:
* - xsltCopyTree()
* - xsltCopyOf()
* - xsltCopy()
*
* Returns: a new xmlAttrPtr, or NULL in case of error.
*/
static xmlAttrPtr
xsltShallowCopyAttr(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
xmlNodePtr target, xmlAttrPtr attr)
{
xmlAttrPtr copy;
xmlChar *value;
if (attr == NULL)
return(NULL);
if (target->type != XML_ELEMENT_NODE) {
xsltTransformError(ctxt, NULL, invocNode,
"Cannot add an attribute node to a non-element node.\n");
return(NULL);
}
if (target->children != NULL) {
xsltTransformError(ctxt, NULL, invocNode,
"Attribute nodes must be added before "
"any child nodes to an element.\n");
return(NULL);
}
value = xmlNodeListGetString(attr->doc, attr->children, 1);
if (attr->ns != NULL) {
xmlNsPtr ns;
ns = xsltGetSpecialNamespace(ctxt, invocNode,
attr->ns->href, attr->ns->prefix, target);
if (ns == NULL) {
xsltTransformError(ctxt, NULL, invocNode,
"Namespace fixup error: Failed to acquire an in-scope "
"namespace binding of the copied attribute '{%s}%s'.\n",
attr->ns->href, attr->name);
/*
* TODO: Should we just stop here?
*/
}
/*
* Note that xmlSetNsProp() will take care of duplicates
* and assigns the new namespace even to a duplicate.
*/
copy = xmlSetNsProp(target, ns, attr->name, value);
} else {
copy = xmlSetNsProp(target, NULL, attr->name, value);
}
if (value != NULL)
xmlFree(value);
if (copy == NULL)
return(NULL);
#if 0
/*
* NOTE: This was optimized according to bug #342695.
* TODO: Can this further be optimized, if source and target
* share the same dict and attr->children is just 1 text node
* which is in the dict? How probable is such a case?
*/
/*
* TODO: Do we need to create an empty text node if the value
* is the empty string?
*/
value = xmlNodeListGetString(attr->doc, attr->children, 1);
if (value != NULL) {
txtNode = xmlNewDocText(target->doc, NULL);
if (txtNode == NULL)
return(NULL);
if ((target->doc != NULL) &&
(target->doc->dict != NULL))
{
txtNode->content =
(xmlChar *) xmlDictLookup(target->doc->dict,
BAD_CAST value, -1);
xmlFree(value);
} else
txtNode->content = value;
copy->children = txtNode;
}
#endif
return(copy);
}
/**
* xsltCopyAttrListNoOverwrite:
* @ctxt: a XSLT process context
* @invocNode: responsible node in the stylesheet; used for error reports
* @target: the element where the new attributes will be grafted
* @attr: the first attribute in the list to be copied
*
* Copies a list of attribute nodes, starting with @attr, over to the
* @target element node.
*
* Called by:
* - xsltCopyTree()
*
* Returns 0 on success and -1 on errors and internal errors.
*/
static int
xsltCopyAttrListNoOverwrite(xsltTransformContextPtr ctxt,
xmlNodePtr invocNode,
xmlNodePtr target, xmlAttrPtr attr)
{
xmlAttrPtr copy;
xmlNsPtr origNs = NULL, copyNs = NULL;
xmlChar *value;
/*
* Don't use xmlCopyProp() here, since it will try to
* reconciliate namespaces.
*/
while (attr != NULL) {
/*
* Find a namespace node in the tree of @target.
* Avoid searching for the same ns.
*/
if (attr->ns != origNs) {
origNs = attr->ns;
if (attr->ns != NULL) {
copyNs = xsltGetSpecialNamespace(ctxt, invocNode,
attr->ns->href, attr->ns->prefix, target);
if (copyNs == NULL)
return(-1);
} else
copyNs = NULL;
}
/*
* If attribute has a value, we need to copy it (watching out
* for possible entities)
*/
if ((attr->children) && (attr->children->type == XML_TEXT_NODE) &&
(attr->children->next == NULL)) {
copy = xmlNewNsProp(target, copyNs, attr->name,
attr->children->content);
} else if (attr->children != NULL) {
value = xmlNodeListGetString(attr->doc, attr->children, 1);
copy = xmlNewNsProp(target, copyNs, attr->name, BAD_CAST value);
xmlFree(value);
} else {
copy = xmlNewNsProp(target, copyNs, attr->name, NULL);
}
if (copy == NULL)
return(-1);
attr = attr->next;
}
return(0);
}
/**
* xsltShallowCopyElem:
* @ctxt: the XSLT process context
* @node: the element node in the source tree
* or the Literal Result Element
* @insert: the parent in the result tree
* @isLRE: if @node is a Literal Result Element
*
* Make a copy of the element node @node
* and insert it as last child of @insert.
*
* URGENT TODO: The problem with this one (for the non-refactored code)
* is that it is used for both, Literal Result Elements *and*
* copying input nodes.
*
* BIG NOTE: This is only called for XML_ELEMENT_NODEs.
*
* Called from:
* xsltApplySequenceConstructor()
* (for Literal Result Elements - which is a problem)
* xsltCopy() (for shallow-copying elements via xsl:copy)
*
* Returns a pointer to the new node, or NULL in case of error
*/
static xmlNodePtr
xsltShallowCopyElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr insert, int isLRE)
{
xmlNodePtr copy;
if ((node->type == XML_DTD_NODE) || (insert == NULL))
return(NULL);
if ((node->type == XML_TEXT_NODE) ||
(node->type == XML_CDATA_SECTION_NODE))
return(xsltCopyText(ctxt, insert, node, 0));
copy = xmlDocCopyNode(node, insert->doc, 0);
if (copy != NULL) {
copy->doc = ctxt->output;
copy = xsltAddChild(insert, copy);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, node,
"xsltShallowCopyElem: copy failed\n");
return (copy);
}
if (node->type == XML_ELEMENT_NODE) {
/*
* Add namespaces as they are needed
*/
if (node->nsDef != NULL) {
/*
* TODO: Remove the LRE case in the refactored code
* gets enabled.
*/
if (isLRE)
xsltCopyNamespaceList(ctxt, copy, node->nsDef);
else
xsltCopyNamespaceListInternal(copy, node->nsDef);
}
/*
* URGENT TODO: The problem with this is that it does not
* copy over all namespace nodes in scope.
* The damn thing about this is, that we would need to
* use the xmlGetNsList(), for every single node; this is
* also done in xsltCopyTree(), but only for the top node.
*/
if (node->ns != NULL) {
if (isLRE) {
/*
* REVISIT TODO: Since the non-refactored code still does
* ns-aliasing, we need to call xsltGetNamespace() here.
* Remove this when ready.
*/
copy->ns = xsltGetNamespace(ctxt, node, node->ns, copy);
} else {
copy->ns = xsltGetSpecialNamespace(ctxt,
node, node->ns->href, node->ns->prefix, copy);
}
} else if ((insert->type == XML_ELEMENT_NODE) &&
(insert->ns != NULL))
{
/*
* "Undeclare" the default namespace.
*/
xsltGetSpecialNamespace(ctxt, node, NULL, NULL, copy);
}
}
} else {
xsltTransformError(ctxt, NULL, node,
"xsltShallowCopyElem: copy %s failed\n", node->name);
}
return(copy);
}
/**
* xsltCopyTreeList:
* @ctxt: a XSLT process context
* @invocNode: responsible node in the stylesheet; used for error reports
* @list: the list of element nodes in the source tree.
* @insert: the parent in the result tree.
* @isLRE: is this a literal result element list
* @topElemVisited: indicates if a top-most element was already processed
*
* Make a copy of the full list of tree @list
* and insert it as last children of @insert
*
* NOTE: Not to be used for Literal Result Elements.
*
* Used by:
* - xsltCopyOf()
*
* Returns a pointer to the new list, or NULL in case of error
*/
static xmlNodePtr
xsltCopyTreeList(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
xmlNodePtr list,
xmlNodePtr insert, int isLRE, int topElemVisited)
{
xmlNodePtr copy, ret = NULL;
while (list != NULL) {
copy = xsltCopyTree(ctxt, invocNode,
list, insert, isLRE, topElemVisited);
if (copy != NULL) {
if (ret == NULL) {
ret = copy;
}
}
list = list->next;
}
return(ret);
}
/**
* xsltCopyNamespaceListInternal:
* @node: the target node
* @cur: the first namespace
*
* Do a copy of a namespace list. If @node is non-NULL the
* new namespaces are added automatically.
* Called by:
* xsltCopyTree()
*
* QUESTION: What is the exact difference between this function
* and xsltCopyNamespaceList() in "namespaces.c"?
* ANSWER: xsltCopyNamespaceList() tries to apply ns-aliases.
*
* Returns: a new xmlNsPtr, or NULL in case of error.
*/
static xmlNsPtr
xsltCopyNamespaceListInternal(xmlNodePtr elem, xmlNsPtr ns) {
xmlNsPtr ret = NULL;
xmlNsPtr p = NULL, q, luNs;
if (ns == NULL)
return(NULL);
/*
* One can add namespaces only on element nodes
*/
if ((elem != NULL) && (elem->type != XML_ELEMENT_NODE))
elem = NULL;
do {
if (ns->type != XML_NAMESPACE_DECL)
break;
/*
* Avoid duplicating namespace declarations on the tree.
*/
if (elem != NULL) {
if ((elem->ns != NULL) &&
xmlStrEqual(elem->ns->prefix, ns->prefix) &&
xmlStrEqual(elem->ns->href, ns->href))
{
ns = ns->next;
continue;
}
luNs = xmlSearchNs(elem->doc, elem, ns->prefix);
if ((luNs != NULL) && (xmlStrEqual(luNs->href, ns->href)))
{
ns = ns->next;
continue;
}
}
q = xmlNewNs(elem, ns->href, ns->prefix);
if (p == NULL) {
ret = p = q;
} else if (q != NULL) {
p->next = q;
p = q;
}
ns = ns->next;
} while (ns != NULL);
return(ret);
}
/**
* xsltShallowCopyNsNode:
* @ctxt: the XSLT transformation context
* @invocNode: responsible node in the stylesheet; used for error reports
* @insert: the target element node in the result tree
* @ns: the namespace node
*
* This is used for copying ns-nodes with xsl:copy-of and xsl:copy.
*
* Returns a new/existing ns-node, or NULL.
*/
static xmlNsPtr
xsltShallowCopyNsNode(xsltTransformContextPtr ctxt,
xmlNodePtr invocNode,
xmlNodePtr insert,
xmlNsPtr ns)
{
/*
* TODO: Contrary to header comments, this is declared as int.
* be modified to return a node pointer, or NULL if any error
*/
xmlNsPtr tmpns;
if ((insert == NULL) || (insert->type != XML_ELEMENT_NODE))
return(NULL);
if (insert->children != NULL) {
xsltTransformError(ctxt, NULL, invocNode,
"Namespace nodes must be added before "
"any child nodes are added to an element.\n");
return(NULL);
}
/*
* BIG NOTE: Xalan-J simply overwrites any ns-decls with
* an equal prefix. We definitively won't do that.
*
* MSXML 4.0 and the .NET ignores ns-decls for which an
* equal prefix is already in use.
*
* Saxon raises an error like:
* "net.sf.saxon.xpath.DynamicError: Cannot create two namespace
* nodes with the same name".
*
* NOTE: We'll currently follow MSXML here.
* REVISIT TODO: Check if it's better to follow Saxon here.
*/
if (ns->prefix == NULL) {
/*
* If we are adding ns-nodes to an element using e.g.
* <xsl:copy-of select="/foo/namespace::*">, then we need
* to ensure that we don't incorrectly declare a default
* namespace on an element in no namespace, which otherwise
* would move the element incorrectly into a namespace, if
* the node tree is serialized.
*/
if (insert->ns == NULL)
goto occupied;
} else if ((ns->prefix[0] == 'x') &&
xmlStrEqual(ns->prefix, BAD_CAST "xml"))
{
/*
* The XML namespace is built in.
*/
return(NULL);
}
if (insert->nsDef != NULL) {
tmpns = insert->nsDef;
do {
if ((tmpns->prefix == NULL) == (ns->prefix == NULL)) {
if ((tmpns->prefix == ns->prefix) ||
xmlStrEqual(tmpns->prefix, ns->prefix))
{
/*
* Same prefix.
*/
if (xmlStrEqual(tmpns->href, ns->href))
return(NULL);
goto occupied;
}
}
tmpns = tmpns->next;
} while (tmpns != NULL);
}
tmpns = xmlSearchNs(insert->doc, insert, ns->prefix);
if ((tmpns != NULL) && xmlStrEqual(tmpns->href, ns->href))
return(NULL);
/*
* Declare a new namespace.
* TODO: The problem (wrt efficiency) with this xmlNewNs() is
* that it will again search the already declared namespaces
* for a duplicate :-/
*/
return(xmlNewNs(insert, ns->href, ns->prefix));
occupied:
/*
* TODO: We could as well raise an error here (like Saxon does),
* or at least generate a warning.
*/
return(NULL);
}
/**
* xsltCopyTree:
* @ctxt: the XSLT transformation context
* @invocNode: responsible node in the stylesheet; used for error reports
* @node: the element node in the source tree
* @insert: the parent in the result tree
* @isLRE: indicates if @node is a Literal Result Element
* @topElemVisited: indicates if a top-most element was already processed
*
* Make a copy of the full tree under the element node @node
* and insert it as last child of @insert
*
* NOTE: Not to be used for Literal Result Elements.
*
* Used by:
* - xsltCopyOf()
*
* Returns a pointer to the new tree, or NULL in case of error
*/
static xmlNodePtr
xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
xmlNodePtr node, xmlNodePtr insert, int isLRE,
int topElemVisited)
{
xmlNodePtr copy;
if (node == NULL)
return(NULL);
switch (node->type) {
case XML_ELEMENT_NODE:
case XML_ENTITY_REF_NODE:
case XML_ENTITY_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
break;
case XML_TEXT_NODE: {
int noenc = (node->name == xmlStringTextNoenc);
return(xsltCopyTextString(ctxt, insert, node->content, noenc));
}
case XML_CDATA_SECTION_NODE:
return(xsltCopyTextString(ctxt, insert, node->content, 0));
case XML_ATTRIBUTE_NODE:
return((xmlNodePtr)
xsltShallowCopyAttr(ctxt, invocNode, insert, (xmlAttrPtr) node));
case XML_NAMESPACE_DECL:
return((xmlNodePtr) xsltShallowCopyNsNode(ctxt, invocNode,
insert, (xmlNsPtr) node));
case XML_DOCUMENT_TYPE_NODE:
case XML_DOCUMENT_FRAG_NODE:
case XML_NOTATION_NODE:
case XML_DTD_NODE:
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
return(NULL);
}
if (XSLT_IS_RES_TREE_FRAG(node)) {
if (node->children != NULL)
copy = xsltCopyTreeList(ctxt, invocNode,
node->children, insert, 0, 0);
else
copy = NULL;
return(copy);
}
copy = xmlDocCopyNode(node, insert->doc, 0);
if (copy != NULL) {
copy->doc = ctxt->output;
copy = xsltAddChild(insert, copy);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, invocNode,
"xsltCopyTree: Copying of '%s' failed.\n", node->name);
return (copy);
}
/*
* The node may have been coalesced into another text node.
*/
if (insert->last != copy)
return(insert->last);
copy->next = NULL;
if (node->type == XML_ELEMENT_NODE) {
/*
* Copy in-scope namespace nodes.
*
* REVISIT: Since we try to reuse existing in-scope ns-decls by
* using xmlSearchNsByHref(), this will eventually change
* the prefix of an original ns-binding; thus it might
* break QNames in element/attribute content.
* OPTIMIZE TODO: If we had a xmlNsPtr * on the transformation
* context, plus a ns-lookup function, which writes directly
* to a given list, then we wouldn't need to create/free the
* nsList every time.
*/
if ((topElemVisited == 0) &&
(node->parent != NULL) &&
(node->parent->type != XML_DOCUMENT_NODE) &&
(node->parent->type != XML_HTML_DOCUMENT_NODE))
{
xmlNsPtr *nsList, *curns, ns;
/*
* If this is a top-most element in a tree to be
* copied, then we need to ensure that all in-scope
* namespaces are copied over. For nodes deeper in the
* tree, it is sufficient to reconcile only the ns-decls
* (node->nsDef entries).
*/
nsList = xmlGetNsList(node->doc, node);
if (nsList != NULL) {
curns = nsList;
do {
/*
* Search by prefix first in order to break as less
* QNames in element/attribute content as possible.
*/
ns = xmlSearchNs(insert->doc, insert,
(*curns)->prefix);
if ((ns == NULL) ||
(! xmlStrEqual(ns->href, (*curns)->href)))
{
ns = NULL;
/*
* Search by namespace name.
* REVISIT TODO: Currently disabled.
*/
#if 0
ns = xmlSearchNsByHref(insert->doc,
insert, (*curns)->href);
#endif
}
if (ns == NULL) {
/*
* Declare a new namespace on the copied element.
*/
ns = xmlNewNs(copy, (*curns)->href,
(*curns)->prefix);
/* TODO: Handle errors */
}
if (node->ns == *curns) {
/*
* If this was the original's namespace then set
* the generated counterpart on the copy.
*/
copy->ns = ns;
}
curns++;
} while (*curns != NULL);
xmlFree(nsList);
}
} else if (node->nsDef != NULL) {
/*
* Copy over all namespace declaration attributes.
*/
if (node->nsDef != NULL) {
if (isLRE)
xsltCopyNamespaceList(ctxt, copy, node->nsDef);
else
xsltCopyNamespaceListInternal(copy, node->nsDef);
}
}
/*
* Set the namespace.
*/
if (node->ns != NULL) {
if (copy->ns == NULL) {
/*
* This will map copy->ns to one of the newly created
* in-scope ns-decls, OR create a new ns-decl on @copy.
*/
copy->ns = xsltGetSpecialNamespace(ctxt, invocNode,
node->ns->href, node->ns->prefix, copy);
}
} else if ((insert->type == XML_ELEMENT_NODE) &&
(insert->ns != NULL))
{
/*
* "Undeclare" the default namespace on @copy with xmlns="".
*/
xsltGetSpecialNamespace(ctxt, invocNode, NULL, NULL, copy);
}
/*
* Copy attribute nodes.
*/
if (node->properties != NULL) {
xsltCopyAttrListNoOverwrite(ctxt, invocNode,
copy, node->properties);
}
if (topElemVisited == 0)
topElemVisited = 1;
}
/*
* Copy the subtree.
*/
if (node->children != NULL) {
xsltCopyTreeList(ctxt, invocNode,
node->children, copy, isLRE, topElemVisited);
}
} else {
xsltTransformError(ctxt, NULL, invocNode,
"xsltCopyTree: Copying of '%s' failed.\n", node->name);
}
return(copy);
}
/************************************************************************
* *
* Error/fallback processing *
* *
************************************************************************/
/**
* xsltApplyFallbacks:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the node generating the error
*
* Process possible xsl:fallback nodes present under @inst
*
* Returns the number of xsl:fallback element found and processed
*/
static int
xsltApplyFallbacks(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst) {
xmlNodePtr child;
int ret = 0;
if ((ctxt == NULL) || (node == NULL) || (inst == NULL) ||
(inst->children == NULL))
return(0);
child = inst->children;
while (child != NULL) {
if ((IS_XSLT_ELEM(child)) &&
(xmlStrEqual(child->name, BAD_CAST "fallback"))) {
#ifdef WITH_XSLT_DEBUG_PARSING
xsltGenericDebug(xsltGenericDebugContext,
"applying xsl:fallback\n");
#endif
ret++;
xsltApplySequenceConstructor(ctxt, node, child->children,
NULL);
}
child = child->next;
}
return(ret);
}
/************************************************************************
* *
* Default processing *
* *
************************************************************************/
/**
* xsltDefaultProcessOneNode:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @params: extra parameters passed to the template if any
*
* Process the source node with the default built-in template rule:
* <xsl:template match="*|/">
* <xsl:apply-templates/>
* </xsl:template>
*
* and
*
* <xsl:template match="text()|@*">
* <xsl:value-of select="."/>
* </xsl:template>
*
* Note also that namespace declarations are copied directly:
*
* the built-in template rule is the only template rule that is applied
* for namespace nodes.
*/
static void
xsltDefaultProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr node,
xsltStackElemPtr params) {
xmlNodePtr copy;
xmlNodePtr delete = NULL, cur;
int nbchild = 0, oldSize;
int childno = 0, oldPos;
xsltTemplatePtr template;
CHECK_STOPPED;
/*
* Handling of leaves
*/
switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
case XML_ELEMENT_NODE:
break;
case XML_CDATA_SECTION_NODE:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy CDATA %s\n",
node->content));
#endif
copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, node,
"xsltDefaultProcessOneNode: cdata copy failed\n");
}
return;
case XML_TEXT_NODE:
#ifdef WITH_XSLT_DEBUG_PROCESS
if (node->content == NULL) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy empty text\n"));
return;
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy text %s\n",
node->content));
}
#endif
copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, node,
"xsltDefaultProcessOneNode: text copy failed\n");
}
return;
case XML_ATTRIBUTE_NODE:
cur = node->children;
while ((cur != NULL) && (cur->type != XML_TEXT_NODE))
cur = cur->next;
if (cur == NULL) {
xsltTransformError(ctxt, NULL, node,
"xsltDefaultProcessOneNode: no text for attribute\n");
} else {
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->content == NULL) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy empty text\n"));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy text %s\n",
cur->content));
}
#endif
copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, node,
"xsltDefaultProcessOneNode: text copy failed\n");
}
}
return;
default:
return;
}
/*
* Handling of Elements: first pass, cleanup and counting
*/
cur = node->children;
while (cur != NULL) {
switch (cur->type) {
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
case XML_ELEMENT_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
nbchild++;
break;
case XML_DTD_NODE:
/* Unlink the DTD, it's still reachable using doc->intSubset */
if (cur->next != NULL)
cur->next->prev = cur->prev;
if (cur->prev != NULL)
cur->prev->next = cur->next;
break;
default:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: skipping node type %d\n",
cur->type));
#endif
delete = cur;
}
cur = cur->next;
if (delete != NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: removing ignorable blank node\n"));
#endif
xmlUnlinkNode(delete);
xmlFreeNode(delete);
delete = NULL;
}
}
if (delete != NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: removing ignorable blank node\n"));
#endif
xmlUnlinkNode(delete);
xmlFreeNode(delete);
delete = NULL;
}
/*
* Handling of Elements: second pass, actual processing
*
* Note that params are passed to the next template. This matches
* XSLT 2.0 behavior but doesn't conform to XSLT 1.0.
*/
oldSize = ctxt->xpathCtxt->contextSize;
oldPos = ctxt->xpathCtxt->proximityPosition;
cur = node->children;
while (cur != NULL) {
childno++;
switch (cur->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
case XML_ELEMENT_NODE:
ctxt->xpathCtxt->contextSize = nbchild;
ctxt->xpathCtxt->proximityPosition = childno;
xsltProcessOneNode(ctxt, cur, params);
break;
case XML_CDATA_SECTION_NODE:
template = xsltGetTemplate(ctxt, cur, NULL);
if (template) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: applying template for CDATA %s\n",
cur->content));
#endif
/*
* Instantiate the xsl:template.
*/
xsltApplyXSLTTemplate(ctxt, cur, template->content,
template, params);
} else /* if (ctxt->mode == NULL) */ {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy CDATA %s\n",
cur->content));
#endif
copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, cur,
"xsltDefaultProcessOneNode: cdata copy failed\n");
}
}
break;
case XML_TEXT_NODE:
template = xsltGetTemplate(ctxt, cur, NULL);
if (template) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: applying template for text %s\n",
cur->content));
#endif
ctxt->xpathCtxt->contextSize = nbchild;
ctxt->xpathCtxt->proximityPosition = childno;
/*
* Instantiate the xsl:template.
*/
xsltApplyXSLTTemplate(ctxt, cur, template->content,
template, params);
} else /* if (ctxt->mode == NULL) */ {
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->content == NULL) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy empty text\n"));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: copy text %s\n",
cur->content));
}
#endif
copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, cur,
"xsltDefaultProcessOneNode: text copy failed\n");
}
}
break;
case XML_PI_NODE:
case XML_COMMENT_NODE:
template = xsltGetTemplate(ctxt, cur, NULL);
if (template) {
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->type == XML_PI_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: template found for PI %s\n",
cur->name));
} else if (cur->type == XML_COMMENT_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltDefaultProcessOneNode: template found for comment\n"));
}
#endif
ctxt->xpathCtxt->contextSize = nbchild;
ctxt->xpathCtxt->proximityPosition = childno;
/*
* Instantiate the xsl:template.
*/
xsltApplyXSLTTemplate(ctxt, cur, template->content,
template, params);
}
break;
default:
break;
}
cur = cur->next;
}
ctxt->xpathCtxt->contextSize = oldSize;
ctxt->xpathCtxt->proximityPosition = oldPos;
}
/**
* xsltProcessOneNode:
* @ctxt: a XSLT process context
* @contextNode: the "current node" in the source tree
* @withParams: extra parameters (e.g. xsl:with-param) passed to the
* template if any
*
* Process the source node.
*/
void
xsltProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
xsltStackElemPtr withParams)
{
xsltTemplatePtr templ;
xmlNodePtr oldNode;
templ = xsltGetTemplate(ctxt, contextNode, NULL);
/*
* If no template is found, apply the default rule.
*/
if (templ == NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
if (contextNode->type == XML_DOCUMENT_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: no template found for /\n"));
} else if (contextNode->type == XML_CDATA_SECTION_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: no template found for CDATA\n"));
} else if (contextNode->type == XML_ATTRIBUTE_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: no template found for attribute %s\n",
((xmlAttrPtr) contextNode)->name));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: no template found for %s\n", contextNode->name));
}
#endif
oldNode = ctxt->node;
ctxt->node = contextNode;
xsltDefaultProcessOneNode(ctxt, contextNode, withParams);
ctxt->node = oldNode;
return;
}
if (contextNode->type == XML_ATTRIBUTE_NODE) {
xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
/*
* Set the "current template rule".
*/
ctxt->currentTemplateRule = templ;
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: applying template '%s' for attribute %s\n",
templ->match, contextNode->name));
#endif
xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
ctxt->currentTemplateRule = oldCurTempRule;
} else {
xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
/*
* Set the "current template rule".
*/
ctxt->currentTemplateRule = templ;
#ifdef WITH_XSLT_DEBUG_PROCESS
if (contextNode->type == XML_DOCUMENT_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: applying template '%s' for /\n",
templ->match));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessOneNode: applying template '%s' for %s\n",
templ->match, contextNode->name));
}
#endif
xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
ctxt->currentTemplateRule = oldCurTempRule;
}
}
static xmlNodePtr
xsltDebuggerStartSequenceConstructor(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode,
xmlNodePtr list,
xsltTemplatePtr templ,
int *addCallResult)
{
xmlNodePtr debugedNode = NULL;
if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
if (templ) {
*addCallResult = xslAddCall(templ, templ->elem);
} else {
*addCallResult = xslAddCall(NULL, list);
}
switch (ctxt->debugStatus) {
case XSLT_DEBUG_RUN_RESTART:
case XSLT_DEBUG_QUIT:
if (*addCallResult)
xslDropCall();
return(NULL);
}
if (templ) {
xslHandleDebugger(templ->elem, contextNode, templ, ctxt);
debugedNode = templ->elem;
} else if (list) {
xslHandleDebugger(list, contextNode, templ, ctxt);
debugedNode = list;
} else if (ctxt->inst) {
xslHandleDebugger(ctxt->inst, contextNode, templ, ctxt);
debugedNode = ctxt->inst;
}
}
return(debugedNode);
}
/**
* xsltLocalVariablePush:
* @ctxt: the transformation context
* @variable: variable to be pushed to the variable stack
* @level: new value for variable's level
*
* Places the variable onto the local variable stack
*
* Returns: 0 for success, -1 for any error
* **NOTE:**
* This is an internal routine and should not be called by users!
*/
int
xsltLocalVariablePush(xsltTransformContextPtr ctxt,
xsltStackElemPtr variable,
int level)
{
if (ctxt->varsMax == 0) {
ctxt->varsMax = 10;
ctxt->varsTab =
(xsltStackElemPtr *) xmlMalloc(ctxt->varsMax *
sizeof(ctxt->varsTab[0]));
if (ctxt->varsTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
return (-1);
}
}
if (ctxt->varsNr >= ctxt->varsMax) {
ctxt->varsMax *= 2;
ctxt->varsTab =
(xsltStackElemPtr *) xmlRealloc(ctxt->varsTab,
ctxt->varsMax *
sizeof(ctxt->varsTab[0]));
if (ctxt->varsTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
return (-1);
}
}
ctxt->varsTab[ctxt->varsNr++] = variable;
ctxt->vars = variable;
variable->level = level;
return(0);
}
/**
* xsltReleaseLocalRVTs:
*
* Fragments which are results of extension instructions
* are preserved; all other fragments are freed/cached.
*/
static void
xsltReleaseLocalRVTs(xsltTransformContextPtr ctxt, xmlDocPtr base)
{
xmlDocPtr cur = ctxt->localRVT, tmp;
if (cur == base)
return;
if (cur->prev != NULL)
xsltTransformError(ctxt, NULL, NULL, "localRVT not head of list\n");
do {
tmp = cur;
cur = (xmlDocPtr) cur->next;
if (tmp->psvi == XSLT_RVT_LOCAL) {
xsltReleaseRVT(ctxt, tmp);
} else if (tmp->psvi == XSLT_RVT_GLOBAL) {
xsltRegisterPersistRVT(ctxt, tmp);
} else if (tmp->psvi != XSLT_RVT_FUNC_RESULT) {
xmlGenericError(xmlGenericErrorContext,
"xsltReleaseLocalRVTs: Unexpected RVT flag %p\n",
tmp->psvi);
}
} while (cur != base);
if (base != NULL)
base->prev = NULL;
ctxt->localRVT = base;
}
/**
* xsltApplySequenceConstructor:
* @ctxt: a XSLT process context
* @contextNode: the "current node" in the source tree
* @list: the nodes of a sequence constructor;
* (plus leading xsl:param elements)
* @templ: the compiled xsl:template (optional)
*
* Processes a sequence constructor.
*
* NOTE: ctxt->currentTemplateRule was introduced to reflect the
* semantics of "current template rule". I.e. the field ctxt->templ
* is not intended to reflect this, thus always pushed onto the
* template stack.
*/
static void
xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode, xmlNodePtr list,
xsltTemplatePtr templ)
{
xmlNodePtr oldInsert, oldInst, oldCurInst, oldContextNode;
xmlNodePtr cur, insert, copy = NULL;
int level = 0, oldVarsNr;
xmlDocPtr oldLocalFragmentTop;
#ifdef XSLT_REFACTORED
xsltStylePreCompPtr info;
#endif
#ifdef WITH_DEBUGGER
int addCallResult = 0;
xmlNodePtr debuggedNode = NULL;
#endif
if (ctxt == NULL)
return;
#ifdef WITH_DEBUGGER
if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
debuggedNode =
xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
list, templ, &addCallResult);
if (debuggedNode == NULL)
return;
}
#endif
if (list == NULL)
return;
CHECK_STOPPED;
/*
* Check for infinite recursion: stop if the maximum of nested templates
* is excceeded. Adjust xsltMaxDepth if you need more.
*/
if (ctxt->depth >= ctxt->maxTemplateDepth) {
xsltTransformError(ctxt, NULL, list,
"xsltApplySequenceConstructor: A potential infinite template "
"recursion was detected.\n"
"You can adjust xsltMaxDepth (--maxdepth) in order to "
"raise the maximum number of nested template calls and "
"variables/params (currently set to %d).\n",
ctxt->maxTemplateDepth);
xsltDebug(ctxt, contextNode, list, NULL);
ctxt->state = XSLT_STATE_STOPPED;
return;
}
ctxt->depth++;
oldLocalFragmentTop = ctxt->localRVT;
oldInsert = insert = ctxt->insert;
oldInst = oldCurInst = ctxt->inst;
oldContextNode = ctxt->node;
/*
* Save current number of variables on the stack; new vars are popped when
* exiting.
*/
oldVarsNr = ctxt->varsNr;
/*
* Process the sequence constructor.
*/
cur = list;
while (cur != NULL) {
ctxt->inst = cur;
#ifdef WITH_DEBUGGER
switch (ctxt->debugStatus) {
case XSLT_DEBUG_RUN_RESTART:
case XSLT_DEBUG_QUIT:
break;
}
#endif
/*
* Test; we must have a valid insertion point.
*/
if (insert == NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: insert == NULL !\n"));
#endif
goto error;
}
#ifdef WITH_DEBUGGER
if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (debuggedNode != cur))
xslHandleDebugger(cur, contextNode, templ, ctxt);
#endif
#ifdef XSLT_REFACTORED
if (cur->type == XML_ELEMENT_NODE) {
info = (xsltStylePreCompPtr) cur->psvi;
/*
* We expect a compiled representation on:
* 1) XSLT instructions of this XSLT version (1.0)
* (with a few exceptions)
* 2) Literal result elements
* 3) Extension instructions
* 4) XSLT instructions of future XSLT versions
* (forwards-compatible mode).
*/
if (info == NULL) {
/*
* Handle the rare cases where we don't expect a compiled
* representation on an XSLT element.
*/
if (IS_XSLT_ELEM_FAST(cur) && IS_XSLT_NAME(cur, "message")) {
xsltMessage(ctxt, contextNode, cur);
goto skip_children;
}
/*
* Something really went wrong:
*/
xsltTransformError(ctxt, NULL, cur,
"Internal error in xsltApplySequenceConstructor(): "
"The element '%s' in the stylesheet has no compiled "
"representation.\n",
cur->name);
goto skip_children;
}
if (info->type == XSLT_FUNC_LITERAL_RESULT_ELEMENT) {
xsltStyleItemLRElementInfoPtr lrInfo =
(xsltStyleItemLRElementInfoPtr) info;
/*
* Literal result elements
* --------------------------------------------------------
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy literal result "
"element '%s'\n", cur->name));
#endif
/*
* Copy the raw element-node.
* OLD: if ((copy = xsltShallowCopyElem(ctxt, cur, insert))
* == NULL)
* goto error;
*/
copy = xmlDocCopyNode(cur, insert->doc, 0);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, cur,
"Internal error in xsltApplySequenceConstructor(): "
"Failed to copy literal result element '%s'.\n",
cur->name);
goto error;
} else {
/*
* Add the element-node to the result tree.
*/
copy->doc = ctxt->output;
copy = xsltAddChild(insert, copy);
/*
* Create effective namespaces declarations.
* OLD: xsltCopyNamespaceList(ctxt, copy, cur->nsDef);
*/
if (lrInfo->effectiveNs != NULL) {
xsltEffectiveNsPtr effNs = lrInfo->effectiveNs;
xmlNsPtr ns, lastns = NULL;
while (effNs != NULL) {
/*
* Avoid generating redundant namespace
* declarations; thus lookup if there is already
* such a ns-decl in the result.
*/
ns = xmlSearchNs(copy->doc, copy, effNs->prefix);
if ((ns != NULL) &&
(xmlStrEqual(ns->href, effNs->nsName)))
{
effNs = effNs->next;
continue;
}
ns = xmlNewNs(copy, effNs->nsName, effNs->prefix);
if (ns == NULL) {
xsltTransformError(ctxt, NULL, cur,
"Internal error in "
"xsltApplySequenceConstructor(): "
"Failed to copy a namespace "
"declaration.\n");
goto error;
}
if (lastns == NULL)
copy->nsDef = ns;
else
lastns->next =ns;
lastns = ns;
effNs = effNs->next;
}
}
/*
* NOTE that we don't need to apply ns-alising: this was
* already done at compile-time.
*/
if (cur->ns != NULL) {
/*
* If there's no such ns-decl in the result tree,
* then xsltGetSpecialNamespace() will
* create a ns-decl on the copied node.
*/
copy->ns = xsltGetSpecialNamespace(ctxt, cur,
cur->ns->href, cur->ns->prefix, copy);
} else {
/*
* Undeclare the default namespace if needed.
* This can be skipped, if the result element has
* no ns-decls, in which case the result element
* obviously does not declare a default namespace;
* AND there's either no parent, or the parent
* element is in no namespace; this means there's no
* default namespace is scope to care about.
*
* REVISIT: This might result in massive
* generation of ns-decls if nodes in a default
* namespaces are mixed with nodes in no namespace.
*
*/
if (copy->nsDef ||
((insert != NULL) &&
(insert->type == XML_ELEMENT_NODE) &&
(insert->ns != NULL)))
{
xsltGetSpecialNamespace(ctxt, cur,
NULL, NULL, copy);
}
}
}
/*
* SPEC XSLT 2.0 "Each attribute of the literal result
* element, other than an attribute in the XSLT namespace,
* is processed to produce an attribute for the element in
* the result tree."
* NOTE: See bug #341325.
*/
if (cur->properties != NULL) {
xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
}
} else if (IS_XSLT_ELEM_FAST(cur)) {
/*
* XSLT instructions
* --------------------------------------------------------
*/
if (info->type == XSLT_FUNC_UNKOWN_FORWARDS_COMPAT) {
/*
* We hit an unknown XSLT element.
* Try to apply one of the fallback cases.
*/
ctxt->insert = insert;
if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
xsltTransformError(ctxt, NULL, cur,
"The is no fallback behaviour defined for "
"the unknown XSLT element '%s'.\n",
cur->name);
}
ctxt->insert = oldInsert;
} else if (info->func != NULL) {
/*
* Execute the XSLT instruction.
*/
ctxt->insert = insert;
info->func(ctxt, contextNode, cur,
(xsltElemPreCompPtr) info);
/*
* Cleanup temporary tree fragments.
*/
if (oldLocalFragmentTop != ctxt->localRVT)
xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
ctxt->insert = oldInsert;
} else if (info->type == XSLT_FUNC_VARIABLE) {
xsltStackElemPtr tmpvar = ctxt->vars;
xsltParseStylesheetVariable(ctxt, cur);
if (tmpvar != ctxt->vars) {
/*
* TODO: Using a @tmpvar is an annoying workaround, but
* the current mechanisms do not provide any other way
* of knowing if the var was really pushed onto the
* stack.
*/
ctxt->vars->level = level;
}
} else if (info->type == XSLT_FUNC_MESSAGE) {
/*
* TODO: Won't be hit, since we don't compile xsl:message.
*/
xsltMessage(ctxt, contextNode, cur);
} else {
xsltTransformError(ctxt, NULL, cur,
"Unexpected XSLT element '%s'.\n", cur->name);
}
goto skip_children;
} else {
xsltTransformFunction func;
/*
* Extension intructions (elements)
* --------------------------------------------------------
*/
if (cur->psvi == xsltExtMarker) {
/*
* The xsltExtMarker was set during the compilation
* of extension instructions if there was no registered
* handler for this specific extension function at
* compile-time.
* Libxslt will now lookup if a handler is
* registered in the context of this transformation.
*/
func = (xsltTransformFunction)
xsltExtElementLookup(ctxt, cur->name, cur->ns->href);
} else
func = ((xsltElemPreCompPtr) cur->psvi)->func;
if (func == NULL) {
/*
* No handler available.
* Try to execute fallback behaviour via xsl:fallback.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: unknown extension %s\n",
cur->name));
#endif
ctxt->insert = insert;
if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
xsltTransformError(ctxt, NULL, cur,
"Unknown extension instruction '{%s}%s'.\n",
cur->ns->href, cur->name);
}
ctxt->insert = oldInsert;
} else {
/*
* Execute the handler-callback.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: extension construct %s\n",
cur->name));
#endif
/*
* Disable the xsltCopyTextString optimization for
* extension elements. Extensions could append text using
* xmlAddChild which will free the buffer pointed to by
* 'lasttext'. This buffer could later be reallocated with
* a different size than recorded in 'lasttsize'. See bug
* #777432.
*/
if (cur->psvi == xsltExtMarker) {
ctxt->lasttext = NULL;
}
ctxt->insert = insert;
func(ctxt, contextNode, cur, cur->psvi);
/*
* Cleanup temporary tree fragments.
*/
if (oldLocalFragmentTop != ctxt->localRVT)
xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
ctxt->insert = oldInsert;
}
goto skip_children;
}
} else if (XSLT_IS_TEXT_NODE(cur)) {
/*
* Text
* ------------------------------------------------------------
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->name == xmlStringTextNoenc) {
XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy unescaped text '%s'\n",
cur->content));
} else {
XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy text '%s'\n",
cur->content));
}
#endif
if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
goto error;
}
#else /* XSLT_REFACTORED */
if (IS_XSLT_ELEM(cur)) {
/*
* This is an XSLT node
*/
xsltStylePreCompPtr info = (xsltStylePreCompPtr) cur->psvi;
if (info == NULL) {
if (IS_XSLT_NAME(cur, "message")) {
xsltMessage(ctxt, contextNode, cur);
} else {
/*
* That's an error try to apply one of the fallback cases
*/
ctxt->insert = insert;
if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
xsltGenericError(xsltGenericErrorContext,
"xsltApplySequenceConstructor: %s was not compiled\n",
cur->name);
}
ctxt->insert = oldInsert;
}
goto skip_children;
}
if (info->func != NULL) {
oldCurInst = ctxt->inst;
ctxt->inst = cur;
ctxt->insert = insert;
info->func(ctxt, contextNode, cur, (xsltElemPreCompPtr) info);
/*
* Cleanup temporary tree fragments.
*/
if (oldLocalFragmentTop != ctxt->localRVT)
xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
ctxt->insert = oldInsert;
ctxt->inst = oldCurInst;
goto skip_children;
}
if (IS_XSLT_NAME(cur, "variable")) {
xsltStackElemPtr tmpvar = ctxt->vars;
oldCurInst = ctxt->inst;
ctxt->inst = cur;
xsltParseStylesheetVariable(ctxt, cur);
ctxt->inst = oldCurInst;
if (tmpvar != ctxt->vars) {
/*
* TODO: Using a @tmpvar is an annoying workaround, but
* the current mechanisms do not provide any other way
* of knowing if the var was really pushed onto the
* stack.
*/
ctxt->vars->level = level;
}
} else if (IS_XSLT_NAME(cur, "message")) {
xsltMessage(ctxt, contextNode, cur);
} else {
xsltTransformError(ctxt, NULL, cur,
"Unexpected XSLT element '%s'.\n", cur->name);
}
goto skip_children;
} else if ((cur->type == XML_TEXT_NODE) ||
(cur->type == XML_CDATA_SECTION_NODE)) {
/*
* This text comes from the stylesheet
* For stylesheets, the set of whitespace-preserving
* element names consists of just xsl:text.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
if (cur->type == XML_CDATA_SECTION_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy CDATA text %s\n",
cur->content));
} else if (cur->name == xmlStringTextNoenc) {
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy unescaped text %s\n",
cur->content));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy text %s\n",
cur->content));
}
#endif
if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
goto error;
} else if ((cur->type == XML_ELEMENT_NODE) &&
(cur->ns != NULL) && (cur->psvi != NULL)) {
xsltTransformFunction function;
oldCurInst = ctxt->inst;
ctxt->inst = cur;
/*
* Flagged as an extension element
*/
if (cur->psvi == xsltExtMarker)
function = (xsltTransformFunction)
xsltExtElementLookup(ctxt, cur->name, cur->ns->href);
else
function = ((xsltElemPreCompPtr) cur->psvi)->func;
if (function == NULL) {
xmlNodePtr child;
int found = 0;
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: unknown extension %s\n",
cur->name));
#endif
/*
* Search if there are fallbacks
*/
child = cur->children;
while (child != NULL) {
if ((IS_XSLT_ELEM(child)) &&
(IS_XSLT_NAME(child, "fallback")))
{
found = 1;
xsltApplySequenceConstructor(ctxt, contextNode,
child->children, NULL);
}
child = child->next;
}
if (!found) {
xsltTransformError(ctxt, NULL, cur,
"xsltApplySequenceConstructor: failed to find extension %s\n",
cur->name);
}
} else {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: extension construct %s\n",
cur->name));
#endif
/*
* Disable the xsltCopyTextString optimization for
* extension elements. Extensions could append text using
* xmlAddChild which will free the buffer pointed to by
* 'lasttext'. This buffer could later be reallocated with
* a different size than recorded in 'lasttsize'. See bug
* #777432.
*/
if (cur->psvi == xsltExtMarker) {
ctxt->lasttext = NULL;
}
ctxt->insert = insert;
function(ctxt, contextNode, cur, cur->psvi);
/*
* Cleanup temporary tree fragments.
*/
if (oldLocalFragmentTop != ctxt->localRVT)
xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
ctxt->insert = oldInsert;
}
ctxt->inst = oldCurInst;
goto skip_children;
} else if (cur->type == XML_ELEMENT_NODE) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplySequenceConstructor: copy node %s\n",
cur->name));
#endif
oldCurInst = ctxt->inst;
ctxt->inst = cur;
if ((copy = xsltShallowCopyElem(ctxt, cur, insert, 1)) == NULL)
goto error;
/*
* Add extra namespaces inherited from the current template
* if we are in the first level children and this is a
* "real" template.
*/
if ((templ != NULL) && (oldInsert == insert) &&
(ctxt->templ != NULL) && (ctxt->templ->inheritedNs != NULL)) {
int i;
xmlNsPtr ns, ret;
for (i = 0; i < ctxt->templ->inheritedNsNr; i++) {
const xmlChar *URI = NULL;
xsltStylesheetPtr style;
ns = ctxt->templ->inheritedNs[i];
/* Note that the XSLT namespace was already excluded
* in xsltGetInheritedNsList().
*/
#if 0
if (xmlStrEqual(ns->href, XSLT_NAMESPACE))
continue;
#endif
style = ctxt->style;
while (style != NULL) {
if (style->nsAliases != NULL)
URI = (const xmlChar *)
xmlHashLookup(style->nsAliases, ns->href);
if (URI != NULL)
break;
style = xsltNextImport(style);
}
if (URI == UNDEFINED_DEFAULT_NS)
continue;
if (URI == NULL)
URI = ns->href;
/*
* TODO: The following will still be buggy for the
* non-refactored code.
*/
ret = xmlSearchNs(copy->doc, copy, ns->prefix);
if ((ret == NULL) || (!xmlStrEqual(ret->href, URI)))
{
xmlNewNs(copy, URI, ns->prefix);
}
}
if (copy->ns != NULL) {
/*
* Fix the node namespace if needed
*/
copy->ns = xsltGetNamespace(ctxt, cur, copy->ns, copy);
}
}
/*
* all the attributes are directly inherited
*/
if (cur->properties != NULL) {
xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
}
ctxt->inst = oldCurInst;
}
#endif /* else of XSLT_REFACTORED */
/*
* Descend into content in document order.
*/
if (cur->children != NULL) {
if (cur->children->type != XML_ENTITY_DECL) {
cur = cur->children;
level++;
if (copy != NULL)
insert = copy;
continue;
}
}
skip_children:
/*
* If xslt:message was just processed, we might have hit a
* terminate='yes'; if so, then break the loop and clean up.
* TODO: Do we need to check this also before trying to descend
* into the content?
*/
if (ctxt->state == XSLT_STATE_STOPPED)
break;
if (cur->next != NULL) {
cur = cur->next;
continue;
}
do {
cur = cur->parent;
level--;
/*
* Pop variables/params (xsl:variable and xsl:param).
*/
if ((ctxt->varsNr > oldVarsNr) && (ctxt->vars->level > level)) {
xsltLocalVariablePop(ctxt, oldVarsNr, level);
}
insert = insert->parent;
if (cur == NULL)
break;
if (cur == list->parent) {
cur = NULL;
break;
}
if (cur->next != NULL) {
cur = cur->next;
break;
}
} while (cur != NULL);
}
error:
/*
* In case of errors: pop remaining variables.
*/
if (ctxt->varsNr > oldVarsNr)
xsltLocalVariablePop(ctxt, oldVarsNr, -1);
ctxt->node = oldContextNode;
ctxt->inst = oldInst;
ctxt->insert = oldInsert;
ctxt->depth--;
#ifdef WITH_DEBUGGER
if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
xslDropCall();
}
#endif
}
/*
* xsltApplyXSLTTemplate:
* @ctxt: a XSLT transformation context
* @contextNode: the node in the source tree.
* @list: the nodes of a sequence constructor;
* (plus leading xsl:param elements)
* @templ: the compiled xsl:template declaration;
* NULL if a sequence constructor
* @withParams: a set of caller-parameters (xsl:with-param) or NULL
*
* Called by:
* - xsltApplyImports()
* - xsltCallTemplate()
* - xsltDefaultProcessOneNode()
* - xsltProcessOneNode()
*/
static void
xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode,
xmlNodePtr list,
xsltTemplatePtr templ,
xsltStackElemPtr withParams)
{
int oldVarsBase = 0;
long start = 0;
xmlNodePtr cur;
xsltStackElemPtr tmpParam = NULL;
xmlDocPtr oldUserFragmentTop;
#ifdef XSLT_REFACTORED
xsltStyleItemParamPtr iparam;
#else
xsltStylePreCompPtr iparam;
#endif
#ifdef WITH_DEBUGGER
int addCallResult = 0;
#endif
if (ctxt == NULL)
return;
if (templ == NULL) {
xsltTransformError(ctxt, NULL, list,
"xsltApplyXSLTTemplate: Bad arguments; @templ is mandatory.\n");
return;
}
#ifdef WITH_DEBUGGER
if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
if (xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
list, templ, &addCallResult) == NULL)
return;
}
#endif
if (list == NULL)
return;
CHECK_STOPPED;
if (ctxt->varsNr >= ctxt->maxTemplateVars)
{
xsltTransformError(ctxt, NULL, list,
"xsltApplyXSLTTemplate: A potential infinite template recursion "
"was detected.\n"
"You can adjust maxTemplateVars (--maxvars) in order to "
"raise the maximum number of variables/params (currently set to %d).\n",
ctxt->maxTemplateVars);
xsltDebug(ctxt, contextNode, list, NULL);
ctxt->state = XSLT_STATE_STOPPED;
return;
}
oldUserFragmentTop = ctxt->tmpRVT;
ctxt->tmpRVT = NULL;
/*
* Initiate a distinct scope of local params/variables.
*/
oldVarsBase = ctxt->varsBase;
ctxt->varsBase = ctxt->varsNr;
ctxt->node = contextNode;
if (ctxt->profile) {
templ->nbCalls++;
start = xsltTimestamp();
profPush(ctxt, 0);
profCallgraphAdd(templ, ctxt->templ);
}
/*
* Push the xsl:template declaration onto the stack.
*/
templPush(ctxt, templ);
#ifdef WITH_XSLT_DEBUG_PROCESS
if (templ->name != NULL)
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"applying xsl:template '%s'\n", templ->name));
#endif
/*
* Process xsl:param instructions and skip those elements for
* further processing.
*/
cur = list;
do {
if (cur->type == XML_TEXT_NODE) {
cur = cur->next;
continue;
}
if ((cur->type != XML_ELEMENT_NODE) ||
(cur->name[0] != 'p') ||
(cur->psvi == NULL) ||
(! xmlStrEqual(cur->name, BAD_CAST "param")) ||
(! IS_XSLT_ELEM(cur)))
{
break;
}
list = cur->next;
#ifdef XSLT_REFACTORED
iparam = (xsltStyleItemParamPtr) cur->psvi;
#else
iparam = (xsltStylePreCompPtr) cur->psvi;
#endif
/*
* Substitute xsl:param for a given xsl:with-param.
* Since the XPath expression will reference the params/vars
* by index, we need to slot the xsl:with-params in the
* order of encountered xsl:params to keep the sequence of
* params/variables in the stack exactly as it was at
* compile time,
*/
tmpParam = NULL;
if (withParams) {
tmpParam = withParams;
do {
if ((tmpParam->name == (iparam->name)) &&
(tmpParam->nameURI == (iparam->ns)))
{
/*
* Push the caller-parameter.
*/
xsltLocalVariablePush(ctxt, tmpParam, -1);
break;
}
tmpParam = tmpParam->next;
} while (tmpParam != NULL);
}
/*
* Push the xsl:param.
*/
if (tmpParam == NULL) {
/*
* Note that we must assume that the added parameter
* has a @depth of 0.
*/
xsltParseStylesheetParam(ctxt, cur);
}
cur = cur->next;
} while (cur != NULL);
/*
* Process the sequence constructor.
*/
xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
/*
* Remove remaining xsl:param and xsl:with-param items from
* the stack. Don't free xsl:with-param items.
*/
if (ctxt->varsNr > ctxt->varsBase)
xsltTemplateParamsCleanup(ctxt);
ctxt->varsBase = oldVarsBase;
/*
* Release user-created fragments stored in the scope
* of xsl:template. Note that this mechanism is deprecated:
* user code should now use xsltRegisterLocalRVT() instead
* of the obsolete xsltRegisterTmpRVT().
*/
if (ctxt->tmpRVT) {
xmlDocPtr curdoc = ctxt->tmpRVT, tmp;
while (curdoc != NULL) {
tmp = curdoc;
curdoc = (xmlDocPtr) curdoc->next;
xsltReleaseRVT(ctxt, tmp);
}
}
ctxt->tmpRVT = oldUserFragmentTop;
/*
* Pop the xsl:template declaration from the stack.
*/
templPop(ctxt);
if (ctxt->profile) {
long spent, child, total, end;
end = xsltTimestamp();
child = profPop(ctxt);
total = end - start;
spent = total - child;
if (spent <= 0) {
/*
* Not possible unless the original calibration failed
* we can try to correct it on the fly.
*/
xsltCalibrateAdjust(spent);
spent = 0;
}
templ->time += spent;
if (ctxt->profNr > 0)
ctxt->profTab[ctxt->profNr - 1] += total;
}
#ifdef WITH_DEBUGGER
if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
xslDropCall();
}
#endif
}
/**
* xsltApplyOneTemplate:
* @ctxt: a XSLT process context
* @contextNode: the node in the source tree.
* @list: the nodes of a sequence constructor
* @templ: not used
* @params: a set of parameters (xsl:param) or NULL
*
* Processes a sequence constructor on the current node in the source tree.
*
* @params are the already computed variable stack items; this function
* pushes them on the variable stack, and pops them before exiting; it's
* left to the caller to free or reuse @params afterwards. The initial
* states of the variable stack will always be restored before this
* function exits.
* NOTE that this does *not* initiate a new distinct variable scope; i.e.
* variables already on the stack are visible to the process. The caller's
* side needs to start a new variable scope if needed (e.g. in exsl:function).
*
* @templ is obsolete and not used anymore (e.g. <exslt:function> does not
* provide a @templ); a non-NULL @templ might raise an error in the future.
*
* BIG NOTE: This function is not intended to process the content of an
* xsl:template; it does not expect xsl:param instructions in @list and
* will report errors if found.
*
* Called by:
* - xsltEvalVariable() (variables.c)
* - exsltFuncFunctionFunction() (libexsl/functions.c)
*/
void
xsltApplyOneTemplate(xsltTransformContextPtr ctxt,
xmlNodePtr contextNode,
xmlNodePtr list,
xsltTemplatePtr templ ATTRIBUTE_UNUSED,
xsltStackElemPtr params)
{
if ((ctxt == NULL) || (list == NULL))
return;
CHECK_STOPPED;
if (params) {
/*
* This code should be obsolete - was previously used
* by libexslt/functions.c, but due to bug 381319 the
* logic there was changed.
*/
int oldVarsNr = ctxt->varsNr;
/*
* Push the given xsl:param(s) onto the variable stack.
*/
while (params != NULL) {
xsltLocalVariablePush(ctxt, params, -1);
params = params->next;
}
xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
/*
* Pop the given xsl:param(s) from the stack but don't free them.
*/
xsltLocalVariablePop(ctxt, oldVarsNr, -2);
} else
xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
}
/************************************************************************
* *
* XSLT-1.1 extensions *
* *
************************************************************************/
/**
* xsltDocumentElem:
* @ctxt: an XSLT processing context
* @node: The current node
* @inst: the instruction in the stylesheet
* @castedComp: precomputed information
*
* Process an EXSLT/XSLT-1.1 document element
*/
void
xsltDocumentElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemDocumentPtr comp = (xsltStyleItemDocumentPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xsltStylesheetPtr style = NULL;
int ret;
xmlChar *filename = NULL, *prop, *elements;
xmlChar *element, *end;
xmlDocPtr res = NULL;
xmlDocPtr oldOutput;
xmlNodePtr oldInsert, root;
const char *oldOutputFile;
xsltOutputType oldType;
xmlChar *URL = NULL;
const xmlChar *method;
const xmlChar *doctypePublic;
const xmlChar *doctypeSystem;
const xmlChar *version;
const xmlChar *encoding;
int redirect_write_append = 0;
if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
return;
if (comp->filename == NULL) {
if (xmlStrEqual(inst->name, (const xmlChar *) "output")) {
/*
* The element "output" is in the namespace XSLT_SAXON_NAMESPACE
* (http://icl.com/saxon)
* The @file is in no namespace.
*/
#ifdef WITH_XSLT_DEBUG_EXTRA
xsltGenericDebug(xsltGenericDebugContext,
"Found saxon:output extension\n");
#endif
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "file",
XSLT_SAXON_NAMESPACE);
if (URL == NULL)
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "href",
XSLT_SAXON_NAMESPACE);
} else if (xmlStrEqual(inst->name, (const xmlChar *) "write")) {
#ifdef WITH_XSLT_DEBUG_EXTRA
xsltGenericDebug(xsltGenericDebugContext,
"Found xalan:write extension\n");
#endif
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"select",
XSLT_XALAN_NAMESPACE);
if (URL != NULL) {
xmlXPathCompExprPtr cmp;
xmlChar *val;
/*
* Trying to handle bug #59212
* The value of the "select" attribute is an
* XPath expression.
* (see http://xml.apache.org/xalan-j/extensionslib.html#redirect)
*/
cmp = xmlXPathCompile(URL);
val = xsltEvalXPathString(ctxt, cmp);
xmlXPathFreeCompExpr(cmp);
xmlFree(URL);
URL = val;
}
if (URL == NULL)
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"file",
XSLT_XALAN_NAMESPACE);
if (URL == NULL)
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"href",
XSLT_XALAN_NAMESPACE);
} else if (xmlStrEqual(inst->name, (const xmlChar *) "document")) {
URL = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "href",
NULL);
}
} else {
URL = xmlStrdup(comp->filename);
}
if (URL == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: href/URI-Reference not found\n");
return;
}
/*
* If the computation failed, it's likely that the URL wasn't escaped
*/
filename = xmlBuildURI(URL, (const xmlChar *) ctxt->outputFile);
if (filename == NULL) {
xmlChar *escURL;
escURL=xmlURIEscapeStr(URL, BAD_CAST ":/.?,");
if (escURL != NULL) {
filename = xmlBuildURI(escURL, (const xmlChar *) ctxt->outputFile);
xmlFree(escURL);
}
}
if (filename == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: URL computation failed for %s\n",
URL);
xmlFree(URL);
return;
}
/*
* Security checking: can we write to this resource
*/
if (ctxt->sec != NULL) {
ret = xsltCheckWrite(ctxt->sec, ctxt, filename);
if (ret <= 0) {
if (ret == 0)
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: write rights for %s denied\n",
filename);
xmlFree(URL);
xmlFree(filename);
return;
}
}
oldOutputFile = ctxt->outputFile;
oldOutput = ctxt->output;
oldInsert = ctxt->insert;
oldType = ctxt->type;
ctxt->outputFile = (const char *) filename;
style = xsltNewStylesheet();
if (style == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: out of memory\n");
goto error;
}
/*
* Version described in 1.1 draft allows full parameterization
* of the output.
*/
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "version",
NULL);
if (prop != NULL) {
if (style->version != NULL)
xmlFree(style->version);
style->version = prop;
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "encoding",
NULL);
if (prop != NULL) {
if (style->encoding != NULL)
xmlFree(style->encoding);
style->encoding = prop;
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "method",
NULL);
if (prop != NULL) {
const xmlChar *URI;
if (style->method != NULL)
xmlFree(style->method);
style->method = NULL;
if (style->methodURI != NULL)
xmlFree(style->methodURI);
style->methodURI = NULL;
URI = xsltGetQNameURI(inst, &prop);
if (prop == NULL) {
if (style != NULL) style->errors++;
} else if (URI == NULL) {
if ((xmlStrEqual(prop, (const xmlChar *) "xml")) ||
(xmlStrEqual(prop, (const xmlChar *) "html")) ||
(xmlStrEqual(prop, (const xmlChar *) "text"))) {
style->method = prop;
} else {
xsltTransformError(ctxt, NULL, inst,
"invalid value for method: %s\n", prop);
if (style != NULL) style->warnings++;
}
} else {
style->method = prop;
style->methodURI = xmlStrdup(URI);
}
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"doctype-system", NULL);
if (prop != NULL) {
if (style->doctypeSystem != NULL)
xmlFree(style->doctypeSystem);
style->doctypeSystem = prop;
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"doctype-public", NULL);
if (prop != NULL) {
if (style->doctypePublic != NULL)
xmlFree(style->doctypePublic);
style->doctypePublic = prop;
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "standalone",
NULL);
if (prop != NULL) {
if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
style->standalone = 1;
} else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
style->standalone = 0;
} else {
xsltTransformError(ctxt, NULL, inst,
"invalid value for standalone: %s\n",
prop);
if (style != NULL) style->warnings++;
}
xmlFree(prop);
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "indent",
NULL);
if (prop != NULL) {
if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
style->indent = 1;
} else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
style->indent = 0;
} else {
xsltTransformError(ctxt, NULL, inst,
"invalid value for indent: %s\n", prop);
if (style != NULL) style->warnings++;
}
xmlFree(prop);
}
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"omit-xml-declaration",
NULL);
if (prop != NULL) {
if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
style->omitXmlDeclaration = 1;
} else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
style->omitXmlDeclaration = 0;
} else {
xsltTransformError(ctxt, NULL, inst,
"invalid value for omit-xml-declaration: %s\n",
prop);
if (style != NULL) style->warnings++;
}
xmlFree(prop);
}
elements = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)
"cdata-section-elements",
NULL);
if (elements != NULL) {
if (style->stripSpaces == NULL)
style->stripSpaces = xmlHashCreate(10);
if (style->stripSpaces == NULL)
return;
element = elements;
while (*element != 0) {
while (IS_BLANK_CH(*element))
element++;
if (*element == 0)
break;
end = element;
while ((*end != 0) && (!IS_BLANK_CH(*end)))
end++;
element = xmlStrndup(element, end - element);
if (element) {
const xmlChar *URI;
#ifdef WITH_XSLT_DEBUG_PARSING
xsltGenericDebug(xsltGenericDebugContext,
"add cdata section output element %s\n",
element);
#endif
URI = xsltGetQNameURI(inst, &element);
xmlHashAddEntry2(style->stripSpaces, element, URI,
(xmlChar *) "cdata");
xmlFree(element);
}
element = end;
}
xmlFree(elements);
}
/*
* Create a new document tree and process the element template
*/
XSLT_GET_IMPORT_PTR(method, style, method)
XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
XSLT_GET_IMPORT_PTR(version, style, version)
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if ((method != NULL) &&
(!xmlStrEqual(method, (const xmlChar *) "xml"))) {
if (xmlStrEqual(method, (const xmlChar *) "html")) {
ctxt->type = XSLT_OUTPUT_HTML;
if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
res = htmlNewDoc(doctypeSystem, doctypePublic);
else {
if (version != NULL) {
#ifdef XSLT_GENERATE_HTML_DOCTYPE
xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
#endif
}
res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
}
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
} else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: unsupported method xhtml\n");
ctxt->type = XSLT_OUTPUT_HTML;
res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
} else if (xmlStrEqual(method, (const xmlChar *) "text")) {
ctxt->type = XSLT_OUTPUT_TEXT;
res = xmlNewDoc(style->version);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
} else {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: unsupported method (%s)\n",
method);
goto error;
}
} else {
ctxt->type = XSLT_OUTPUT_XML;
res = xmlNewDoc(style->version);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
}
res->charset = XML_CHAR_ENCODING_UTF8;
if (encoding != NULL)
res->encoding = xmlStrdup(encoding);
ctxt->output = res;
ctxt->insert = (xmlNodePtr) res;
xsltApplySequenceConstructor(ctxt, node, inst->children, NULL);
/*
* Do some post processing work depending on the generated output
*/
root = xmlDocGetRootElement(res);
if (root != NULL) {
const xmlChar *doctype = NULL;
if ((root->ns != NULL) && (root->ns->prefix != NULL))
doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
if (doctype == NULL)
doctype = root->name;
/*
* Apply the default selection of the method
*/
if ((method == NULL) &&
(root->ns == NULL) &&
(!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
xmlNodePtr tmp;
tmp = res->children;
while ((tmp != NULL) && (tmp != root)) {
if (tmp->type == XML_ELEMENT_NODE)
break;
if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
break;
tmp = tmp->next;
}
if (tmp == root) {
ctxt->type = XSLT_OUTPUT_HTML;
res->type = XML_HTML_DOCUMENT_NODE;
if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
res->intSubset = xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
#ifdef XSLT_GENERATE_HTML_DOCTYPE
} else if (version != NULL) {
xsltGetHTMLIDs(version, &doctypePublic,
&doctypeSystem);
if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
res->intSubset =
xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
#endif
}
}
}
if (ctxt->type == XSLT_OUTPUT_XML) {
XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
res->intSubset = xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
}
}
/*
* Calls to redirect:write also take an optional attribute append.
* Attribute append="true|yes" which will attempt to simply append
* to an existing file instead of always opening a new file. The
* default behavior of always overwriting the file still happens
* if we do not specify append.
* Note that append use will forbid use of remote URI target.
*/
prop = xsltEvalAttrValueTemplate(ctxt, inst, (const xmlChar *)"append",
NULL);
if (prop != NULL) {
if (xmlStrEqual(prop, (const xmlChar *) "true") ||
xmlStrEqual(prop, (const xmlChar *) "yes")) {
style->omitXmlDeclaration = 1;
redirect_write_append = 1;
} else
style->omitXmlDeclaration = 0;
xmlFree(prop);
}
if (redirect_write_append) {
FILE *f;
f = fopen((const char *) filename, "ab");
if (f == NULL) {
ret = -1;
} else {
ret = xsltSaveResultToFile(f, res, style);
fclose(f);
}
} else {
ret = xsltSaveResultToFilename((const char *) filename, res, style, 0);
}
if (ret < 0) {
xsltTransformError(ctxt, NULL, inst,
"xsltDocumentElem: unable to save to %s\n",
filename);
#ifdef WITH_XSLT_DEBUG_EXTRA
} else {
xsltGenericDebug(xsltGenericDebugContext,
"Wrote %d bytes to %s\n", ret, filename);
#endif
}
error:
ctxt->output = oldOutput;
ctxt->insert = oldInsert;
ctxt->type = oldType;
ctxt->outputFile = oldOutputFile;
if (URL != NULL)
xmlFree(URL);
if (filename != NULL)
xmlFree(filename);
if (style != NULL)
xsltFreeStylesheet(style);
if (res != NULL)
xmlFreeDoc(res);
}
/************************************************************************
* *
* Most of the XSLT-1.0 transformations *
* *
************************************************************************/
/**
* xsltSort:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt sort node
* @comp: precomputed information
*
* function attached to xsl:sort nodes, but this should not be
* called directly
*/
void
xsltSort(xsltTransformContextPtr ctxt,
xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
xsltStylePreCompPtr comp) {
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:sort : compilation failed\n");
return;
}
xsltTransformError(ctxt, NULL, inst,
"xsl:sort : improper use this should not be reached\n");
}
/**
* xsltCopy:
* @ctxt: an XSLT process context
* @node: the node in the source tree
* @inst: the element node of the XSLT-copy instruction
* @castedComp: computed information of the XSLT-copy instruction
*
* Execute the XSLT-copy instruction on the source node.
*/
void
xsltCopy(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemCopyPtr comp = (xsltStyleItemCopyPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xmlNodePtr copy, oldInsert;
oldInsert = ctxt->insert;
if (ctxt->insert != NULL) {
switch (node->type) {
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
/*
* This text comes from the stylesheet
* For stylesheets, the set of whitespace-preserving
* element names consists of just xsl:text.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
if (node->type == XML_CDATA_SECTION_NODE) {
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: CDATA text %s\n", node->content));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: text %s\n", node->content));
}
#endif
xsltCopyText(ctxt, ctxt->insert, node, 0);
break;
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
break;
case XML_ELEMENT_NODE:
/*
* REVISIT NOTE: The "fake" is a doc-node, not an element node.
* REMOVED:
* if (xmlStrEqual(node->name, BAD_CAST " fake node libxslt"))
* return;
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: node %s\n", node->name));
#endif
copy = xsltShallowCopyElem(ctxt, node, ctxt->insert, 0);
ctxt->insert = copy;
if (comp->use != NULL) {
xsltApplyAttributeSet(ctxt, node, inst, comp->use);
}
break;
case XML_ATTRIBUTE_NODE: {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: attribute %s\n", node->name));
#endif
/*
* REVISIT: We could also raise an error if the parent is not
* an element node.
* OPTIMIZE TODO: Can we set the value/children of the
* attribute without an intermediate copy of the string value?
*/
xsltShallowCopyAttr(ctxt, inst, ctxt->insert, (xmlAttrPtr) node);
break;
}
case XML_PI_NODE:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: PI %s\n", node->name));
#endif
copy = xmlNewDocPI(ctxt->insert->doc, node->name,
node->content);
copy = xsltAddChild(ctxt->insert, copy);
break;
case XML_COMMENT_NODE:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: comment\n"));
#endif
copy = xmlNewComment(node->content);
copy = xsltAddChild(ctxt->insert, copy);
break;
case XML_NAMESPACE_DECL:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopy: namespace declaration\n"));
#endif
xsltShallowCopyNsNode(ctxt, inst, ctxt->insert, (xmlNsPtr)node);
break;
default:
break;
}
}
switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
case XML_ELEMENT_NODE:
xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
NULL);
break;
default:
break;
}
ctxt->insert = oldInsert;
}
/**
* xsltText:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt text node
* @comp: precomputed information
*
* Process the xslt text node on the source node
*/
void
xsltText(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
xmlNodePtr inst, xsltStylePreCompPtr comp ATTRIBUTE_UNUSED) {
if ((inst->children != NULL) && (comp != NULL)) {
xmlNodePtr text = inst->children;
xmlNodePtr copy;
while (text != NULL) {
if ((text->type != XML_TEXT_NODE) &&
(text->type != XML_CDATA_SECTION_NODE)) {
xsltTransformError(ctxt, NULL, inst,
"xsl:text content problem\n");
break;
}
copy = xmlNewDocText(ctxt->output, text->content);
if (text->type != XML_CDATA_SECTION_NODE) {
#ifdef WITH_XSLT_DEBUG_PARSING
xsltGenericDebug(xsltGenericDebugContext,
"Disable escaping: %s\n", text->content);
#endif
copy->name = xmlStringTextNoenc;
}
copy = xsltAddChild(ctxt->insert, copy);
text = text->next;
}
}
}
/**
* xsltElement:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt element node
* @castedComp: precomputed information
*
* Process the xslt element node on the source node
*/
void
xsltElement(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp) {
#ifdef XSLT_REFACTORED
xsltStyleItemElementPtr comp = (xsltStyleItemElementPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xmlChar *prop = NULL;
const xmlChar *name, *prefix = NULL, *nsName = NULL;
xmlNodePtr copy;
xmlNodePtr oldInsert;
if (ctxt->insert == NULL)
return;
/*
* A comp->has_name == 0 indicates that we need to skip this instruction,
* since it was evaluated to be invalid already during compilation.
*/
if (!comp->has_name)
return;
/*
* stack and saves
*/
oldInsert = ctxt->insert;
if (comp->name == NULL) {
/* TODO: fix attr acquisition wrt to the XSLT namespace */
prop = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "name", XSLT_NAMESPACE);
if (prop == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:element: The attribute 'name' is missing.\n");
goto error;
}
if (xmlValidateQName(prop, 0)) {
xsltTransformError(ctxt, NULL, inst,
"xsl:element: The effective name '%s' is not a "
"valid QName.\n", prop);
/* we fall through to catch any further errors, if possible */
}
name = xsltSplitQName(ctxt->dict, prop, &prefix);
xmlFree(prop);
} else {
/*
* The "name" value was static.
*/
#ifdef XSLT_REFACTORED
prefix = comp->nsPrefix;
name = comp->name;
#else
name = xsltSplitQName(ctxt->dict, comp->name, &prefix);
#endif
}
/*
* Create the new element
*/
if (ctxt->output->dict == ctxt->dict) {
copy = xmlNewDocNodeEatName(ctxt->output, NULL, (xmlChar *)name, NULL);
} else {
copy = xmlNewDocNode(ctxt->output, NULL, (xmlChar *)name, NULL);
}
if (copy == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:element : creation of %s failed\n", name);
return;
}
copy = xsltAddChild(ctxt->insert, copy);
if (copy == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:element : xsltAddChild failed\n");
return;
}
/*
* Namespace
* ---------
*/
if (comp->has_ns) {
if (comp->ns != NULL) {
/*
* No AVT; just plain text for the namespace name.
*/
if (comp->ns[0] != 0)
nsName = comp->ns;
} else {
xmlChar *tmpNsName;
/*
* Eval the AVT.
*/
/* TODO: check attr acquisition wrt to the XSLT namespace */
tmpNsName = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *) "namespace", XSLT_NAMESPACE);
/*
* SPEC XSLT 1.0:
* "If the string is empty, then the expanded-name of the
* attribute has a null namespace URI."
*/
if ((tmpNsName != NULL) && (tmpNsName[0] != 0))
nsName = xmlDictLookup(ctxt->dict, BAD_CAST tmpNsName, -1);
xmlFree(tmpNsName);
}
if (xmlStrEqual(nsName, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
xsltTransformError(ctxt, NULL, inst,
"xsl:attribute: Namespace http://www.w3.org/2000/xmlns/ "
"forbidden.\n");
goto error;
}
if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
prefix = BAD_CAST "xml";
} else if (xmlStrEqual(prefix, BAD_CAST "xml")) {
prefix = NULL;
}
} else {
xmlNsPtr ns;
/*
* SPEC XSLT 1.0:
* "If the namespace attribute is not present, then the QName is
* expanded into an expanded-name using the namespace declarations
* in effect for the xsl:element element, including any default
* namespace declaration.
*/
ns = xmlSearchNs(inst->doc, inst, prefix);
if (ns == NULL) {
/*
* TODO: Check this in the compilation layer in case it's a
* static value.
*/
if (prefix != NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:element: The QName '%s:%s' has no "
"namespace binding in scope in the stylesheet; "
"this is an error, since the namespace was not "
"specified by the instruction itself.\n", prefix, name);
}
} else
nsName = ns->href;
}
/*
* Find/create a matching ns-decl in the result tree.
*/
if (nsName != NULL) {
if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
/* Don't use a prefix of "xmlns" */
xmlChar *pref = xmlStrdup(BAD_CAST "ns_1");
copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, pref, copy);
xmlFree(pref);
} else {
copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, prefix,
copy);
}
} else if ((copy->parent != NULL) &&
(copy->parent->type == XML_ELEMENT_NODE) &&
(copy->parent->ns != NULL))
{
/*
* "Undeclare" the default namespace.
*/
xsltGetSpecialNamespace(ctxt, inst, NULL, NULL, copy);
}
ctxt->insert = copy;
if (comp->has_use) {
if (comp->use != NULL) {
xsltApplyAttributeSet(ctxt, node, inst, comp->use);
} else {
xmlChar *attrSets = NULL;
/*
* BUG TODO: use-attribute-sets is not a value template.
* use-attribute-sets = qnames
*/
attrSets = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)"use-attribute-sets", NULL);
if (attrSets != NULL) {
xsltApplyAttributeSet(ctxt, node, inst, attrSets);
xmlFree(attrSets);
}
}
}
/*
* Instantiate the sequence constructor.
*/
if (inst->children != NULL)
xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
NULL);
error:
ctxt->insert = oldInsert;
return;
}
/**
* xsltComment:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt comment node
* @comp: precomputed information
*
* Process the xslt comment node on the source node
*/
void
xsltComment(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr comp ATTRIBUTE_UNUSED) {
xmlChar *value = NULL;
xmlNodePtr commentNode;
int len;
value = xsltEvalTemplateString(ctxt, node, inst);
/* TODO: use or generate the compiled form */
len = xmlStrlen(value);
if (len > 0) {
if ((value[len-1] == '-') ||
(xmlStrstr(value, BAD_CAST "--"))) {
xsltTransformError(ctxt, NULL, inst,
"xsl:comment : '--' or ending '-' not allowed in comment\n");
/* fall through to try to catch further errors */
}
}
#ifdef WITH_XSLT_DEBUG_PROCESS
if (value == NULL) {
XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
"xsltComment: empty\n"));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
"xsltComment: content %s\n", value));
}
#endif
commentNode = xmlNewComment(value);
commentNode = xsltAddChild(ctxt->insert, commentNode);
if (value != NULL)
xmlFree(value);
}
/**
* xsltProcessingInstruction:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt processing-instruction node
* @castedComp: precomputed information
*
* Process the xslt processing-instruction node on the source node
*/
void
xsltProcessingInstruction(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp) {
#ifdef XSLT_REFACTORED
xsltStyleItemPIPtr comp = (xsltStyleItemPIPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
const xmlChar *name;
xmlChar *value = NULL;
xmlNodePtr pi;
if (ctxt->insert == NULL)
return;
if (comp->has_name == 0)
return;
if (comp->name == NULL) {
name = xsltEvalAttrValueTemplate(ctxt, inst,
(const xmlChar *)"name", NULL);
if (name == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:processing-instruction : name is missing\n");
goto error;
}
} else {
name = comp->name;
}
/* TODO: check that it's both an an NCName and a PITarget. */
value = xsltEvalTemplateString(ctxt, node, inst);
if (xmlStrstr(value, BAD_CAST "?>") != NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:processing-instruction: '?>' not allowed within PI content\n");
goto error;
}
#ifdef WITH_XSLT_DEBUG_PROCESS
if (value == NULL) {
XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessingInstruction: %s empty\n", name));
} else {
XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
"xsltProcessingInstruction: %s content %s\n", name, value));
}
#endif
pi = xmlNewDocPI(ctxt->insert->doc, name, value);
pi = xsltAddChild(ctxt->insert, pi);
error:
if ((name != NULL) && (name != comp->name))
xmlFree((xmlChar *) name);
if (value != NULL)
xmlFree(value);
}
/**
* xsltCopyOf:
* @ctxt: an XSLT transformation context
* @node: the current node in the source tree
* @inst: the element node of the XSLT copy-of instruction
* @castedComp: precomputed information of the XSLT copy-of instruction
*
* Process the XSLT copy-of instruction.
*/
void
xsltCopyOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp) {
#ifdef XSLT_REFACTORED
xsltStyleItemCopyOfPtr comp = (xsltStyleItemCopyOfPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xmlXPathObjectPtr res = NULL;
xmlNodeSetPtr list = NULL;
int i;
if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
return;
if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
xsltTransformError(ctxt, NULL, inst,
"xsl:copy-of : compilation failed\n");
return;
}
/*
* SPEC XSLT 1.0:
* "The xsl:copy-of element can be used to insert a result tree
* fragment into the result tree, without first converting it to
* a string as xsl:value-of does (see [7.6.1 Generating Text with
* xsl:value-of]). The required select attribute contains an
* expression. When the result of evaluating the expression is a
* result tree fragment, the complete fragment is copied into the
* result tree. When the result is a node-set, all the nodes in the
* set are copied in document order into the result tree; copying
* an element node copies the attribute nodes, namespace nodes and
* children of the element node as well as the element node itself;
* a root node is copied by copying its children. When the result
* is neither a node-set nor a result tree fragment, the result is
* converted to a string and then inserted into the result tree,
* as with xsl:value-of.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyOf: select %s\n", comp->select));
#endif
/*
* Evaluate the "select" expression.
*/
res = xsltPreCompEval(ctxt, node, comp);
if (res != NULL) {
if (res->type == XPATH_NODESET) {
/*
* Node-set
* --------
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyOf: result is a node set\n"));
#endif
list = res->nodesetval;
if (list != NULL) {
xmlNodePtr cur;
/*
* The list is already sorted in document order by XPath.
* Append everything in this order under ctxt->insert.
*/
for (i = 0;i < list->nodeNr;i++) {
cur = list->nodeTab[i];
if (cur == NULL)
continue;
if ((cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE))
{
xsltCopyTreeList(ctxt, inst,
cur->children, ctxt->insert, 0, 0);
} else if (cur->type == XML_ATTRIBUTE_NODE) {
xsltShallowCopyAttr(ctxt, inst,
ctxt->insert, (xmlAttrPtr) cur);
} else {
xsltCopyTree(ctxt, inst, cur, ctxt->insert, 0, 0);
}
}
}
} else if (res->type == XPATH_XSLT_TREE) {
/*
* Result tree fragment
* --------------------
* E.g. via <xsl:variable ...><foo/></xsl:variable>
* Note that the root node of such trees is an xmlDocPtr in Libxslt.
*/
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyOf: result is a result tree fragment\n"));
#endif
list = res->nodesetval;
if ((list != NULL) && (list->nodeTab != NULL) &&
(list->nodeTab[0] != NULL) &&
(IS_XSLT_REAL_NODE(list->nodeTab[0])))
{
xsltCopyTreeList(ctxt, inst,
list->nodeTab[0]->children, ctxt->insert, 0, 0);
}
} else {
xmlChar *value = NULL;
/*
* Convert to a string.
*/
value = xmlXPathCastToString(res);
if (value == NULL) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltCopyOf(): "
"failed to cast an XPath object to string.\n");
ctxt->state = XSLT_STATE_STOPPED;
} else {
if (value[0] != 0) {
/*
* Append content as text node.
*/
xsltCopyTextString(ctxt, ctxt->insert, value, 0);
}
xmlFree(value);
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltCopyOf: result %s\n", res->stringval));
#endif
}
}
} else {
ctxt->state = XSLT_STATE_STOPPED;
}
if (res != NULL)
xmlXPathFreeObject(res);
}
/**
* xsltValueOf:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt value-of node
* @castedComp: precomputed information
*
* Process the xslt value-of node on the source node
*/
void
xsltValueOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemValueOfPtr comp = (xsltStyleItemValueOfPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xmlXPathObjectPtr res = NULL;
xmlChar *value = NULL;
if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
return;
if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltValueOf(): "
"The XSLT 'value-of' instruction was not compiled.\n");
return;
}
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltValueOf: select %s\n", comp->select));
#endif
res = xsltPreCompEval(ctxt, node, comp);
/*
* Cast the XPath object to string.
*/
if (res != NULL) {
value = xmlXPathCastToString(res);
if (value == NULL) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltValueOf(): "
"failed to cast an XPath object to string.\n");
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
if (value[0] != 0) {
xsltCopyTextString(ctxt, ctxt->insert, value, comp->noescape);
}
} else {
xsltTransformError(ctxt, NULL, inst,
"XPath evaluation returned no result.\n");
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
#ifdef WITH_XSLT_DEBUG_PROCESS
if (value) {
XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
"xsltValueOf: result '%s'\n", value));
}
#endif
error:
if (value != NULL)
xmlFree(value);
if (res != NULL)
xmlXPathFreeObject(res);
}
/**
* xsltNumber:
* @ctxt: a XSLT process context
* @node: the node in the source tree.
* @inst: the xslt number node
* @castedComp: precomputed information
*
* Process the xslt number node on the source node
*/
void
xsltNumber(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemNumberPtr comp = (xsltStyleItemNumberPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xmlXPathContextPtr xpctxt;
xmlNsPtr *oldXPNamespaces;
int oldXPNsNr;
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:number : compilation failed\n");
return;
}
if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
return;
comp->numdata.doc = inst->doc;
comp->numdata.node = inst;
xpctxt = ctxt->xpathCtxt;
oldXPNsNr = xpctxt->nsNr;
oldXPNamespaces = xpctxt->namespaces;
#ifdef XSLT_REFACTORED
if (comp->inScopeNs != NULL) {
xpctxt->namespaces = comp->inScopeNs->list;
xpctxt->nsNr = comp->inScopeNs->xpathNumber;
} else {
xpctxt->namespaces = NULL;
xpctxt->nsNr = 0;
}
#else
xpctxt->namespaces = comp->nsList;
xpctxt->nsNr = comp->nsNr;
#endif
xsltNumberFormat(ctxt, &comp->numdata, node);
xpctxt->nsNr = oldXPNsNr;
xpctxt->namespaces = oldXPNamespaces;
}
/**
* xsltApplyImports:
* @ctxt: an XSLT transformation context
* @contextNode: the current node in the source tree.
* @inst: the element node of the XSLT 'apply-imports' instruction
* @comp: the compiled instruction
*
* Process the XSLT apply-imports element.
*/
void
xsltApplyImports(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
xmlNodePtr inst,
xsltStylePreCompPtr comp ATTRIBUTE_UNUSED)
{
xsltTemplatePtr templ;
if ((ctxt == NULL) || (inst == NULL))
return;
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltApplyImports(): "
"The XSLT 'apply-imports' instruction was not compiled.\n");
return;
}
/*
* NOTE that ctxt->currentTemplateRule and ctxt->templ is not the
* same; the former is the "Current Template Rule" as defined by the
* XSLT spec, the latter is simply the template struct being
* currently processed.
*/
if (ctxt->currentTemplateRule == NULL) {
/*
* SPEC XSLT 2.0:
* "[ERR XTDE0560] It is a non-recoverable dynamic error if
* xsl:apply-imports or xsl:next-match is evaluated when the
* current template rule is null."
*/
xsltTransformError(ctxt, NULL, inst,
"It is an error to call 'apply-imports' "
"when there's no current template rule.\n");
return;
}
/*
* TODO: Check if this is correct.
*/
templ = xsltGetTemplate(ctxt, contextNode,
ctxt->currentTemplateRule->style);
if (templ != NULL) {
xsltTemplatePtr oldCurTemplRule = ctxt->currentTemplateRule;
/*
* Set the current template rule.
*/
ctxt->currentTemplateRule = templ;
/*
* URGENT TODO: Need xsl:with-param be handled somehow here?
*/
xsltApplyXSLTTemplate(ctxt, contextNode, templ->content,
templ, NULL);
ctxt->currentTemplateRule = oldCurTemplRule;
}
else {
/* Use built-in templates. */
xsltDefaultProcessOneNode(ctxt, contextNode, NULL);
}
}
/**
* xsltCallTemplate:
* @ctxt: a XSLT transformation context
* @node: the "current node" in the source tree
* @inst: the XSLT 'call-template' instruction
* @castedComp: the compiled information of the instruction
*
* Processes the XSLT call-template instruction on the source node.
*/
void
xsltCallTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemCallTemplatePtr comp =
(xsltStyleItemCallTemplatePtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
xsltStackElemPtr withParams = NULL;
if (ctxt->insert == NULL)
return;
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"The XSLT 'call-template' instruction was not compiled.\n");
return;
}
/*
* The template must have been precomputed
*/
if (comp->templ == NULL) {
comp->templ = xsltFindTemplate(ctxt, comp->name, comp->ns);
if (comp->templ == NULL) {
if (comp->ns != NULL) {
xsltTransformError(ctxt, NULL, inst,
"The called template '{%s}%s' was not found.\n",
comp->ns, comp->name);
} else {
xsltTransformError(ctxt, NULL, inst,
"The called template '%s' was not found.\n",
comp->name);
}
return;
}
}
#ifdef WITH_XSLT_DEBUG_PROCESS
if ((comp != NULL) && (comp->name != NULL))
XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"call-template: name %s\n", comp->name));
#endif
if (inst->children) {
xmlNodePtr cur;
xsltStackElemPtr param;
cur = inst->children;
while (cur != NULL) {
#ifdef WITH_DEBUGGER
if (ctxt->debugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(cur, node, comp->templ, ctxt);
#endif
if (ctxt->state == XSLT_STATE_STOPPED) break;
/*
* TODO: The "with-param"s could be part of the "call-template"
* structure. Avoid to "search" for params dynamically
* in the XML tree every time.
*/
if (IS_XSLT_ELEM(cur)) {
if (IS_XSLT_NAME(cur, "with-param")) {
param = xsltParseStylesheetCallerParam(ctxt, cur);
if (param != NULL) {
param->next = withParams;
withParams = param;
}
} else {
xsltGenericError(xsltGenericErrorContext,
"xsl:call-template: misplaced xsl:%s\n", cur->name);
}
} else {
xsltGenericError(xsltGenericErrorContext,
"xsl:call-template: misplaced %s element\n", cur->name);
}
cur = cur->next;
}
}
/*
* Create a new frame using the params first
*/
xsltApplyXSLTTemplate(ctxt, node, comp->templ->content, comp->templ,
withParams);
if (withParams != NULL)
xsltFreeStackElemList(withParams);
#ifdef WITH_XSLT_DEBUG_PROCESS
if ((comp != NULL) && (comp->name != NULL))
XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
"call-template returned: name %s\n", comp->name));
#endif
}
/**
* xsltApplyTemplates:
* @ctxt: a XSLT transformation context
* @node: the 'current node' in the source tree
* @inst: the element node of an XSLT 'apply-templates' instruction
* @castedComp: the compiled instruction
*
* Processes the XSLT 'apply-templates' instruction on the current node.
*/
void
xsltApplyTemplates(xsltTransformContextPtr ctxt, xmlNodePtr node,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemApplyTemplatesPtr comp =
(xsltStyleItemApplyTemplatesPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
int i;
xmlNodePtr cur, delNode = NULL, oldContextNode;
xmlNodeSetPtr list = NULL, oldList;
xsltStackElemPtr withParams = NULL;
int oldXPProximityPosition, oldXPContextSize;
const xmlChar *oldMode, *oldModeURI;
xmlDocPtr oldXPDoc;
xsltDocumentPtr oldDocInfo;
xmlXPathContextPtr xpctxt;
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:apply-templates : compilation failed\n");
return;
}
if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
return;
#ifdef WITH_XSLT_DEBUG_PROCESS
if ((node != NULL) && (node->name != NULL))
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: node: '%s'\n", node->name));
#endif
xpctxt = ctxt->xpathCtxt;
/*
* Save context states.
*/
oldContextNode = ctxt->node;
oldMode = ctxt->mode;
oldModeURI = ctxt->modeURI;
oldDocInfo = ctxt->document;
oldList = ctxt->nodeList;
/*
* The xpath context size and proximity position, as
* well as the xpath and context documents, may be changed
* so we save their initial state and will restore on exit
*/
oldXPContextSize = xpctxt->contextSize;
oldXPProximityPosition = xpctxt->proximityPosition;
oldXPDoc = xpctxt->doc;
/*
* Set up contexts.
*/
ctxt->mode = comp->mode;
ctxt->modeURI = comp->modeURI;
if (comp->select != NULL) {
xmlXPathObjectPtr res = NULL;
if (comp->comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:apply-templates : compilation failed\n");
goto error;
}
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: select %s\n", comp->select));
#endif
res = xsltPreCompEval(ctxt, node, comp);
if (res != NULL) {
if (res->type == XPATH_NODESET) {
list = res->nodesetval; /* consume the node set */
res->nodesetval = NULL;
} else {
xsltTransformError(ctxt, NULL, inst,
"The 'select' expression did not evaluate to a "
"node set.\n");
ctxt->state = XSLT_STATE_STOPPED;
xmlXPathFreeObject(res);
goto error;
}
xmlXPathFreeObject(res);
/*
* Note: An xsl:apply-templates with a 'select' attribute,
* can change the current source doc.
*/
} else {
xsltTransformError(ctxt, NULL, inst,
"Failed to evaluate the 'select' expression.\n");
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
if (list == NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: select didn't evaluate to a node list\n"));
#endif
goto exit;
}
/*
*
* NOTE: Previously a document info (xsltDocument) was
* created and attached to the Result Tree Fragment.
* But such a document info is created on demand in
* xsltKeyFunction() (functions.c), so we need to create
* it here beforehand.
* In order to take care of potential keys we need to
* do some extra work for the case when a Result Tree Fragment
* is converted into a nodeset (e.g. exslt:node-set()) :
* We attach a "pseudo-doc" (xsltDocument) to _private.
* This xsltDocument, together with the keyset, will be freed
* when the Result Tree Fragment is freed.
*
*/
#if 0
if ((ctxt->nbKeys > 0) &&
(list->nodeNr != 0) &&
(list->nodeTab[0]->doc != NULL) &&
XSLT_IS_RES_TREE_FRAG(list->nodeTab[0]->doc))
{
/*
* NOTE that it's also OK if @effectiveDocInfo will be
* set to NULL.
*/
isRTF = 1;
effectiveDocInfo = list->nodeTab[0]->doc->_private;
}
#endif
} else {
/*
* Build an XPath node set with the children
*/
list = xmlXPathNodeSetCreate(NULL);
if (list == NULL)
goto error;
if (node->type != XML_NAMESPACE_DECL)
cur = node->children;
else
cur = NULL;
while (cur != NULL) {
switch (cur->type) {
case XML_TEXT_NODE:
if ((IS_BLANK_NODE(cur)) &&
(cur->parent != NULL) &&
(cur->parent->type == XML_ELEMENT_NODE) &&
(ctxt->style->stripSpaces != NULL)) {
const xmlChar *val;
if (cur->parent->ns != NULL) {
val = (const xmlChar *)
xmlHashLookup2(ctxt->style->stripSpaces,
cur->parent->name,
cur->parent->ns->href);
if (val == NULL) {
val = (const xmlChar *)
xmlHashLookup2(ctxt->style->stripSpaces,
BAD_CAST "*",
cur->parent->ns->href);
}
} else {
val = (const xmlChar *)
xmlHashLookup2(ctxt->style->stripSpaces,
cur->parent->name, NULL);
}
if ((val != NULL) &&
(xmlStrEqual(val, (xmlChar *) "strip"))) {
delNode = cur;
break;
}
}
/* no break on purpose */
case XML_ELEMENT_NODE:
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
xmlXPathNodeSetAddUnique(list, cur);
break;
case XML_DTD_NODE:
/* Unlink the DTD, it's still reachable
* using doc->intSubset */
if (cur->next != NULL)
cur->next->prev = cur->prev;
if (cur->prev != NULL)
cur->prev->next = cur->next;
break;
case XML_NAMESPACE_DECL:
break;
default:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: skipping cur type %d\n",
cur->type));
#endif
delNode = cur;
}
cur = cur->next;
if (delNode != NULL) {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: removing ignorable blank cur\n"));
#endif
xmlUnlinkNode(delNode);
xmlFreeNode(delNode);
delNode = NULL;
}
}
}
#ifdef WITH_XSLT_DEBUG_PROCESS
if (list != NULL)
XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyTemplates: list of %d nodes\n", list->nodeNr));
#endif
if ((list == NULL) || (list->nodeNr == 0))
goto exit;
/*
* Set the context's node set and size; this is also needed for
* for xsltDoSortFunction().
*/
ctxt->nodeList = list;
/*
* Process xsl:with-param and xsl:sort instructions.
* (The code became so verbose just to avoid the
* xmlNodePtr sorts[XSLT_MAX_SORT] if there's no xsl:sort)
* BUG TODO: We are not using namespaced potentially defined on the
* xsl:sort or xsl:with-param elements; XPath expression might fail.
*/
if (inst->children) {
xsltStackElemPtr param;
cur = inst->children;
while (cur) {
#ifdef WITH_DEBUGGER
if (ctxt->debugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(cur, node, NULL, ctxt);
#endif
if (ctxt->state == XSLT_STATE_STOPPED)
break;
if (cur->type == XML_TEXT_NODE) {
cur = cur->next;
continue;
}
if (! IS_XSLT_ELEM(cur))
break;
if (IS_XSLT_NAME(cur, "with-param")) {
param = xsltParseStylesheetCallerParam(ctxt, cur);
if (param != NULL) {
param->next = withParams;
withParams = param;
}
}
if (IS_XSLT_NAME(cur, "sort")) {
xsltTemplatePtr oldCurTempRule =
ctxt->currentTemplateRule;
int nbsorts = 0;
xmlNodePtr sorts[XSLT_MAX_SORT];
sorts[nbsorts++] = cur;
while (cur) {
#ifdef WITH_DEBUGGER
if (ctxt->debugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(cur, node, NULL, ctxt);
#endif
if (ctxt->state == XSLT_STATE_STOPPED)
break;
if (cur->type == XML_TEXT_NODE) {
cur = cur->next;
continue;
}
if (! IS_XSLT_ELEM(cur))
break;
if (IS_XSLT_NAME(cur, "with-param")) {
param = xsltParseStylesheetCallerParam(ctxt, cur);
if (param != NULL) {
param->next = withParams;
withParams = param;
}
}
if (IS_XSLT_NAME(cur, "sort")) {
if (nbsorts >= XSLT_MAX_SORT) {
xsltTransformError(ctxt, NULL, cur,
"The number (%d) of xsl:sort instructions exceeds the "
"maximum allowed by this processor's settings.\n",
nbsorts);
ctxt->state = XSLT_STATE_STOPPED;
break;
} else {
sorts[nbsorts++] = cur;
}
}
cur = cur->next;
}
/*
* The "current template rule" is cleared for xsl:sort.
*/
ctxt->currentTemplateRule = NULL;
/*
* Sort.
*/
xsltDoSortFunction(ctxt, sorts, nbsorts);
ctxt->currentTemplateRule = oldCurTempRule;
break;
}
cur = cur->next;
}
}
xpctxt->contextSize = list->nodeNr;
/*
* Apply templates for all selected source nodes.
*/
for (i = 0; i < list->nodeNr; i++) {
cur = list->nodeTab[i];
/*
* The node becomes the "current node".
*/
ctxt->node = cur;
/*
* An xsl:apply-templates can change the current context doc.
* OPTIMIZE TODO: Get rid of the need to set the context doc.
*/
if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
xpctxt->doc = cur->doc;
xpctxt->proximityPosition = i + 1;
/*
* Find and apply a template for this node.
*/
xsltProcessOneNode(ctxt, cur, withParams);
}
exit:
error:
/*
* Free the parameter list.
*/
if (withParams != NULL)
xsltFreeStackElemList(withParams);
if (list != NULL)
xmlXPathFreeNodeSet(list);
/*
* Restore context states.
*/
xpctxt->doc = oldXPDoc;
xpctxt->contextSize = oldXPContextSize;
xpctxt->proximityPosition = oldXPProximityPosition;
ctxt->document = oldDocInfo;
ctxt->nodeList = oldList;
ctxt->node = oldContextNode;
ctxt->mode = oldMode;
ctxt->modeURI = oldModeURI;
}
/**
* xsltChoose:
* @ctxt: a XSLT process context
* @contextNode: the current node in the source tree
* @inst: the xsl:choose instruction
* @comp: compiled information of the instruction
*
* Processes the xsl:choose instruction on the source node.
*/
void
xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
xmlNodePtr inst, xsltStylePreCompPtr comp ATTRIBUTE_UNUSED)
{
xmlNodePtr cur;
if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
return;
/*
* TODO: Content model checks should be done only at compilation
* time.
*/
cur = inst->children;
if (cur == NULL) {
xsltTransformError(ctxt, NULL, inst,
"xsl:choose: The instruction has no content.\n");
return;
}
#ifdef XSLT_REFACTORED
/*
* We don't check the content model during transformation.
*/
#else
if ((! IS_XSLT_ELEM(cur)) || (! IS_XSLT_NAME(cur, "when"))) {
xsltTransformError(ctxt, NULL, inst,
"xsl:choose: xsl:when expected first\n");
return;
}
#endif
{
int testRes = 0, res = 0;
#ifdef XSLT_REFACTORED
xsltStyleItemWhenPtr wcomp = NULL;
#else
xsltStylePreCompPtr wcomp = NULL;
#endif
/*
* Process xsl:when ---------------------------------------------------
*/
while (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "when")) {
wcomp = cur->psvi;
if ((wcomp == NULL) || (wcomp->test == NULL) ||
(wcomp->comp == NULL))
{
xsltTransformError(ctxt, NULL, cur,
"Internal error in xsltChoose(): "
"The XSLT 'when' instruction was not compiled.\n");
goto error;
}
#ifdef WITH_DEBUGGER
if (xslDebugStatus != XSLT_DEBUG_NONE) {
/*
* TODO: Isn't comp->templ always NULL for xsl:choose?
*/
xslHandleDebugger(cur, contextNode, NULL, ctxt);
}
#endif
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
"xsltChoose: test %s\n", wcomp->test));
#endif
#ifdef XSLT_FAST_IF
res = xsltPreCompEvalToBoolean(ctxt, contextNode, wcomp);
if (res == -1) {
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
testRes = (res == 1) ? 1 : 0;
#else /* XSLT_FAST_IF */
res = xsltPreCompEval(ctxt, cotextNode, wcomp);
if (res != NULL) {
if (res->type != XPATH_BOOLEAN)
res = xmlXPathConvertBoolean(res);
if (res->type == XPATH_BOOLEAN)
testRes = res->boolval;
else {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
"xsltChoose: test didn't evaluate to a boolean\n"));
#endif
goto error;
}
xmlXPathFreeObject(res);
res = NULL;
} else {
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
#endif /* else of XSLT_FAST_IF */
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
"xsltChoose: test evaluate to %d\n", testRes));
#endif
if (testRes)
goto test_is_true;
cur = cur->next;
}
/*
* Process xsl:otherwise ----------------------------------------------
*/
if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "otherwise")) {
#ifdef WITH_DEBUGGER
if (xslDebugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(cur, contextNode, NULL, ctxt);
#endif
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
"evaluating xsl:otherwise\n"));
#endif
goto test_is_true;
}
goto exit;
test_is_true:
goto process_sequence;
}
process_sequence:
/*
* Instantiate the sequence constructor.
*/
xsltApplySequenceConstructor(ctxt, ctxt->node, cur->children,
NULL);
exit:
error:
return;
}
/**
* xsltIf:
* @ctxt: a XSLT process context
* @contextNode: the current node in the source tree
* @inst: the xsl:if instruction
* @castedComp: compiled information of the instruction
*
* Processes the xsl:if instruction on the source node.
*/
void
xsltIf(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
int res = 0;
#ifdef XSLT_REFACTORED
xsltStyleItemIfPtr comp = (xsltStyleItemIfPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
return;
if ((comp == NULL) || (comp->test == NULL) || (comp->comp == NULL)) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltIf(): "
"The XSLT 'if' instruction was not compiled.\n");
return;
}
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
"xsltIf: test %s\n", comp->test));
#endif
#ifdef XSLT_FAST_IF
{
xmlDocPtr oldLocalFragmentTop = ctxt->localRVT;
res = xsltPreCompEvalToBoolean(ctxt, contextNode, comp);
/*
* Cleanup fragments created during evaluation of the
* "select" expression.
*/
if (oldLocalFragmentTop != ctxt->localRVT)
xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
}
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
"xsltIf: test evaluate to %d\n", res));
#endif
if (res == -1) {
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
if (res == 1) {
/*
* Instantiate the sequence constructor of xsl:if.
*/
xsltApplySequenceConstructor(ctxt,
contextNode, inst->children, NULL);
}
#else /* XSLT_FAST_IF */
{
/*
* OLD CODE:
*/
xmlXPathObjectPtr xpobj = xsltPreCompEval(ctxt, contextNode, comp);
if (xpobj != NULL) {
if (xpobj->type != XPATH_BOOLEAN)
xpobj = xmlXPathConvertBoolean(xpobj);
if (xpobj->type == XPATH_BOOLEAN) {
res = xpobj->boolval;
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
"xsltIf: test evaluate to %d\n", res));
#endif
if (res) {
xsltApplySequenceConstructor(ctxt,
contextNode, inst->children, NULL);
}
} else {
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt, XSLT_TRACE_IF,
xsltGenericDebug(xsltGenericDebugContext,
"xsltIf: test didn't evaluate to a boolean\n"));
#endif
ctxt->state = XSLT_STATE_STOPPED;
}
xmlXPathFreeObject(xpobj);
} else {
ctxt->state = XSLT_STATE_STOPPED;
}
}
#endif /* else of XSLT_FAST_IF */
error:
return;
}
/**
* xsltForEach:
* @ctxt: an XSLT transformation context
* @contextNode: the "current node" in the source tree
* @inst: the element node of the xsl:for-each instruction
* @castedComp: the compiled information of the instruction
*
* Process the xslt for-each node on the source node
*/
void
xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
xmlNodePtr inst, xsltStylePreCompPtr castedComp)
{
#ifdef XSLT_REFACTORED
xsltStyleItemForEachPtr comp = (xsltStyleItemForEachPtr) castedComp;
#else
xsltStylePreCompPtr comp = castedComp;
#endif
int i;
xmlXPathObjectPtr res = NULL;
xmlNodePtr cur, curInst;
xmlNodeSetPtr list = NULL;
xmlNodeSetPtr oldList;
int oldXPProximityPosition, oldXPContextSize;
xmlNodePtr oldContextNode;
xsltTemplatePtr oldCurTemplRule;
xmlDocPtr oldXPDoc;
xsltDocumentPtr oldDocInfo;
xmlXPathContextPtr xpctxt;
if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL)) {
xsltGenericError(xsltGenericErrorContext,
"xsltForEach(): Bad arguments.\n");
return;
}
if (comp == NULL) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltForEach(): "
"The XSLT 'for-each' instruction was not compiled.\n");
return;
}
if ((comp->select == NULL) || (comp->comp == NULL)) {
xsltTransformError(ctxt, NULL, inst,
"Internal error in xsltForEach(): "
"The selecting expression of the XSLT 'for-each' "
"instruction was not compiled correctly.\n");
return;
}
xpctxt = ctxt->xpathCtxt;
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
"xsltForEach: select %s\n", comp->select));
#endif
/*
* Save context states.
*/
oldDocInfo = ctxt->document;
oldList = ctxt->nodeList;
oldContextNode = ctxt->node;
/*
* The "current template rule" is cleared for the instantiation of
* xsl:for-each.
*/
oldCurTemplRule = ctxt->currentTemplateRule;
ctxt->currentTemplateRule = NULL;
oldXPDoc = xpctxt->doc;
oldXPProximityPosition = xpctxt->proximityPosition;
oldXPContextSize = xpctxt->contextSize;
/*
* Evaluate the 'select' expression.
*/
res = xsltPreCompEval(ctxt, contextNode, comp);
if (res != NULL) {
if (res->type == XPATH_NODESET)
list = res->nodesetval;
else {
xsltTransformError(ctxt, NULL, inst,
"The 'select' expression does not evaluate to a node set.\n");
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
"xsltForEach: select didn't evaluate to a node list\n"));
#endif
goto error;
}
} else {
xsltTransformError(ctxt, NULL, inst,
"Failed to evaluate the 'select' expression.\n");
ctxt->state = XSLT_STATE_STOPPED;
goto error;
}
if ((list == NULL) || (list->nodeNr <= 0))
goto exit;
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
"xsltForEach: select evaluates to %d nodes\n", list->nodeNr));
#endif
/*
* Set the list; this has to be done already here for xsltDoSortFunction().
*/
ctxt->nodeList = list;
/*
* Handle xsl:sort instructions and skip them for further processing.
* BUG TODO: We are not using namespaced potentially defined on the
* xsl:sort element; XPath expression might fail.
*/
curInst = inst->children;
if (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
int nbsorts = 0;
xmlNodePtr sorts[XSLT_MAX_SORT];
sorts[nbsorts++] = curInst;
#ifdef WITH_DEBUGGER
if (xslDebugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(curInst, contextNode, NULL, ctxt);
#endif
curInst = curInst->next;
while (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
if (nbsorts >= XSLT_MAX_SORT) {
xsltTransformError(ctxt, NULL, curInst,
"The number of xsl:sort instructions exceeds the "
"maximum (%d) allowed by this processor.\n",
XSLT_MAX_SORT);
goto error;
} else {
sorts[nbsorts++] = curInst;
}
#ifdef WITH_DEBUGGER
if (xslDebugStatus != XSLT_DEBUG_NONE)
xslHandleDebugger(curInst, contextNode, NULL, ctxt);
#endif
curInst = curInst->next;
}
xsltDoSortFunction(ctxt, sorts, nbsorts);
}
xpctxt->contextSize = list->nodeNr;
/*
* Instantiate the sequence constructor for each selected node.
*/
for (i = 0; i < list->nodeNr; i++) {
cur = list->nodeTab[i];
/*
* The selected node becomes the "current node".
*/
ctxt->node = cur;
/*
* An xsl:for-each can change the current context doc.
* OPTIMIZE TODO: Get rid of the need to set the context doc.
*/
if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
xpctxt->doc = cur->doc;
xpctxt->proximityPosition = i + 1;
xsltApplySequenceConstructor(ctxt, cur, curInst, NULL);
}
exit:
error:
if (res != NULL)
xmlXPathFreeObject(res);
/*
* Restore old states.
*/
ctxt->document = oldDocInfo;
ctxt->nodeList = oldList;
ctxt->node = oldContextNode;
ctxt->currentTemplateRule = oldCurTemplRule;
xpctxt->doc = oldXPDoc;
xpctxt->contextSize = oldXPContextSize;
xpctxt->proximityPosition = oldXPProximityPosition;
}
/************************************************************************
* *
* Generic interface *
* *
************************************************************************/
#ifdef XSLT_GENERATE_HTML_DOCTYPE
typedef struct xsltHTMLVersion {
const char *version;
const char *public;
const char *system;
} xsltHTMLVersion;
static xsltHTMLVersion xsltHTMLVersions[] = {
{ "5", NULL, "about:legacy-compat" },
{ "4.01frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
"http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd"},
{ "4.01strict", "-//W3C//DTD HTML 4.01//EN",
"http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"},
{ "4.01trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
{ "4.01", "-//W3C//DTD HTML 4.01 Transitional//EN",
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
{ "4.0strict", "-//W3C//DTD HTML 4.01//EN",
"http://www.w3.org/TR/html4/strict.dtd"},
{ "4.0trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
"http://www.w3.org/TR/html4/loose.dtd"},
{ "4.0frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
"http://www.w3.org/TR/html4/frameset.dtd"},
{ "4.0", "-//W3C//DTD HTML 4.01 Transitional//EN",
"http://www.w3.org/TR/html4/loose.dtd"},
{ "3.2", "-//W3C//DTD HTML 3.2//EN", NULL }
};
/**
* xsltGetHTMLIDs:
* @version: the version string
* @publicID: used to return the public ID
* @systemID: used to return the system ID
*
* Returns -1 if not found, 0 otherwise and the system and public
* Identifier for this given verion of HTML
*/
static int
xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
const xmlChar **systemID) {
unsigned int i;
if (version == NULL)
return(-1);
for (i = 0;i < (sizeof(xsltHTMLVersions)/sizeof(xsltHTMLVersions[1]));
i++) {
if (!xmlStrcasecmp(version,
(const xmlChar *) xsltHTMLVersions[i].version)) {
if (publicID != NULL)
*publicID = (const xmlChar *) xsltHTMLVersions[i].public;
if (systemID != NULL)
*systemID = (const xmlChar *) xsltHTMLVersions[i].system;
return(0);
}
}
return(-1);
}
#endif
/**
* xsltApplyStripSpaces:
* @ctxt: a XSLT process context
* @node: the root of the XML tree
*
* Strip the unwanted ignorable spaces from the input tree
*/
void
xsltApplyStripSpaces(xsltTransformContextPtr ctxt, xmlNodePtr node) {
xmlNodePtr current;
#ifdef WITH_XSLT_DEBUG_PROCESS
int nb = 0;
#endif
current = node;
while (current != NULL) {
/*
* Cleanup children empty nodes if asked for
*/
if ((IS_XSLT_REAL_NODE(current)) &&
(current->children != NULL) &&
(xsltFindElemSpaceHandling(ctxt, current))) {
xmlNodePtr delete = NULL, cur = current->children;
while (cur != NULL) {
if (IS_BLANK_NODE(cur))
delete = cur;
cur = cur->next;
if (delete != NULL) {
xmlUnlinkNode(delete);
xmlFreeNode(delete);
delete = NULL;
#ifdef WITH_XSLT_DEBUG_PROCESS
nb++;
#endif
}
}
}
/*
* Skip to next node in document order.
*/
if (node->type == XML_ENTITY_REF_NODE) {
/* process deep in entities */
xsltApplyStripSpaces(ctxt, node->children);
}
if ((current->children != NULL) &&
(current->type != XML_ENTITY_REF_NODE)) {
current = current->children;
} else if (current->next != NULL) {
current = current->next;
} else {
do {
current = current->parent;
if (current == NULL)
break;
if (current == node)
goto done;
if (current->next != NULL) {
current = current->next;
break;
}
} while (current != NULL);
}
}
done:
#ifdef WITH_XSLT_DEBUG_PROCESS
XSLT_TRACE(ctxt,XSLT_TRACE_STRIP_SPACES,xsltGenericDebug(xsltGenericDebugContext,
"xsltApplyStripSpaces: removed %d ignorable blank node\n", nb));
#endif
return;
}
static int
xsltCountKeys(xsltTransformContextPtr ctxt)
{
xsltStylesheetPtr style;
xsltKeyDefPtr keyd;
if (ctxt == NULL)
return(-1);
/*
* Do we have those nastly templates with a key() in the match pattern?
*/
ctxt->hasTemplKeyPatterns = 0;
style = ctxt->style;
while (style != NULL) {
if (style->keyMatch != NULL) {
ctxt->hasTemplKeyPatterns = 1;
break;
}
style = xsltNextImport(style);
}
/*
* Count number of key declarations.
*/
ctxt->nbKeys = 0;
style = ctxt->style;
while (style != NULL) {
keyd = style->keys;
while (keyd) {
ctxt->nbKeys++;
keyd = keyd->next;
}
style = xsltNextImport(style);
}
return(ctxt->nbKeys);
}
/**
* xsltApplyStylesheetInternal:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated array of parameters names/values tuples
* @output: the targetted output
* @profile: profile FILE * output or NULL
* @user: user provided parameter
*
* Apply the stylesheet to the document
* NOTE: This may lead to a non-wellformed output XML wise !
*
* Returns the result document or NULL in case of error
*/
static xmlDocPtr
xsltApplyStylesheetInternal(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params, const char *output,
FILE * profile, xsltTransformContextPtr userCtxt)
{
xmlDocPtr res = NULL;
xsltTransformContextPtr ctxt = NULL;
xmlNodePtr root, node;
const xmlChar *method;
const xmlChar *doctypePublic;
const xmlChar *doctypeSystem;
const xmlChar *version;
const xmlChar *encoding;
xsltStackElemPtr variables;
xsltStackElemPtr vptr;
xsltInitGlobals();
if ((style == NULL) || (doc == NULL))
return (NULL);
if (style->internalized == 0) {
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"Stylesheet was not fully internalized !\n");
#endif
}
if (doc->intSubset != NULL) {
/*
* Avoid hitting the DTD when scanning nodes
* but keep it linked as doc->intSubset
*/
xmlNodePtr cur = (xmlNodePtr) doc->intSubset;
if (cur->next != NULL)
cur->next->prev = cur->prev;
if (cur->prev != NULL)
cur->prev->next = cur->next;
if (doc->children == cur)
doc->children = cur->next;
if (doc->last == cur)
doc->last = cur->prev;
cur->prev = cur->next = NULL;
}
/*
* Check for XPath document order availability
*/
root = xmlDocGetRootElement(doc);
if (root != NULL) {
if (((ptrdiff_t) root->content >= 0) &&
(xslDebugStatus == XSLT_DEBUG_NONE))
xmlXPathOrderDocElems(doc);
}
if (userCtxt != NULL)
ctxt = userCtxt;
else
ctxt = xsltNewTransformContext(style, doc);
if (ctxt == NULL)
return (NULL);
ctxt->initialContextDoc = doc;
ctxt->initialContextNode = (xmlNodePtr) doc;
if (profile != NULL)
ctxt->profile = 1;
if (output != NULL)
ctxt->outputFile = output;
else
ctxt->outputFile = NULL;
/*
* internalize the modes if needed
*/
if (ctxt->dict != NULL) {
if (ctxt->mode != NULL)
ctxt->mode = xmlDictLookup(ctxt->dict, ctxt->mode, -1);
if (ctxt->modeURI != NULL)
ctxt->modeURI = xmlDictLookup(ctxt->dict, ctxt->modeURI, -1);
}
XSLT_GET_IMPORT_PTR(method, style, method)
XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
XSLT_GET_IMPORT_PTR(version, style, version)
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if ((method != NULL) &&
(!xmlStrEqual(method, (const xmlChar *) "xml")))
{
if (xmlStrEqual(method, (const xmlChar *) "html")) {
ctxt->type = XSLT_OUTPUT_HTML;
if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
res = htmlNewDoc(doctypeSystem, doctypePublic);
} else {
if (version == NULL) {
xmlDtdPtr dtd;
res = htmlNewDoc(NULL, NULL);
/*
* Make sure no DTD node is generated in this case
*/
if (res != NULL) {
dtd = xmlGetIntSubset(res);
if (dtd != NULL) {
xmlUnlinkNode((xmlNodePtr) dtd);
xmlFreeDtd(dtd);
}
res->intSubset = NULL;
res->extSubset = NULL;
}
} else {
#ifdef XSLT_GENERATE_HTML_DOCTYPE
xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
#endif
res = htmlNewDoc(doctypeSystem, doctypePublic);
}
}
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
} else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
"xsltApplyStylesheetInternal: unsupported method xhtml, using html\n");
ctxt->type = XSLT_OUTPUT_HTML;
res = htmlNewDoc(doctypeSystem, doctypePublic);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
} else if (xmlStrEqual(method, (const xmlChar *) "text")) {
ctxt->type = XSLT_OUTPUT_TEXT;
res = xmlNewDoc(style->version);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(res->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
} else {
xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
"xsltApplyStylesheetInternal: unsupported method (%s)\n",
method);
goto error;
}
} else {
ctxt->type = XSLT_OUTPUT_XML;
res = xmlNewDoc(style->version);
if (res == NULL)
goto error;
res->dict = ctxt->dict;
xmlDictReference(ctxt->dict);
#ifdef WITH_XSLT_DEBUG
xsltGenericDebug(xsltGenericDebugContext,
"reusing transformation dict for output\n");
#endif
}
res->charset = XML_CHAR_ENCODING_UTF8;
if (encoding != NULL)
res->encoding = xmlStrdup(encoding);
variables = style->variables;
/*
* Start the evaluation, evaluate the params, the stylesheets globals
* and start by processing the top node.
*/
if (xsltNeedElemSpaceHandling(ctxt))
xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
/*
* Evaluate global params and user-provided params.
*/
ctxt->node = (xmlNodePtr) doc;
if (ctxt->globalVars == NULL)
ctxt->globalVars = xmlHashCreate(20);
if (params != NULL) {
xsltEvalUserParams(ctxt, params);
}
/* need to be called before evaluating global variables */
xsltCountKeys(ctxt);
xsltEvalGlobalVariables(ctxt);
/* Clean up any unused RVTs. */
xsltReleaseLocalRVTs(ctxt, NULL);
ctxt->node = (xmlNodePtr) doc;
ctxt->output = res;
ctxt->insert = (xmlNodePtr) res;
ctxt->varsBase = ctxt->varsNr - 1;
ctxt->xpathCtxt->contextSize = 1;
ctxt->xpathCtxt->proximityPosition = 1;
ctxt->xpathCtxt->node = NULL; /* TODO: Set the context node here? */
/*
* Start processing the source tree -----------------------------------
*/
xsltProcessOneNode(ctxt, ctxt->node, NULL);
/*
* Remove all remaining vars from the stack.
*/
xsltLocalVariablePop(ctxt, 0, -2);
xsltShutdownCtxtExts(ctxt);
xsltCleanupTemplates(style); /* TODO: <- style should be read only */
/*
* Now cleanup our variables so stylesheet can be re-used
*
* TODO: this is not needed anymore global variables are copied
* and not evaluated directly anymore, keep this as a check
*/
if (style->variables != variables) {
vptr = style->variables;
while (vptr->next != variables)
vptr = vptr->next;
vptr->next = NULL;
xsltFreeStackElemList(style->variables);
style->variables = variables;
}
vptr = style->variables;
while (vptr != NULL) {
if (vptr->computed) {
if (vptr->value != NULL) {
xmlXPathFreeObject(vptr->value);
vptr->value = NULL;
vptr->computed = 0;
}
}
vptr = vptr->next;
}
#if 0
/*
* code disabled by wmb; awaiting kb's review
* problem is that global variable(s) may contain xpath objects
* from doc associated with RVT, so can't be freed at this point.
* xsltFreeTransformContext includes a call to xsltFreeRVTs, so
* I assume this shouldn't be required at this point.
*/
/*
* Free all remaining tree fragments.
*/
xsltFreeRVTs(ctxt);
#endif
/*
* Do some post processing work depending on the generated output
*/
root = xmlDocGetRootElement(res);
if (root != NULL) {
const xmlChar *doctype = NULL;
/*
* cleanup ids which may have been saved in Elements content ptrs
*/
if (ctxt->nextid != 0) {
xsltCleanupIds(ctxt, root);
}
if ((root->ns != NULL) && (root->ns->prefix != NULL))
doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
if (doctype == NULL)
doctype = root->name;
/*
* Apply the default selection of the method
*/
if ((method == NULL) &&
(root->ns == NULL) &&
(!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
xmlNodePtr tmp;
tmp = res->children;
while ((tmp != NULL) && (tmp != root)) {
if (tmp->type == XML_ELEMENT_NODE)
break;
if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
break;
tmp = tmp->next;
}
if (tmp == root) {
ctxt->type = XSLT_OUTPUT_HTML;
/*
* REVISIT TODO: XML_HTML_DOCUMENT_NODE is set after the
* transformation on the doc, but functions like
*/
res->type = XML_HTML_DOCUMENT_NODE;
if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
res->intSubset = xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
#ifdef XSLT_GENERATE_HTML_DOCTYPE
} else if (version != NULL) {
xsltGetHTMLIDs(version, &doctypePublic,
&doctypeSystem);
if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
res->intSubset =
xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
#endif
}
}
}
if (ctxt->type == XSLT_OUTPUT_XML) {
XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
xmlNodePtr last;
/* Need a small "hack" here to assure DTD comes before
possible comment nodes */
node = res->children;
last = res->last;
res->children = NULL;
res->last = NULL;
res->intSubset = xmlCreateIntSubset(res, doctype,
doctypePublic,
doctypeSystem);
if (res->children != NULL) {
res->children->next = node;
node->prev = res->children;
res->last = last;
} else {
res->children = node;
res->last = last;
}
}
}
}
xmlXPathFreeNodeSet(ctxt->nodeList);
if (profile != NULL) {
xsltSaveProfiling(ctxt, profile);
}
/*
* Be pedantic.
*/
if ((ctxt != NULL) && (ctxt->state != XSLT_STATE_OK)) {
xmlFreeDoc(res);
res = NULL;
}
if ((res != NULL) && (ctxt != NULL) && (output != NULL)) {
int ret;
ret = xsltCheckWrite(ctxt->sec, ctxt, (const xmlChar *) output);
if (ret == 0) {
xsltTransformError(ctxt, NULL, NULL,
"xsltApplyStylesheet: forbidden to save to %s\n",
output);
} else if (ret < 0) {
xsltTransformError(ctxt, NULL, NULL,
"xsltApplyStylesheet: saving to %s may not be possible\n",
output);
}
}
#ifdef XSLT_DEBUG_PROFILE_CACHE
printf("# Cache:\n");
printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
#endif
if ((ctxt != NULL) && (userCtxt == NULL))
xsltFreeTransformContext(ctxt);
return (res);
error:
if (res != NULL)
xmlFreeDoc(res);
#ifdef XSLT_DEBUG_PROFILE_CACHE
printf("# Cache:\n");
printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
#endif
if ((ctxt != NULL) && (userCtxt == NULL))
xsltFreeTransformContext(ctxt);
return (NULL);
}
/**
* xsltApplyStylesheet:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated arry of parameters names/values tuples
*
* Apply the stylesheet to the document
* NOTE: This may lead to a non-wellformed output XML wise !
*
* Returns the result document or NULL in case of error
*/
xmlDocPtr
xsltApplyStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params)
{
return (xsltApplyStylesheetInternal(style, doc, params, NULL, NULL, NULL));
}
/**
* xsltProfileStylesheet:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated arry of parameters names/values tuples
* @output: a FILE * for the profiling output
*
* Apply the stylesheet to the document and dump the profiling to
* the given output.
*
* Returns the result document or NULL in case of error
*/
xmlDocPtr
xsltProfileStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params, FILE * output)
{
xmlDocPtr res;
res = xsltApplyStylesheetInternal(style, doc, params, NULL, output, NULL);
return (res);
}
/**
* xsltApplyStylesheetUser:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated array of parameters names/values tuples
* @output: the targetted output
* @profile: profile FILE * output or NULL
* @userCtxt: user provided transform context
*
* Apply the stylesheet to the document and allow the user to provide
* its own transformation context.
*
* Returns the result document or NULL in case of error
*/
xmlDocPtr
xsltApplyStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params, const char *output,
FILE * profile, xsltTransformContextPtr userCtxt)
{
xmlDocPtr res;
res = xsltApplyStylesheetInternal(style, doc, params, output,
profile, userCtxt);
return (res);
}
/**
* xsltRunStylesheetUser:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated array of parameters names/values tuples
* @output: the URL/filename ot the generated resource if available
* @SAX: a SAX handler for progressive callback output (not implemented yet)
* @IObuf: an output buffer for progressive output (not implemented yet)
* @profile: profile FILE * output or NULL
* @userCtxt: user provided transform context
*
* Apply the stylesheet to the document and generate the output according
* to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
*
* NOTE: This may lead to a non-wellformed output XML wise !
* NOTE: This may also result in multiple files being generated
* NOTE: using IObuf, the result encoding used will be the one used for
* creating the output buffer, use the following macro to read it
* from the stylesheet
* XSLT_GET_IMPORT_PTR(encoding, style, encoding)
* NOTE: using SAX, any encoding specified in the stylesheet will be lost
* since the interface uses only UTF8
*
* Returns the number of by written to the main resource or -1 in case of
* error.
*/
int
xsltRunStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params, const char *output,
xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf,
FILE * profile, xsltTransformContextPtr userCtxt)
{
xmlDocPtr tmp;
int ret;
if ((output == NULL) && (SAX == NULL) && (IObuf == NULL))
return (-1);
if ((SAX != NULL) && (IObuf != NULL))
return (-1);
/* unsupported yet */
if (SAX != NULL) {
XSLT_TODO /* xsltRunStylesheet xmlSAXHandlerPtr SAX */
return (-1);
}
tmp = xsltApplyStylesheetInternal(style, doc, params, output, profile,
userCtxt);
if (tmp == NULL) {
xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
"xsltRunStylesheet : run failed\n");
return (-1);
}
if (IObuf != NULL) {
/* TODO: incomplete, IObuf output not progressive */
ret = xsltSaveResultTo(IObuf, tmp, style);
} else {
ret = xsltSaveResultToFilename(output, tmp, style, 0);
}
xmlFreeDoc(tmp);
return (ret);
}
/**
* xsltRunStylesheet:
* @style: a parsed XSLT stylesheet
* @doc: a parsed XML document
* @params: a NULL terminated array of parameters names/values tuples
* @output: the URL/filename ot the generated resource if available
* @SAX: a SAX handler for progressive callback output (not implemented yet)
* @IObuf: an output buffer for progressive output (not implemented yet)
*
* Apply the stylesheet to the document and generate the output according
* to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
*
* NOTE: This may lead to a non-wellformed output XML wise !
* NOTE: This may also result in multiple files being generated
* NOTE: using IObuf, the result encoding used will be the one used for
* creating the output buffer, use the following macro to read it
* from the stylesheet
* XSLT_GET_IMPORT_PTR(encoding, style, encoding)
* NOTE: using SAX, any encoding specified in the stylesheet will be lost
* since the interface uses only UTF8
*
* Returns the number of bytes written to the main resource or -1 in case of
* error.
*/
int
xsltRunStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
const char **params, const char *output,
xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf)
{
return(xsltRunStylesheetUser(style, doc, params, output, SAX, IObuf,
NULL, NULL));
}
/**
* xsltRegisterAllElement:
* @ctxt: the XPath context
*
* Registers all default XSLT elements in this context
*/
void
xsltRegisterAllElement(xsltTransformContextPtr ctxt)
{
xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-templates",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltApplyTemplates);
xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-imports",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltApplyImports);
xsltRegisterExtElement(ctxt, (const xmlChar *) "call-template",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltCallTemplate);
xsltRegisterExtElement(ctxt, (const xmlChar *) "element",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltElement);
xsltRegisterExtElement(ctxt, (const xmlChar *) "attribute",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltAttribute);
xsltRegisterExtElement(ctxt, (const xmlChar *) "text",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltText);
xsltRegisterExtElement(ctxt, (const xmlChar *) "processing-instruction",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltProcessingInstruction);
xsltRegisterExtElement(ctxt, (const xmlChar *) "comment",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltComment);
xsltRegisterExtElement(ctxt, (const xmlChar *) "copy",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltCopy);
xsltRegisterExtElement(ctxt, (const xmlChar *) "value-of",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltValueOf);
xsltRegisterExtElement(ctxt, (const xmlChar *) "number",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltNumber);
xsltRegisterExtElement(ctxt, (const xmlChar *) "for-each",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltForEach);
xsltRegisterExtElement(ctxt, (const xmlChar *) "if",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltIf);
xsltRegisterExtElement(ctxt, (const xmlChar *) "choose",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltChoose);
xsltRegisterExtElement(ctxt, (const xmlChar *) "sort",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltSort);
xsltRegisterExtElement(ctxt, (const xmlChar *) "copy-of",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltCopyOf);
xsltRegisterExtElement(ctxt, (const xmlChar *) "message",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltMessage);
/*
* Those don't have callable entry points but are registered anyway
*/
xsltRegisterExtElement(ctxt, (const xmlChar *) "variable",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "param",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "with-param",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "decimal-format",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "when",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "otherwise",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
xsltRegisterExtElement(ctxt, (const xmlChar *) "fallback",
XSLT_NAMESPACE,
(xsltTransformFunction) xsltDebug);
}
|