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
|
#!/usr/bin/env python
#
# Copyright (c), 2018-2021, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
#
# Note: Many tests are built using the examples of the XPath standards,
# published by W3C under the W3C Document License.
#
# References:
# http://www.w3.org/TR/1999/REC-xpath-19991116/
# http://www.w3.org/TR/2010/REC-xpath20-20101214/
# http://www.w3.org/TR/2010/REC-xpath-functions-20101214/
# https://www.w3.org/Consortium/Legal/2015/doc-license
# https://www.w3.org/TR/charmod-norm/
#
import unittest
import datetime
import io
import locale
import math
import os
import platform
import time
from textwrap import dedent
from decimal import Decimal
try:
import lxml.etree as lxml_etree
except ImportError:
lxml_etree = None
try:
import xmlschema
except ImportError:
xmlschema = None
else:
xmlschema.XMLSchema.meta_schema.build()
from elementpath import XPath2Parser, XPathContext, ElementPathError, \
MissingContextError, select, Selector, datatypes, get_node_tree, \
NamespaceNode, TextNode
from elementpath.xpath_nodes import TextAttributeNode
from elementpath.namespaces import XSI_NAMESPACE, XML_NAMESPACE, XML_ID
from elementpath.datatypes import DateTime10, DateTime, Date10, Date, Time, \
Timezone, DayTimeDuration, YearMonthDuration, QName, UntypedAtomic
from elementpath.collations import UNICODE_CODEPOINT_COLLATION
try:
from tests import test_xpath1_parser
except ImportError:
import test_xpath1_parser
XML_GENERIC_TEST = test_xpath1_parser.XML_GENERIC_TEST
XML_POEM_TEST = """<poem author="Wilhelm Busch">
Kaum hat dies der Hahn gesehen,
Fängt er auch schon an zu krähen:
«Kikeriki! Kikikerikih!!»
Tak, tak, tak! - da kommen sie.
</poem>"""
try:
from tests import xpath_test_class
except ImportError:
import xpath_test_class
class XPath2FunctionsTest(xpath_test_class.XPathTestCase):
def setUp(self):
self.parser = XPath2Parser(namespaces=self.namespaces)
# Make sure the tests are repeatable.
env_vars_to_tweak = 'LC_ALL', 'LANG'
self.current_env_vars = {v: os.environ.get(v) for v in env_vars_to_tweak}
for v in self.current_env_vars:
os.environ[v] = 'en_US.UTF-8'
def tearDown(self):
if hasattr(self, 'current_env_vars'):
for v in self.current_env_vars:
if self.current_env_vars[v] is not None:
os.environ[v] = self.current_env_vars[v]
def test_boolean_function(self):
root = self.etree.XML('<A><B1/><B2/><B3/></A>')
self.check_selector("boolean(/A)", root, True)
self.check_selector("boolean((-10, 35))", root, TypeError) # Sequence with 2 numeric values
self.check_selector("boolean((/A, 35))", root, True)
def test_abs_function(self):
# Test cases taken from https://www.w3.org/TR/xquery-operators/#numeric-value-functions
self.check_value("abs(10.5)", 10.5)
self.check_value("abs(-10.5)", 10.5)
self.check_value("abs(())")
root = self.etree.XML('<root>-10</root>')
context = XPathContext(root, item=float('nan'))
self.check_value("abs(.)", float('nan'), context=context)
context = XPathContext(root)
self.check_value("abs(.)", 10, context=context)
context = XPathContext(root=self.etree.XML('<root>foo</root>'))
self.wrong_type('abs("10")', 'XPTY0004', 'invalid argument type')
with self.assertRaises(ValueError) as err:
self.check_value("abs(.)", 10, context=context)
self.assertIn('FOCA0002', str(err.exception))
self.assertIn('invalid string value', str(err.exception))
def test_round_half_to_even_function(self):
self.check_value("round-half-to-even(())")
self.check_value("round-half-to-even(0.5)", 0)
self.check_value("round-half-to-even(1)", 1)
self.check_value("round-half-to-even(1.5)", 2)
self.check_value("round-half-to-even(2.5)", 2)
self.check_value("round-half-to-even(xs:float(2.5))", 2)
self.check_value("round-half-to-even(3.567812E+3, 2)", 3567.81E0)
self.check_value("round-half-to-even(4.7564E-3, 2)", 0.0E0)
self.check_value("round-half-to-even(35612.25, -2)", 35600)
self.wrong_type('round-half-to-even(3.5, "2")', 'XPTY0004')
self.check_value('fn:round-half-to-even(xs:double("1.0E300"))', 1.0E300)
self.check_value('fn:round-half-to-even(4.8712122, 8328782878)', 4.8712122)
root = self.etree.XML('<root/>')
context = XPathContext(root, item=float('nan'))
self.check_value("round-half-to-even(.)", float('nan'), context=context)
self.wrong_type('round-half-to-even("wrong")', 'XPTY0004', 'invalid argument type')
def test_sum_function(self):
self.check_value("sum((10, 15, 6, -2))", 29)
def test_avg_function(self):
context = XPathContext(root=self.etree.XML('<A/>'),
variables={
'd1': YearMonthDuration.fromstring("P20Y"),
'd2': YearMonthDuration.fromstring("P10M"),
'seq3': [3, 4, 5]
})
self.check_value("fn:avg($seq3)", 4.0, context=context)
self.check_value("fn:avg(($d1, $d2))", YearMonthDuration.fromstring("P125M"),
context=context)
root_token = self.parser.parse("fn:avg(($d1, $seq3))")
self.assertRaises(TypeError, root_token.evaluate, context=context)
self.check_value("fn:avg(())")
self.wrong_type("fn:avg('10')", 'FORG0006')
self.check_value("fn:avg($seq3)", 4.0, context=context)
self.check_value('avg((xs:float(1), xs:untypedAtomic(2), xs:integer(0)))', 1)
self.check_value('avg((1.0, 2.0, 3.0))', 2)
self.wrong_type('avg((xs:float(1), true(), xs:integer(0)))', 'FORG0006')
self.wrong_type('avg((xs:untypedAtomic(3), xs:integer(3), "three"))',
'FORG0006', 'unsupported operand')
root_token = self.parser.parse("fn:avg((xs:float('INF'), xs:float('-INF')))")
self.assertTrue(math.isnan(root_token.evaluate(context)))
root_token = self.parser.parse("fn:avg(($seq3, xs:float('NaN')))")
self.assertTrue(math.isnan(root_token.evaluate(context)))
root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('avg(/a/b/number(text()))', root, 5)
def test_max_function(self):
self.check_value("fn:max(())", [])
self.check_value("fn:max((3,4,5))", 5)
self.check_value("fn:max((3, 4, xs:float('NaN')))", float('nan'))
self.check_value("fn:max((3,4,5), 'en_US.UTF-8')", 5)
self.check_value("fn:max((5, 5.0e0))", 5.0e0)
self.check_value("fn:max((xs:float(1.0E0), xs:double(15.0)))", 15.0)
self.wrong_type("fn:max((3,4,'Zero'))")
dt = datetime.datetime.now()
self.check_value('fn:max((fn:current-date(), xs:date("2001-01-01")))',
Date(dt.year, dt.month, dt.day, tzinfo=dt.tzinfo))
self.check_value('fn:max(("a", "b", "c"))', 'c')
root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('max(/a/b/number(text()))', root, 9)
self.check_selector('max(/a/b)', root, 9)
self.check_value(
'max((xs:anyURI("http://xpath.test/ns0"), xs:anyURI("http://xpath.test/ns1")))',
datatypes.AnyURI("http://xpath.test/ns1")
)
self.check_value('max((xs:dayTimeDuration("P1D"), xs:dayTimeDuration("P2D")))',
datatypes.DayTimeDuration(seconds=3600 * 48))
self.wrong_type('max(QName("http://xpath.test/ns", "foo"))',
'FORG0006', 'xs:QName is not an ordered type')
self.wrong_type('max(xs:duration("P1Y"))', 'FORG0006',
'xs:duration is not an ordered type')
def test_min_function(self):
self.check_value("fn:min(())", [])
self.check_value("fn:min((3,4,5))", 3)
self.check_value("fn:min((3, 4, xs:float('NaN')))", float('nan'))
self.check_value("fn:min((5, 5.0e0))", 5.0e0)
self.check_value("fn:min((xs:float(0.0E0), xs:float(-0.0E0)))", 0.0)
self.check_value("fn:min((xs:float(1.0E0), xs:double(15.0)))", 1.0)
self.check_value('fn:min((fn:current-date(), xs:date("2001-01-01")))',
Date.fromstring("2001-01-01"))
self.check_value('fn:min(("a", "b", "c"))', 'a')
root = self.etree.XML('<a><b>1</b><b>9</b></a>')
self.check_selector('min(/a/b/number(text()))', root, 1)
self.check_selector('min(/a/b)', root, 1)
self.check_value(
'min((xs:anyURI("http://xpath.test/ns0"), xs:anyURI("http://xpath.test/ns1")))',
datatypes.AnyURI("http://xpath.test/ns0")
)
self.check_value('min((xs:dayTimeDuration("P1D"), xs:dayTimeDuration("P2D")))',
datatypes.DayTimeDuration(seconds=3600 * 24))
self.wrong_type('min(QName("http://xpath.test/ns", "foo"))', 'FORG0006')
self.wrong_type('min(xs:duration("P1Y"))', 'FORG0006')
###
# Functions on strings
def test_codepoints_to_string_function(self):
self.check_value("codepoints-to-string((2309, 2358, 2378, 2325))", 'अशॊक')
self.check_value("codepoints-to-string(2309)", 'अ')
self.wrong_value("codepoints-to-string((55296))", 'FOCH0001')
self.wrong_type("codepoints-to-string(('z'))", 'XPTY0004')
self.wrong_type("codepoints-to-string((2309.1))", 'FORG0006')
def test_string_to_codepoints_function(self):
self.check_value('string-to-codepoints("Thérèse")', [84, 104, 233, 114, 232, 115, 101])
self.check_value('string-to-codepoints(())')
self.wrong_type('string-to-codepoints(84)', 'XPTY0004')
self.check_value('string-to-codepoints(("Thérèse"))', [84, 104, 233, 114, 232, 115, 101])
self.wrong_type('string-to-codepoints(("Thér", "èse"))', 'XPTY0004')
def test_codepoint_equal_function(self):
self.check_value("fn:codepoint-equal('abc', 'abc')", True)
self.check_value("fn:codepoint-equal('abc', 'abcd')", False)
self.check_value("fn:codepoint-equal('', '')", True)
self.check_value("fn:codepoint-equal((), 'abc')")
self.check_value("fn:codepoint-equal('abc', ())")
self.check_value("fn:codepoint-equal((), ())")
def test_compare_function(self):
env_locale_setting = locale.getlocale(locale.LC_COLLATE)
locale.setlocale(locale.LC_COLLATE, 'C')
try:
self.assertEqual(locale.getlocale(locale.LC_COLLATE), (None, None))
self.check_value("fn:compare('abc', 'abc')", 0)
self.check_value("fn:compare('abc', 'abd')", -1)
self.check_value("fn:compare('abc', 'abb')", 1)
self.check_value("fn:compare('foo bar', 'foo bar')", 0)
self.check_value("fn:compare('', '')", 0)
self.check_value("fn:compare('abc', 'abcd')", -1)
self.check_value("fn:compare('', ' foo bar')", -1)
self.check_value("fn:compare('abcd', 'abc')", 1)
self.check_value("fn:compare('foo bar', '')", 1)
self.check_value('fn:compare("a","A")', 1)
self.check_value('fn:compare("A","a")', -1)
self.check_value('fn:compare("+++","++")', 1)
self.check_value('fn:compare("1234","123")', 1)
self.check_value("fn:count(fn:compare((), ''))", 0)
self.check_value("fn:count(fn:compare('abc', ()))", 0)
self.check_value("compare(xs:anyURI('http://example.com/'), 'http://example.com/')", 0)
self.check_value(
"compare(xs:untypedAtomic('http://example.com/'), 'http://example.com/')", 0
)
self.check_value('compare("𐀁", "𐀂", '
'"http://www.w3.org/2005/xpath-functions/collation/codepoint")', -1)
self.check_value('compare("𐀁", "￰", '
'"http://www.w3.org/2005/xpath-functions/collation/codepoint")', 1)
# Issue #17
self.check_value("fn:compare('Strassen', 'Straße')", -1)
if platform.system() != 'Linux':
return
locale.setlocale(locale.LC_COLLATE, 'en_US.UTF-8')
self.check_value("fn:compare('Strasse', 'Straße')", -1)
self.check_value("fn:compare('Strassen', 'Straße')", -1)
try:
self.check_value("fn:compare('Strasse', 'Straße', "
"'http://www.w3.org/2013/collation/UCA?lang=it_IT.UTF-8')", -1)
self.check_value("fn:compare('Strasse', 'Straße', 'it_IT.UTF-8')", -1)
except locale.Error:
pass # Skip test if 'it_IT.UTF-8' is an unknown locale setting
try:
self.check_value("fn:compare('Strasse', 'Straße', 'de_DE.UTF-8')", -1)
except locale.Error:
pass # Skip test if 'de_DE.UTF-8' is an unknown locale setting
try:
self.check_value("fn:compare('Strasse', 'Straße', 'deutsch')", -1)
except locale.Error:
pass # Skip test if 'deutsch' is an unknown locale setting
with self.assertRaises(locale.Error) as cm:
self.check_value("fn:compare('Strasse', 'Straße', 'invalid_collation')")
self.assertIn('FOCH0002', str(cm.exception))
self.wrong_type("fn:compare('Strasse', 111)", 'XPTY0004')
self.wrong_type('fn:compare("1234", 1234)', 'XPTY0004')
finally:
locale.setlocale(locale.LC_COLLATE, env_locale_setting)
def test_normalize_unicode_function(self):
self.check_value('fn:normalize-unicode(())', '')
self.check_value('fn:normalize-unicode("menù")', 'menù')
self.wrong_type('fn:normalize-unicode(xs:hexBinary("84"))', 'XPTY0004')
self.assertRaises(ValueError, self.parser.parse,
'fn:normalize-unicode("Ã ", "FULLY-NORMALIZED")')
self.check_value('fn:normalize-unicode("Ã ", "")', 'Ã ')
self.wrong_value('fn:normalize-unicode("Ã ", "UNKNOWN")')
self.wrong_type('fn:normalize-unicode("Ã ", ())', 'XPTY0004', "can't be an empty sequence")
# https://www.w3.org/TR/charmod-norm/#normalization_forms
self.check_value("fn:normalize-unicode('\u01FA')", '\u01FA')
self.check_value("fn:normalize-unicode('\u01FA', 'NFD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u01FA', 'NFKC')", '\u01FA')
self.check_value("fn:normalize-unicode('\u01FA', 'NFKD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u00C5\u0301')", '\u01FA')
self.check_value("fn:normalize-unicode('\u00C5\u0301', 'NFD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u00C5\u0301', 'NFKC')", '\u01FA')
self.check_value("fn:normalize-unicode('\u00C5\u0301', ' nfkd ')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u212B\u0301')", '\u01FA')
self.check_value("fn:normalize-unicode('\u212B\u0301', 'NFD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u212B\u0301', 'NFKC')", '\u01FA')
self.check_value("fn:normalize-unicode('\u212B\u0301', 'NFKD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u0041\u030A\u0301')", '\u01FA')
self.check_value("fn:normalize-unicode('\u0041\u030A\u0301', 'NFD')",
'\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\u0041\u030A\u0301', 'NFKC')", '\u01FA')
self.check_value("fn:normalize-unicode('\u0041\u030A\u0301', 'NFKD')", '\u0041\u030A\u0301')
self.check_value("fn:normalize-unicode('\uFF21\u030A\u0301')", '\uFF21\u030A\u0301')
self.check_value("fn:normalize-unicode('\uFF21\u030A\u0301', 'NFD')",
'\uFF21\u030A\u0301')
self.check_value("fn:normalize-unicode('\uFF21\u030A\u0301', 'NFKC')", '\u01FA')
self.check_value("fn:normalize-unicode('\uFF21\u030A\u0301', 'NFKD')",
'\u0041\u030A\u0301')
def test_count_function(self):
self.check_value("fn:count('')", 1)
self.check_value("count('')", 1)
self.check_value("fn:count('abc')", 1)
self.check_value("fn:count(7)", 1)
self.check_value("fn:count(())", 0)
self.check_value("fn:count((1, 2, 3))", 3)
self.check_value("fn:count((1, 2, ()))", 2)
self.check_value("fn:count((((()))))", 0)
self.check_value("fn:count((((), (), ()), (), (), (), ()))", 0)
self.check_value('fn:count((1, 2 to ()))', 1)
self.check_value("count(('1', (2, ())))", 2)
self.check_value("count(('1', (2, '3')))", 3)
self.check_value("count(1 to 5)", 5)
self.check_value("count(reverse((1, 2, 3, 4)))", 4)
root = self.etree.XML('<root/>')
self.check_selector("count(5)", root, 1)
self.check_value("count((0, 1, 2 + 1, 3 - 1))", 4)
self.check_value('fn:count((xs:decimal("-999999999999999999")))', 1)
self.check_value('fn:count((xs:float("0")))', 1)
self.check_value("count(//*[@name='John Doe'])", MissingContextError)
context = XPathContext(self.etree.XML('<root/>'))
self.check_value("count(//*[@name='John Doe'])", 0, context)
with self.assertRaises(TypeError) as cm:
self.check_value("fn:count()")
self.assertIn('XPST0017', str(cm.exception))
with self.assertRaises(TypeError) as cm:
self.check_value("fn:count(1, ())")
self.assertIn('XPST0017', str(cm.exception))
with self.assertRaises(TypeError) as cm:
self.check_value("fn:count(1, 2)")
self.assertIn('XPST0017', str(cm.exception))
def test_lower_case_function(self):
self.check_value('lower-case("aBcDe01")', 'abcde01')
self.check_value('lower-case(("aBcDe01"))', 'abcde01')
self.check_value('lower-case(())', '')
self.wrong_type('lower-case((10))')
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[lower-case(@id) = 'a_id']", root, [root[0]])
self.check_selector("a[lower-case(@id) = 'a_i']", root, [])
self.check_selector("//b[lower-case(.) = 'some content']", root, [root[0][0]])
self.check_selector("//b[lower-case((.)) = 'some content']", root, [root[0][0]])
self.check_selector("//none[lower-case((.)) = 'some content']", root, [])
def test_upper_case_function(self):
self.check_value('upper-case("aBcDe01")', 'ABCDE01')
self.check_value('upper-case(("aBcDe01"))', 'ABCDE01')
self.check_value('upper-case(())', '')
self.wrong_type('upper-case((10))', 'XPTY0004')
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[upper-case(@id) = 'A_ID']", root, [root[0]])
self.check_selector("a[upper-case(@id) = 'A_I']", root, [])
self.check_selector("//b[upper-case(.) = 'SOME CONTENT']", root, [root[0][0]])
self.check_selector("//b[upper-case((.)) = 'SOME CONTENT']", root, [root[0][0]])
self.check_selector("//none[upper-case((.)) = 'SOME CONTENT']", root, [])
def test_encode_for_uri_function(self):
self.check_value('encode-for-uri("http://xpath.test")', 'http%3A%2F%2Fxpath.test')
self.check_value('encode-for-uri("~bébé")', '~b%C3%A9b%C3%A9')
self.check_value('encode-for-uri("100% organic")', '100%25%20organic')
self.check_value('encode-for-uri("")', '')
self.check_value('encode-for-uri(())', '')
def test_iri_to_uri_function(self):
self.check_value('iri-to-uri("http://www.example.com/00/Weather/CA/Los%20Angeles#ocean")',
'http://www.example.com/00/Weather/CA/Los%20Angeles#ocean')
self.check_value('iri-to-uri("http://www.example.com/~bébé")',
'http://www.example.com/~b%C3%A9b%C3%A9')
self.check_value('iri-to-uri("")', '')
self.check_value('iri-to-uri(())', '')
def test_escape_html_uri_function(self):
self.check_value(
'escape-html-uri("http://www.example.com/00/Weather/CA/Los Angeles#ocean")',
'http://www.example.com/00/Weather/CA/Los Angeles#ocean'
)
self.check_value("escape-html-uri(\"javascript:if (navigator.browserLanguage == 'fr') "
"window.open('http://www.example.com/~bébé');\")",
"javascript:if (navigator.browserLanguage == 'fr') "
"window.open('http://www.example.com/~b%C3%A9b%C3%A9');")
self.check_value('escape-html-uri("")', '')
self.check_value('escape-html-uri(())', '')
def test_string_join_function(self):
self.check_value("string-join(('Now', 'is', 'the', 'time', '...'), ' ')",
"Now is the time ...")
self.check_value("string-join(('Blow, ', 'blow, ', 'thou ', 'winter ', 'wind!'), '')",
'Blow, blow, thou winter wind!')
self.check_value("string-join((), 'separator')", '')
self.check_value("string-join(('a', 'b', 'c'), ', ')", 'a, b, c')
self.wrong_type("string-join(('a', 'b', 'c'), 8)", 'XPTY0004')
if self.parser.version < '3.1':
self.wrong_type("string-join(('a', 4, 'c'), ', ')", 'XPTY0004')
else:
self.check_value("string-join(('a', 4, 'c'), ', ')", 'a, 4, c')
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[string-join((@id, 'foo', 'bar'), ' ') = 'a_id foo bar']",
root, [root[0]])
self.check_selector("a[string-join((@id, 'foo'), ',') = 'a_id,foo']",
root, [root[0]])
self.check_selector("//b[string-join((., 'bar'), ' ') = 'some content bar']",
root, [root[0][0]])
self.check_selector("//b[string-join((., 'bar'), ',') = 'some content,bar']",
root, [root[0][0]])
self.check_selector("//b[string-join((., 'bar'), ',') = 'some content bar']", root, [])
self.check_selector("//none[string-join((., 'bar'), ',') = 'some content,bar']", root, [])
def test_matches_function(self):
self.check_value('fn:matches("abracadabra", "bra")', True)
self.check_value('fn:matches("abracadabra", "^a.*a$")', True)
self.check_value('fn:matches("abracadabra", "^bra")', False)
self.wrong_value('fn:matches("abracadabra", "bra", "k")')
self.wrong_value('fn:matches("abracadabra", "[bra")')
self.wrong_value('fn:matches("abracadabra", "a{1,99999999999999999999999999}")',
'FORX0002')
self.check_value('fn:matches("1", "\\S")', True)
self.check_value('fn:matches(" ", "\\S")', False)
self.check_value('fn:matches("", "\\S")', False)
self.check_value('fn:matches("\t", "\\S")', False)
self.check_value('fn:matches(" foo bar", "\\S")', True)
if platform.python_implementation() != 'PyPy' or self.etree is not lxml_etree:
poem_context = XPathContext(root=self.etree.XML(XML_POEM_TEST))
self.check_value('fn:matches(., "Kaum.*krähen")', False, context=poem_context)
self.check_value('fn:matches(., "Kaum.*krähen", "s")', True, context=poem_context)
self.check_value('fn:matches(., "^Kaum.*gesehen,$", "m")', True, context=poem_context)
self.check_value('fn:matches(., "^Kaum.*gesehen,$")', False, context=poem_context)
self.check_value('fn:matches(., "kiki", "i")', True, context=poem_context)
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[matches(@id, '^a_id$')]", root, [root[0]])
self.check_selector("a[matches(@id, 'a.id')]", root, [root[0]])
self.check_selector("a[matches(@id, '_id')]", root, [root[0]])
self.check_selector("a[matches(@id, 'a!')]", root, [])
self.check_selector("//b[matches(., '^some.content$')]", root, [root[0][0]])
self.check_selector("//b[matches(., '^content')]", root, [])
self.check_selector("//none[matches(., '.*')]", root, [])
def test_ends_with_function(self):
self.check_value('fn:ends-with("abracadabra", "bra")', True)
self.check_value('fn:ends-with("abracadabra", "a")', True)
self.check_value('fn:ends-with("abracadabra", "cbra")', False)
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[ends-with(@id, 'a_id')]", root, [root[0]])
self.check_selector("a[ends-with(@id, 'id')]", root, [root[0]])
self.check_selector("a[ends-with(@id, 'a!')]", root, [])
self.check_selector("//b[ends-with(., 'some content')]", root, [root[0][0]])
self.check_selector("//b[ends-with(., 't')]", root, [root[0][0]])
self.check_selector("//none[ends-with(., 's')]", root, [])
self.check_value('fn:ends-with ( "tattoo", "tattoo", "http://www.w3.org/'
'2005/xpath-functions/collation/codepoint")', True)
self.check_value('fn:ends-with ( "tattoo", "atto", "http://www.w3.org/'
'2005/xpath-functions/collation/codepoint")', False)
self.check_value("ends-with((), ())", True)
def test_replace_function(self):
self.check_value('fn:replace("abracadabra", "bra", "*")', "a*cada*")
self.check_value('fn:replace("abracadabra", "a.*a", "*")', "*")
self.check_value('fn:replace("abracadabra", "a.*?a", "*")', "*c*bra")
self.check_value('fn:replace("abracadabra", "a", "")', "brcdbr")
self.check_value('fn:replace("abracadabra", "a", "", "i")', "brcdbr")
self.wrong_value('fn:replace("abracadabra", "a", "", "z")')
self.wrong_value('fn:replace("abracadabra", "[a", "")')
self.wrong_type('fn:replace("abracadabra")')
self.check_value('fn:replace("abracadabra", "a(.)", "a$1$1")', "abbraccaddabbra")
self.wrong_value('replace("abc", "a(.)", "$x")', 'FORX0004', 'Invalid replacement string')
self.wrong_value('fn:replace("abracadabra", ".*?", "$1")')
self.check_value('fn:replace("AAAA", "A+", "b")', "b")
self.check_value('fn:replace("AAAA", "A+?", "b")', "bbbb")
self.check_value('fn:replace("darted", "^(.*?)d(.*)$", "$1c$2")', "carted")
self.check_value('fn:replace("abcd", "(ab)|(a)", "[1=$1][2=$2]")', "[1=ab][2=]cd")
root = self.etree.XML(XML_GENERIC_TEST)
self.check_selector("a[replace(@id, '^a_id$', 'foo') = 'foo']", root, [root[0]])
self.check_selector("a[replace(@id, 'a.id', 'foo') = 'foo']", root, [root[0]])
self.check_selector("a[replace(@id, '_id', 'no') = 'ano']", root, [root[0]])
self.check_selector("//b[replace(., '^some.content$', 'new') = 'new']", root, [root[0][0]])
self.check_selector("//b[replace(., '^content', '') = '']", root, [])
self.check_selector("//none[replace(., '.*', 'a') = 'a']", root, [])
def test_tokenize_function(self):
self.check_value('fn:tokenize("abracadabra", "(ab)|(a)")', ['', 'r', 'c', 'd', 'r', ''])
self.check_value(r'fn:tokenize("The cat sat on the mat", "\s+")',
['The', 'cat', 'sat', 'on', 'the', 'mat'])
self.check_value(r'fn:tokenize("1, 15, 24, 50", ",\s*")', ['1', '15', '24', '50'])
self.check_value('fn:tokenize("1,15,,24,50,", ",")', ['1', '15', '', '24', '50', ''])
self.check_value(r'fn:tokenize("Some unparsed <br> HTML <BR> text", "\s*<br>\s*", "i")',
['Some unparsed', 'HTML', 'text'])
self.check_value('fn:tokenize("", "(ab)|(a)")', [])
self.wrong_value('fn:tokenize("abc", "[a")', 'FORX0002', 'Invalid regular expression')
self.wrong_value('fn:tokenize("abc", ".*?")', 'FORX0003', 'matches zero-length string')
self.wrong_value('fn:tokenize("abba", ".?")')
self.wrong_value('fn:tokenize("abracadabra", "(ab)|(a)", "sxf")')
self.wrong_type('fn:tokenize("abracadabra", ())')
self.wrong_type('fn:tokenize("abracadabra", "(ab)|(a)", ())')
def test_resolve_uri_function(self):
self.check_value('fn:resolve-uri("dir1/dir2", "file:///home/")', 'file:///home/dir1/dir2')
self.wrong_value('fn:resolve-uri("dir1/dir2", "home/")', '')
self.wrong_value('fn:resolve-uri("dir1/dir2")')
self.check_value('fn:resolve-uri((), "http://xpath.test")')
self.wrong_value('fn:resolve-uri("file:://file1.txt", "http://xpath.test")',
'FORG0002', "'file:://file1.txt' is not a valid URI")
self.wrong_value('fn:resolve-uri("dir1/dir2", "http:://xpath.test")',
'FORG0002', "'http:://xpath.test' is not a valid URI")
self.parser.base_uri = 'http://www.example.com/ns/'
try:
self.check_value('fn:resolve-uri("dir1/dir2")', 'http://www.example.com/ns/dir1/dir2')
self.check_value('fn:resolve-uri("/dir1/dir2")', '/dir1/dir2')
self.check_value('fn:resolve-uri("file:text.txt")', 'file:text.txt')
self.check_value('fn:resolve-uri(())')
self.wrong_value('fn:resolve-uri("http:://xpath.test")',
'FORG0002', "'http:://xpath.test' is not a valid URI")
finally:
self.parser.base_uri = None
def test_empty_function(self):
# Test cases from https://www.w3.org/TR/xquery-operators/#general-seq-funcs
self.check_value('fn:empty(("hello", "world"))', False)
self.check_value('fn:empty(fn:remove(("hello", "world"), 1))', False)
self.check_value('fn:empty(())', True)
self.check_value("empty(() * ())", True)
self.check_value('fn:empty(fn:remove(("hello"), 1))', True)
self.check_value('fn:empty((xs:double("0")))', False)
def test_exists_function(self):
self.check_value('fn:exists(("hello", "world"))', True)
self.check_value('fn:exists(())', False)
self.check_value('fn:exists(fn:remove(("hello"), 1))', False)
self.check_value('fn:exists((xs:int("-1873914410")))', True)
def test_distinct_values_function(self):
self.check_value('fn:distinct-values((1, 2.0, 3, 2))', [1, 2.0, 3])
context = XPathContext(
root=self.etree.XML('<root/>'),
variables={
'x': [UntypedAtomic("foo"), UntypedAtomic("bar"), UntypedAtomic("bar")]
}
)
self.check_value('fn:distinct-values($x)', ['foo', 'bar'], context)
context = XPathContext(
root=self.etree.XML('<root/>'),
variables={'x': [UntypedAtomic("foo"), float('nan'), UntypedAtomic("bar")]}
)
token = self.parser.parse('fn:distinct-values($x)')
results = token.evaluate(context)
self.assertEqual(results[0], 'foo')
self.assertTrue(math.isnan(results[1]))
self.assertEqual(results[2], 'bar')
root = self.etree.XML('<root/>')
self.check_selector(
"fn:distinct-values((xs:float('NaN'), xs:double('NaN'), xs:float('NaN')))",
root, math.isnan
)
self.check_value('fn:distinct-values((xs:float("0"), xs:float("0")))', [0.0])
self.check_value(
'fn:distinct-values("foo", "{}")'.format(UNICODE_CODEPOINT_COLLATION), ['foo']
)
def test_index_of_function(self):
self.check_value('fn:index-of ((10, 20, 30, 40), 35)', [])
self.wrong_type('fn:index-of ((10, 20, 30, 40), ())', 'XPTY0004')
self.check_value('fn:index-of ((10, 20, 30, 30, 20, 10), 20)', [2, 5])
self.check_value('fn:index-of (("a", "sport", "and", "a", "pastime"), "a")', [1, 4])
self.check_value(
'fn:index-of (("foo", "bar"), "bar", "{}")'.format(UNICODE_CODEPOINT_COLLATION), [2]
)
# Issue #28
root = self.etree.XML("""<root>
<incode>030</incode>
<descript></descript>
</root>""")
test1 = "/root/descript[index-of(('030','031'), '030')]"
test2 = "/root/descript[ancestor::root/incode = '030']"
test3 = "/root/descript[index-of(('030','031'), ancestor::root/incode)]"
self.check_selector(test1, root, [root[1]])
self.check_selector(test2, root, [root[1]])
self.check_selector(test3, root, [root[1]])
def test_insert_before_function(self):
context = XPathContext(root=self.etree.XML('<root/>'),
variables={'x': ['a', 'b', 'c']})
self.check_value('fn:insert-before($x, 0, "z")', ['z', 'a', 'b', 'c'], context)
self.check_value('fn:insert-before($x, 1, "z")', ['z', 'a', 'b', 'c'], context)
self.check_value('fn:insert-before($x, 2, "z")', ['a', 'z', 'b', 'c'], context)
self.check_value('fn:insert-before($x, 3, "z")', ['a', 'b', 'z', 'c'], context)
self.check_value('fn:insert-before($x, 4, "z")', ['a', 'b', 'c', 'z'], context)
self.wrong_type('fn:insert-before($x, "1", "z")', 'XPTY0004', context=context)
def test_remove_function(self):
context = XPathContext(root=self.etree.XML('<root/>'),
variables={'x': ['a', 'b', 'c']})
self.check_value('fn:remove($x, 0)', ['a', 'b', 'c'], context)
self.check_value('fn:remove($x, 1)', ['b', 'c'], context)
self.check_value('remove($x, 6)', ['a', 'b', 'c'], context)
self.wrong_type('remove($x, "6")', 'XPTY0004', context=context)
self.check_value('fn:remove((), 3)', [])
def test_reverse_function(self):
context = XPathContext(root=self.etree.XML('<root/>'),
variables={'x': ['a', 'b', 'c']})
self.check_value('reverse($x)', ['c', 'b', 'a'], context)
self.check_value('fn:reverse(("hello"))', ['hello'], context)
self.check_value('fn:reverse(())', [])
def test_subsequence_function(self):
self.check_value('fn:subsequence((), 5)', [])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 1)', [1, 2, 3, 4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 0)', [1, 2, 3, 4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), -1)', [1, 2, 3, 4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 10)', [])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 4)', [4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 4, 2)', [4, 5])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 3, 10)', [3, 4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), xs:float("INF"))', [])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), xs:float("-INF"))',
[1, 2, 3, 4, 5, 6, 7])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 5, xs:float("-INF"))', [])
self.check_value('fn:subsequence((1, 2, 3, 4, 5, 6, 7), 5, xs:float("INF"))', [5, 6, 7])
def test_unordered_function(self):
self.check_value('fn:unordered(())', [])
self.check_value('fn:unordered(("z", 2, "3", "Z", "b", "a"))', [2, '3', 'Z', 'a', 'b', 'z'])
def test_sequence_cardinality_functions(self):
self.check_value('fn:zero-or-one(())', [])
self.check_value('fn:zero-or-one((10))', [10])
self.wrong_value('fn:zero-or-one((10, 20))')
self.wrong_value('fn:one-or-more(())')
self.check_value('fn:one-or-more((10))', [10])
self.check_value('fn:one-or-more((10, 20, 30, 40))', [10, 20, 30, 40])
self.check_value('fn:exactly-one((20))', [20])
self.wrong_value('fn:exactly-one(())')
self.wrong_value('fn:exactly-one((10, 20, 30, 40))')
def test_qname_function(self):
self.check_value('fn:string(fn:QName("", "person"))', 'person')
self.check_value('fn:string(fn:QName((), "person"))', 'person')
self.check_value('fn:string(fn:QName("http://www.example.com/ns/", "person"))', 'person')
self.check_value('fn:string(fn:QName("http://www.example.com/ns/", "ht:person"))',
'ht:person')
self.check_value('fn:string(fn:QName("http://www.example.com/ns/", "xs:person"))',
'xs:person')
self.wrong_value('fn:QName("http://www.example.com/ns/", "@person")')
self.wrong_type('fn:QName(1.0, "person")', 'XPTY0004', '1st argument has an invalid type')
self.wrong_type('fn:QName("", 2)', 'XPTY0004', '2nd argument has an invalid type')
self.wrong_value('fn:QName("", "3")', 'FOCA0002', 'invalid value')
self.wrong_value('fn:QName("", "xs:int")', 'FOCA0002',
'cannot associate a non-empty prefix with no namespace')
self.wrong_type('fn:QName("http://www.example.com/ns/")',
'XPST0017', '2nd argument missing')
self.wrong_type('fn:QName("http://www.example.com/ns/", "person"',
'XPST0017', 'Wrong number of arguments')
if xmlschema is not None:
schema = xmlschema.XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://foo.test">
<xs:element name="root"/>
</xs:schema>""")
with self.schema_bound_parser(schema.xpath_proxy):
context = self.parser.schema.get_context()
self.check_value('fn:QName("http://www.example.com/ns/", "@person")',
expected=ValueError, context=context)
def test_prefix_from_qname_function(self):
self.check_value(
'fn:prefix-from-QName(fn:QName("http://www.example.com/ns/", "ht:person"))', 'ht'
)
self.check_value(
'fn:prefix-from-QName(fn:QName("http://www.example.com/ns/", "person"))', []
)
self.check_value('fn:prefix-from-QName(())', [])
self.check_value('fn:prefix-from-QName(7)', TypeError)
self.check_value('fn:prefix-from-QName("7")', TypeError)
def test_local_name_from_qname_function(self):
self.check_value(
'fn:local-name-from-QName(fn:QName("http://www.example.com/ns/", "person"))', 'person'
)
self.check_value('fn:local-name-from-QName(())')
self.check_value('fn:local-name-from-QName(8)', TypeError)
self.check_value('fn:local-name-from-QName("8")', TypeError)
def test_namespace_uri_from_qname_function(self):
root = self.etree.XML('<p1:A xmlns:p1="ns1" xmlns:p0="ns0">'
' <B1><p2:C xmlns:p2="ns2"/></B1><B2/>'
' <p0:B3><eg:C1 xmlns:eg="http://www.example.com/ns/"/><C2/></p0:B3>'
'</p1:A>')
self.check_value(
'fn:namespace-uri-from-QName(fn:QName("http://www.example.com/ns/", "person"))',
'http://www.example.com/ns/'
)
self.check_value('fn:namespace-uri-from-QName(())')
self.check_value('fn:namespace-uri-from-QName(1)', TypeError)
self.check_value('fn:namespace-uri-from-QName("1")', TypeError)
self.check_selector("fn:namespace-uri-from-QName(xs:QName('p3:C3'))", root, KeyError)
self.check_selector("fn:namespace-uri-from-QName(xs:QName('p3:C3'))", root, ValueError,
namespaces={'p3': ''})
def test_resolve_qname_function(self):
root = self.etree.XML('<p1:A xmlns:p1="ns1" xmlns:p0="ns0">'
' <B1><p2:C xmlns:p2="ns2"/></B1><B2/>'
' <p0:B3><eg:C1 xmlns:eg="http://www.example.com/ns/"/><C2/></p0:B3>'
'</p1:A>')
context = XPathContext(root=root, namespaces=self.namespaces)
self.check_value("fn:resolve-QName((), .)", context=context)
if self.etree is lxml_etree:
self.check_value("fn:string(fn:resolve-QName('eg:C2', .))", KeyError,
context=context)
self.check_selector("fn:resolve-QName('p3:C3', .)", root, KeyError,
namespaces={'p3': ''})
else:
self.check_value("fn:string(fn:resolve-QName('eg:C2', .))", 'eg:C2',
context=context)
self.check_selector("fn:resolve-QName('p3:C3', .)", root, ValueError,
namespaces={'p3': ''})
self.check_raise("fn:resolve-QName('p3:C3', .)", KeyError, 'FONS0004',
"no namespace found for prefix 'p3'", context=context)
self.check_value("fn:resolve-QName('C3', .)", QName('', 'C3'), context=context)
self.check_value("fn:resolve-QName(2, .)", TypeError, context=context)
self.check_value("fn:resolve-QName('2', .)", ValueError, context=context)
self.check_value("fn:resolve-QName((), 4)", context=context)
self.wrong_type("fn:resolve-QName('p3:C3', 4)", 'FORG0006',
'2nd argument 4 is not an element node', context=context)
root = self.etree.XML('<A><B1><C/></B1><B2/><B3><C1/><C2/></B3></A>')
self.check_selector("fn:resolve-QName('C3', .)", root,
[QName('', 'C3')], namespaces={'': ''})
self.check_selector("fn:resolve-QName('xml:lang', .)", root,
[QName(XML_NAMESPACE, 'lang')])
def test_namespace_uri_for_prefix_function(self):
root = self.etree.XML('<p1:A xmlns:p1="ns1" xmlns:p0="ns0">'
' <B1><p2:C xmlns:p2="ns2"/></B1><B2/>'
' <p0:B3><eg:C1 xmlns:eg="http://www.example.com/ns/"/><C2/></p0:B3>'
'</p1:A>')
context = XPathContext(root=root)
self.check_value("fn:namespace-uri-for-prefix('p1', .)", context=context)
self.check_value("fn:namespace-uri-for-prefix(4, .)", TypeError, context=context)
self.check_value("fn:namespace-uri-for-prefix('p1', 9)", TypeError, context=context)
self.check_value("fn:namespace-uri-for-prefix('eg', .)",
'http://www.example.com/ns/', context=context)
self.check_selector("fn:namespace-uri-for-prefix('p3', .)",
root, NameError, namespaces={'p3': ''})
# Note: default namespace for XPath 2 tests is 'http://www.example.com/ns/'
self.check_value("fn:namespace-uri-for-prefix('', .)", context=context)
self.check_value(
'fn:namespace-uri-from-QName(fn:QName("http://www.example.com/ns/", "person"))',
'http://www.example.com/ns/'
)
self.check_value("fn:namespace-uri-for-prefix('', .)", context=context)
self.check_value("fn:namespace-uri-for-prefix((), .)", context=context)
def test_in_scope_prefixes_function(self):
root = self.etree.XML('<p1:A xmlns:p1="ns1" xmlns:p0="ns0">'
' <B1><p2:C xmlns:p2="ns2"/></B1><B2/>'
' <p0:B3><eg:C1 xmlns:eg="http://www.example.com/ns/"/><C2/></p0:B3>'
'</p1:A>')
namespaces = {'p0': 'ns0', 'p2': 'ns2'}
prefixes = select(root, "fn:in-scope-prefixes(.)", namespaces, parser=type(self.parser))
if self.etree is lxml_etree:
self.assertIn('p0', prefixes)
self.assertIn('p1', prefixes)
self.assertNotIn('p2', prefixes)
else:
self.assertIn('p0', prefixes)
self.assertNotIn('p1', prefixes)
self.assertIn('p2', prefixes)
# Provides namespaces through the dynamic context
selector = Selector("fn:in-scope-prefixes(.)", parser=type(self.parser))
prefixes = selector.select(root, namespaces=namespaces)
self.assertIn('p0', prefixes)
self.assertNotIn('p1', prefixes)
self.assertIn('p2', prefixes)
with self.assertRaises(TypeError):
select(root, "fn:in-scope-prefixes('')", namespaces, parser=type(self.parser))
root = self.etree.XML('<tns:A xmlns:tns="ns1" xmlns:xml="{}"/>'.format(XML_NAMESPACE))
namespaces = {'tns': 'ns1', 'xml': XML_NAMESPACE}
prefixes = select(root, "fn:in-scope-prefixes(.)", namespaces, parser=type(self.parser))
if self.etree is lxml_etree:
self.assertIn('tns', prefixes)
self.assertIn('xml', prefixes)
self.assertNotIn('fn', prefixes)
else:
self.assertIn('tns', prefixes)
self.assertIn('xml', prefixes)
self.assertIn('fn', prefixes)
if xmlschema is not None:
schema = xmlschema.XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://foo.test">
<xs:element name="root"/>
</xs:schema>""")
with self.schema_bound_parser(schema.xpath_proxy):
context = self.parser.schema.get_context()
prefixes = {'xml', 'xs', 'fn', 'err', 'xsi', 'eg', 'tst'}
if self.parser.version >= '3.0':
prefixes.add('math')
if self.parser.version >= '3.1':
prefixes.add('map')
prefixes.add('array')
self.check_value("fn:in-scope-prefixes(.)", prefixes, context)
def test_datetime_function(self):
tz = Timezone(datetime.timedelta(hours=5, minutes=24))
self.check_value('fn:dateTime((), xs:time("24:00:00"))', [])
self.check_value('fn:dateTime(xs:date("1999-12-31"), ())', [])
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("12:00:00"))',
datetime.datetime(1999, 12, 31, 12, 0))
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("24:00:00"))',
datetime.datetime(1999, 12, 31, 0, 0))
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("13:00:00+05:24"))',
datetime.datetime(1999, 12, 31, 13, 0, tzinfo=tz))
self.wrong_value('fn:dateTime(xs:date("1999-12-31+03:00"), xs:time("13:00:00+05:24"))',
'FORG0008', 'inconsistent timezones')
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("12:00:00"))', DateTime10)
with self.assertRaises(AssertionError):
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("12:00:00"))', DateTime)
self.parser._xsd_version = '1.1'
try:
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("12:00:00"))',
DateTime(1999, 12, 31, 12))
self.check_value('fn:dateTime(xs:date("1999-12-31"), xs:time("12:00:00"))', DateTime)
finally:
self.parser._xsd_version = '1.0'
def test_year_from_datetime_function(self):
self.check_value('fn:year-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))', 1999)
self.check_value('fn:year-from-dateTime(xs:dateTime("1999-05-31T21:30:00-05:00"))', 1999)
self.check_value('fn:year-from-dateTime(xs:dateTime("1999-12-31T19:20:00"))', 1999)
self.check_value('fn:year-from-dateTime(xs:dateTime("1999-12-31T24:00:00"))', 2000)
self.check_value('fn:year-from-dateTime(())')
def test_month_from_datetime_function(self):
self.check_value('fn:month-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))', 5)
self.check_value('fn:month-from-dateTime(xs:dateTime("1999-12-31T19:20:00-05:00"))', 12)
self.check_value('fn:month-from-dateTime(fn:adjust-dateTime-to-timezone(xs:dateTime('
'"1999-12-31T19:20:00-05:00"), xs:dayTimeDuration("PT0S")))', 1)
def test_day_from_datetime_function(self):
self.check_value('fn:day-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))', 31)
self.check_value('fn:day-from-dateTime(xs:dateTime("1999-12-31T20:00:00-05:00"))', 31)
self.check_value('fn:day-from-dateTime(fn:adjust-dateTime-to-timezone(xs:dateTime('
'"1999-12-31T19:20:00-05:00"), xs:dayTimeDuration("PT0S")))', 1)
def test_hours_from_datetime_function(self):
self.check_value('fn:hours-from-dateTime(xs:dateTime("1999-05-31T08:20:00-05:00")) ', 8)
self.check_value('fn:hours-from-dateTime(xs:dateTime("1999-12-31T21:20:00-05:00"))', 21)
self.check_value('fn:hours-from-dateTime(fn:adjust-dateTime-to-timezone(xs:dateTime('
'"1999-12-31T21:20:00-05:00"), xs:dayTimeDuration("PT0S")))', 2)
self.check_value('fn:hours-from-dateTime(xs:dateTime("1999-12-31T12:00:00")) ', 12)
self.check_value('fn:hours-from-dateTime(xs:dateTime("1999-12-31T24:00:00"))', 0)
def test_minutes_from_datetime_function(self):
self.check_value('fn:minutes-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))', 20)
self.check_value('fn:minutes-from-dateTime(xs:dateTime("1999-05-31T13:30:00+05:30"))', 30)
def test_seconds_from_datetime_function(self):
self.check_value('fn:seconds-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))', 0)
self.check_value('seconds-from-dateTime(xs:dateTime("2001-02-03T08:23:12.43"))',
Decimal('12.43'))
def test_timezone_from_datetime_function(self):
self.check_value('fn:timezone-from-dateTime(xs:dateTime("1999-05-31T13:20:00-05:00"))',
DayTimeDuration(seconds=-18000))
self.check_value('fn:timezone-from-dateTime(())')
def test_year_from_date_function(self):
self.check_value('fn:year-from-date(xs:date("1999-05-31"))', 1999)
self.check_value('fn:year-from-date(xs:date("2000-01-01+05:00"))', 2000)
self.check_value('year-from-date(())')
def test_month_from_date_function(self):
self.check_value('fn:month-from-date(xs:date("1999-05-31-05:00"))', 5)
self.check_value('fn:month-from-date(xs:date("2000-01-01+05:00"))', 1)
def test_day_from_date_function(self):
self.check_value('fn:day-from-date(xs:date("1999-05-31-05:00"))', 31)
self.check_value('fn:day-from-date(xs:date("2000-01-01+05:00"))', 1)
def test_timezone_from_date_function(self):
self.check_value('fn:timezone-from-date(xs:date("1999-05-31-05:00"))',
DayTimeDuration.fromstring('-PT5H'))
self.check_value('fn:timezone-from-date(xs:date("2000-06-12Z"))',
DayTimeDuration.fromstring('PT0H'))
self.check_value('fn:timezone-from-date(xs:date("2000-06-12"))')
def test_hours_from_time_function(self):
self.check_value('fn:hours-from-time(xs:time("11:23:00"))', 11)
self.check_value('fn:hours-from-time(xs:time("21:23:00"))', 21)
self.check_value('fn:hours-from-time(xs:time("01:23:00+05:00"))', 1)
self.check_value('fn:hours-from-time(fn:adjust-time-to-timezone(xs:time("01:23:00+05:00"), '
'xs:dayTimeDuration("PT0S")))', 20)
self.check_value('fn:hours-from-time(xs:time("24:00:00"))', 0)
def test_minutes_from_time_function(self):
self.check_value('fn:minutes-from-time(xs:time("13:00:00Z"))', 0)
self.check_value('fn:minutes-from-time(xs:time("09:45:10"))', 45)
def test_seconds_from_time_function(self):
self.check_value('fn:seconds-from-time(xs:time("13:20:10.5"))', 10.5)
self.check_value('fn:seconds-from-time(xs:time("20:50:10.0"))', 10.0)
self.check_value('fn:seconds-from-time(xs:time("03:59:59.000001"))', Decimal('59.000001'))
def test_timezone_from_time_function(self):
self.check_value('fn:timezone-from-time(xs:time("13:20:00-05:00"))',
DayTimeDuration.fromstring('-PT5H'))
self.check_value('timezone-from-time(())')
def test_years_from_duration_function(self):
self.check_value('fn:years-from-duration(())')
self.check_value('fn:years-from-duration(xs:yearMonthDuration("P20Y15M"))', 21)
self.check_value('fn:years-from-duration(xs:yearMonthDuration("-P15M"))', -1)
self.check_value('fn:years-from-duration(xs:dayTimeDuration("-P2DT15H"))', 0)
def test_months_from_duration_function(self):
self.check_value('fn:months-from-duration(())')
self.check_value('fn:months-from-duration(xs:yearMonthDuration("P20Y15M"))', 3)
self.check_value('fn:months-from-duration(xs:yearMonthDuration("-P20Y18M"))', -6)
self.check_value('fn:months-from-duration(xs:dayTimeDuration("-P2DT15H0M0S"))', 0)
def test_days_from_duration_function(self):
self.check_value('fn:days-from-duration(())')
self.check_value('fn:days-from-duration(xs:dayTimeDuration("P3DT10H"))', 3)
self.check_value('fn:days-from-duration(xs:dayTimeDuration("P3DT55H"))', 5)
self.check_value('fn:days-from-duration(xs:yearMonthDuration("P3Y5M"))', 0)
def test_hours_from_duration_function(self):
self.check_value('fn:hours-from-duration(())')
self.check_value('fn:hours-from-duration(xs:dayTimeDuration("P3DT10H"))', 10)
self.check_value('fn:hours-from-duration(xs:dayTimeDuration("P3DT12H32M12S"))', 12)
self.check_value('fn:hours-from-duration(xs:dayTimeDuration("PT123H"))', 3)
self.check_value('fn:hours-from-duration(xs:dayTimeDuration("-P3DT10H"))', -10)
def test_minutes_from_duration_function(self):
self.check_value('fn:minutes-from-duration(())')
self.check_value('fn:minutes-from-duration(xs:dayTimeDuration("P3DT10H"))', 0)
self.check_value('fn:minutes-from-duration(xs:dayTimeDuration("-P5DT12H30M"))', -30)
def test_seconds_from_duration_function(self):
self.check_value('fn:seconds-from-duration(())')
self.check_value('fn:seconds-from-duration(xs:dayTimeDuration("P3DT10H12.5S"))', 12.5)
self.check_value('fn:seconds-from-duration(xs:dayTimeDuration("-PT256S"))', -16.0)
def test_node_accessor_functions(self):
root = self.etree.XML('<A xmlns:ns0="%s" id="10"><B1><C1 /><C2 ns0:nil="true" /></B1>'
'<B2 /><B3>simple text</B3></A>' % XSI_NAMESPACE)
self.check_selector("node-name(.)", root, QName('', 'A'))
self.check_selector("node-name(/A/B1)", root, QName('', 'B1'))
self.check_selector("node-name(/A/*)", root, TypeError) # Not allowed more than one item!
self.check_selector("nilled(./B1/C1)", root, False)
self.check_selector("nilled(./B1/C2)", root, True)
self.check_raise("nilled(.)", MissingContextError)
context = XPathContext(root)
self.check_value('nilled(())', context=context)
self.wrong_type('nilled(8)', 'XPTY0004', 'an XPath node required', context=context)
self.check_value('node-name(())', context=context)
self.wrong_type('node-name(8)', 'XPTY0004', 'an XPath node required', context=context)
self.check_value('node-name(.)', context=XPathContext(self.etree.ElementTree(root)))
root = self.etree.XML('<tst:root xmlns:tst="http://xpath.test/ns" tst:a="10"/>')
self.check_value('node-name(.)', QName('http://xpath.test/ns', 'root'),
context=XPathContext(root))
self.check_value('node-name(./@tst:a)', QName('http://xpath.test/ns', 'a'),
context=XPathContext(root))
root = self.etree.XML('<root a="10"/>')
self.check_value('node-name(./@a)', QName('', 'a'), context=XPathContext(root))
root = self.etree.XML('<tst0:root xmlns:tst0="http://xpath.test/ns0"/>')
self.check_raise('node-name(.)', KeyError, 'FONS0004',
'no prefix found for namespace http://xpath.test/ns0',
context=XPathContext(root))
def test_node_name_function__issue__091(self):
root = self.etree.XML('<A xmlns:tns="http://xpath.test/tns" id="10" />')
root.tag = '{}A'
context = XPathContext(root)
self.check_value('node-name(.)', QName('', 'A'), context=context)
def test_string_and_data_functions(self):
root = self.etree.XML('<A id="10"><B1> a text, <C1 /><C2>an inner text, </C2>a tail, </B1>'
'<B2 /><B3>an ending text </B3></A>')
self.check_selector("/*/string()", root,
[' a text, an inner text, a tail, an ending text '])
self.check_selector("string(.)", root, ' a text, an inner text, a tail, an ending text ')
self.check_selector("data(.)", root, ' a text, an inner text, a tail, an ending text ')
self.check_selector("data(.)", root, UntypedAtomic)
self.check_selector("data(())", root, [])
self.check_value("string()", MissingContextError)
context = XPathContext(root=self.etree.XML('<A/>'))
parser = XPath2Parser(base_uri='http://www.example.com/ns/')
self.assertEqual(parser.parse('data(fn:resolve-uri(()))').evaluate(context), [])
@unittest.skipIf(xmlschema is None, "The xmlschema library is not installed")
def test_data_function_with_typed_nodes(self):
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child1" type="xs:int"/>
<xs:element name="child2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>"""))
self.parser.schema = xmlschema.xpath.XMLSchemaProxy(schema)
try:
root = self.etree.XML('<root/>')
self.wrong_value("data(/root)", 'FOTY0012',
'argument node', 'does not have a typed value',
context=XPathContext(root, schema=self.parser.schema))
self.wrong_value("data(.)", 'FOTY0012',
'argument node', 'does not have a typed value',
context=XPathContext(root, schema=self.parser.schema))
finally:
self.parser.schema = None
def test_node_set_id_function(self):
root = self.etree.XML('<A><B1 xml:id="foo"/><B2/><B3 xml:id="bar"/><B4 xml:id="baz"/></A>')
self.check_selector('element-with-id("foo")', root, [root[0]])
self.check_selector('id("foo")', root, [root[0]])
doc = self.etree.ElementTree(root)
root = doc.getroot()
self.check_selector('id("foo")', doc, [root[0]])
self.check_selector('id("fox")', doc, [])
self.check_selector('id("foo baz")', doc, [root[0], root[3]])
self.check_selector('id(("foo", "baz"))', doc, [root[0], root[3]])
self.check_selector('id(("foo", "baz bar"))', doc, [root[0], root[2], root[3]])
self.check_selector('id("baz bar foo")', doc, [root[0], root[2], root[3]])
# From XPath documentation
doc = self.etree.parse(io.StringIO("""
<employee xml:id="ID21256">
<empnr>E21256</empnr>
<first>John</first>
<last>Brown</last>
</employee>"""))
root = doc.getroot()
self.check_selector("id('ID21256')", doc, [root])
self.check_selector("id('E21256')", doc, [])
self.check_selector('element-with-id("ID21256")', doc, [root])
self.check_selector('element-with-id("E21256")', doc, [])
with self.assertRaises(MissingContextError) as err:
self.check_value("id('ID21256')")
self.assertIn('XPDY0002', str(err.exception))
context = XPathContext(doc, variables={'x': 11})
with self.assertRaises(TypeError) as err:
self.check_value("id('ID21256', $x)", context=context)
self.assertIn('XPTY0004', str(err.exception))
context = XPathContext(doc, item=11, variables={'x': 11})
with self.assertRaises(TypeError) as err:
self.check_value("id('ID21256', $x)", context=context)
self.assertIn('XPTY0004', str(err.exception))
context = XPathContext(doc, item=root, variables={'x': root})
self.check_value("id('ID21256', $x)", [context.root.getroot()], context=context)
# Id on root element
root = self.etree.XML("<empnr>E21256</empnr>")
self.check_selector("id('E21256')", root, [])
self.check_selector('element-with-id("E21256")', root, [])
@unittest.skipIf(xmlschema is None, "xmlschema library is not installed ...")
def test_node_set_id_function_with_schema(self):
root = self.etree.XML(dedent("""\
<employee xml:id="ID21256">
<empnr>E21256</empnr>
<first>John</first>
<last>Brown</last>
</employee>"""))
doc = self.etree.ElementTree(root)
# Test with matching value of type xs:ID
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="empnr" type="xs:ID"/>
<xs:element name="first" type="xs:string"/>
<xs:element name="last" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="xml:id"/>
</xs:complexType>
</xs:element>
</xs:schema>"""))
self.assertTrue(schema.is_valid(root))
context = XPathContext(doc, schema=schema.xpath_proxy)
self.check_select("id('ID21256')", [context.root.getroot()], context)
# Test with matching value of type xs:string
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="empnr" type="xs:string"/>
<xs:element name="first" type="xs:string"/>
<xs:element name="last" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="xml:id"/>
</xs:complexType>
</xs:element>
</xs:schema>"""))
self.assertTrue(schema.is_valid(root))
context = XPathContext(doc, schema=self.parser.schema)
self.check_select("id('E21256')", [], context)
@unittest.skipIf(xmlschema is None, "xmlschema library is not installed ...")
def test_node_set_id_function_with_wrong_schema(self):
root = self.etree.XML(dedent("""\
<employee xml:id="ID21256">
<empnr>E21256</empnr>
<first>John</first>
<last>Brown</last>
</employee>"""))
doc = self.etree.ElementTree(root)
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="empnr" type="xs:ID"/>
</xs:schema>"""))
self.assertFalse(schema.is_valid(root))
context = XPathContext(doc, schema=schema.xpath_proxy)
self.check_select("id('ID21256')", [context.root.getroot()], context)
self.check_select("id('E21256')", [], context)
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee" type="xs:string"/>
</xs:schema>"""))
self.assertFalse(schema.is_valid(root))
context = XPathContext(doc, schema=schema.xpath_proxy)
self.check_select("id('ID21256')", [context.root.getroot()], context)
self.check_select("id('E21256')", [], context)
def test_node_set_idref_function(self):
doc = self.etree.parse(io.StringIO("""
<employees>
<employee xml:id="ID21256">
<empnr>E21256</empnr>
<first>John</first>
<last>Brown</last>
</employee>
<employee xml:id="ID21257">
<empnr>E21257</empnr>
<first>John</first>
<last>Doe</last>
</employee>
</employees>"""))
root = doc.getroot()
self.check_value("idref('ID21256')", MissingContextError)
self.check_selector("idref('ID21256')", doc, [])
self.check_selector("idref('E21256')", doc, [root[0][0]])
self.check_selector("idref('ID21256')", root, [])
context = XPathContext(doc, variables={'x': 11})
self.wrong_type("idref('ID21256', $x)", 'XPTY0004', context=context)
context = XPathContext(doc, item=root, variables={'x': root})
self.check_value("idref('ID21256', $x)", [], context=context)
context = XPathContext(doc, item=root)
context.variables = {
'x': TextAttributeNode(XML_ID, 'ID21256', parent=context.root[0])
}
self.check_value("idref('ID21256', $x)", [], context=context)
context = XPathContext(root, variables={'x': None})
self.wrong_type("idref('ID21256', $x)", 'XPTY0004', context=context)
context = XPathContext(root, variables={'x': []})
self.wrong_type("idref('ID21256', $x)", 'XPTY0004', context=context)
context = XPathContext(root)
self.wrong_type("idref('ID21256', ())", 'XPTY0004', context=context)
def test_deep_equal_function(self):
root = self.etree.XML("""
<attendees>
<name last='Parker' first='Peter'/>
<name last='Barker' first='Bob'/>
<name last='Parker' first='Peter'/>
</attendees>""")
context = XPathContext(root, variables={'xt': root})
self.check_value('fn:deep-equal($xt, $xt)', True, context=context)
self.check_value('deep-equal($xt, $xt/*)', False, context=context)
self.check_value('deep-equal($xt/name[1], $xt/name[2])', False, context=context)
self.check_value('deep-equal($xt/name[1], $xt/name[3])', True, context=context)
self.check_value('deep-equal($xt/name[1], $xt/name[3]/@last)', False, context=context)
self.check_value('deep-equal($xt/name[1]/@last, $xt/name[3]/@last)', True, context=context)
self.check_value('deep-equal($xt/name[1]/@last, $xt/name[2]/@last)', False, context=context)
self.check_value('deep-equal($xt/name[1], "Peter Parker")', False, context=context)
root = self.etree.XML("""<A xmlns="http://xpath.test/ns"><B1/><B2/><B3/></A>""")
context = XPathContext(root, variables={'xt': root})
self.check_value('deep-equal($xt, $xt)', True, context=context)
self.check_value('deep-equal((1, 2, 3), (1, 2, 3))', True)
self.check_value('deep-equal((1, 2, 3), (1, (), 3))', False)
self.check_value('deep-equal((true(), 2, 3), (1, 2, 3))', False)
self.check_value('deep-equal((true(), 2, 3), (true(), 2, 3))', True)
self.check_value('deep-equal((1, 2, 3), (true(), 2, 3))', False)
self.check_value('deep-equal((xs:untypedAtomic("1"), 2, 3), (1, 2, 3))', False)
self.check_value('deep-equal((1, 2, 3), (xs:untypedAtomic("1"), 2, 3))', False)
self.check_value(
'deep-equal((xs:untypedAtomic("1"), 2, 3), (xs:untypedAtomic("2"), 2, 3))', False
)
self.check_value(
'deep-equal((xs:untypedAtomic("1"), 2, 3), (xs:untypedAtomic("1"), 2, 3))', True
)
self.check_value('deep-equal((), (1, 2, 3))', False)
self.check_value('deep-equal((1, 2, 3), (1, 2, 4))', False)
self.check_value("deep-equal((1, 2, 3), (1, '2', 3))", False)
self.check_value("deep-equal(('1', '2', '3'), ('1', '2', '3'))", True)
self.check_value("deep-equal(('1', '2', '3'), ('1', '4', '3'))", False)
self.check_value("deep-equal((1, 2, 3), (1, 2, 3), 'en_US.UTF-8')", True)
self.check_value('fn:deep-equal(xs:float("NaN"), xs:double("NaN"))', True)
self.check_value('fn:deep-equal(xs:float("NaN"), 1.0)', False)
self.check_value('fn:deep-equal(1.0, xs:double("NaN"))', False)
self.check_value('deep-equal((1.1E0, 2E0, 3), (1.1, 2.0, 3))', True)
self.check_value('deep-equal((1.1E0, 2E0, 3), (1.1, 2.1, 3))', False)
self.check_value('deep-equal((1E0, 2E0, 3), (1, 2, 3))', True)
self.check_value('deep-equal((1E0, 2E0, 3), (1, 4, 3))', False)
self.check_value('deep-equal((1.1, 2.0, 3), (1.1E0, 2E0, 3))', True)
self.check_value('deep-equal((1.1, 2.1, 3), (1.1E0, 2E0, 3))', False)
self.check_value('deep-equal((1, 2, 3), (1E0, 2E0, 3))', True)
self.check_value('deep-equal((1, 4, 3), (1E0, 2E0, 3))', False)
self.check_value('deep-equal(3.1, xs:anyURI("http://xpath.test")) ', False)
context = XPathContext(root)
context.variables = {'a': [TextNode('alpha')],
'b': [TextNode('beta')]}
self.check_value('deep-equal($a, $a)', True, context=context)
self.check_value('deep-equal($a, $b)', False, context=context)
context = XPathContext(root)
context.variables = {'a': [TextAttributeNode('a', '10')],
'b': [TextAttributeNode('b', '10')]}
self.check_value('deep-equal($a, $a)', True, context=context)
self.check_value('deep-equal($a, $b)', False, context=context)
context.variables = {'a': [NamespaceNode('tns0', 'http://xpath.test/ns')],
'b': [NamespaceNode('tns1', 'http://xpath.test/ns')]}
self.check_value('deep-equal($a, $a)', True, context=context)
self.check_value('deep-equal($a, $b)', False, context=context)
def test_deep_equal_function_on_nested_sequences(self):
self.check_value('fn:deep-equal(1, 1)', True)
self.check_value('fn:deep-equal(1, (1))', True)
self.check_value('fn:deep-equal(1, (1, ()))', True)
self.check_value('fn:deep-equal(1, (1, (1)))', False)
self.check_value('fn:deep-equal((1, ()), (1, (1)))', False)
self.check_value('fn:deep-equal(((1), 1), (1, (1)))', True)
def test_adjust_datetime_to_timezone_function(self):
context = XPathContext(root=self.etree.XML('<A/>'), timezone=Timezone.fromstring('-05:00'),
variables={'tz': DayTimeDuration.fromstring("-PT10H")})
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00-07:00"))',
DateTime.fromstring('2002-03-07T12:00:00-05:00'), context)
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00"))',
DateTime.fromstring('2002-03-07T10:00:00'))
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00"))',
DateTime.fromstring('2002-03-07T10:00:00-05:00'), context)
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00"), $tz)',
DateTime.fromstring('2002-03-07T10:00:00-10:00'), context)
self.check_value(
'fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00-07:00"), $tz)',
DateTime.fromstring('2002-03-07T07:00:00-10:00'), context
)
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00-07:00"), '
'xs:dayTimeDuration("PT10H"))',
DateTime.fromstring('2002-03-08T03:00:00+10:00'), context)
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T00:00:00+01:00"), '
'xs:dayTimeDuration("-PT8H"))',
DateTime.fromstring('2002-03-06T15:00:00-08:00'), context)
self.check_value('fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00"), ())',
DateTime.fromstring('2002-03-07T10:00:00'), context)
self.check_value(
'fn:adjust-dateTime-to-timezone(xs:dateTime("2002-03-07T10:00:00-07:00"), ())',
DateTime.fromstring('2002-03-07T10:00:00'), context
)
self.check_value('fn:adjust-dateTime-to-timezone((), ())')
def test_adjust_date_to_timezone_function(self):
context = XPathContext(root=self.etree.XML('<A/>'), timezone=Timezone.fromstring('-05:00'),
variables={'tz': DayTimeDuration.fromstring("-PT10H")})
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07"))',
Date.fromstring('2002-03-07-05:00'), context)
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07-07:00"))',
Date.fromstring('2002-03-07-05:00'), context)
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07"), $tz)',
Date.fromstring('2002-03-07-10:00'), context)
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07"), ())',
Date.fromstring('2002-03-07'), context)
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07-07:00"), ())',
Date.fromstring('2002-03-07'), context)
self.check_value('fn:adjust-date-to-timezone(xs:date("2002-03-07-07:00"), $tz)',
Date.fromstring('2002-03-06-10:00'), context)
self.check_value('fn:adjust-date-to-timezone((), ())')
self.check_value('adjust-date-to-timezone(xs:date("-25252734927766555-06-07+02:00"), '
'xs:dayTimeDuration("PT0S"))', OverflowError)
def test_adjust_time_to_timezone_function(self):
context = XPathContext(root=self.etree.XML('<A/>'), timezone=Timezone.fromstring('-05:00'),
variables={'tz': DayTimeDuration.fromstring("-PT10H")})
self.check_value('fn:adjust-time-to-timezone(())')
self.check_value('fn:adjust-time-to-timezone((), ())')
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00"))',
Time.fromstring('10:00:00-05:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00-07:00"))',
Time.fromstring('12:00:00-05:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00"), $tz)',
Time.fromstring('10:00:00-10:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00-07:00"), $tz)',
Time.fromstring('07:00:00-10:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00"), ())',
Time.fromstring('10:00:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00-07:00"), ())',
Time.fromstring('10:00:00'), context)
self.check_value('fn:adjust-time-to-timezone(xs:time("10:00:00-07:00"), '
'xs:dayTimeDuration("PT10H"))',
Time.fromstring('03:00:00+10:00'), context)
def test_default_collation_function(self):
default_collation = self.parser.default_collation
self.check_value('fn:default-collation()', default_collation)
def test_context_datetime_functions(self):
context = XPathContext(root=self.etree.XML('<A/>'))
self.check_value('fn:current-dateTime()', context=context,
expected=DateTime10.fromdatetime(context.current_dt))
self.check_value(path='fn:current-date()', context=context,
expected=Date10.fromdatetime(context.current_dt.date()))
self.check_value(path='fn:current-time()', context=context,
expected=Time.fromdatetime(context.current_dt))
self.check_value(path='fn:implicit-timezone()', context=context,
expected=DayTimeDuration(seconds=time.timezone))
context.timezone = Timezone.fromstring('-05:00')
self.check_value(path='fn:implicit-timezone()', context=context,
expected=DayTimeDuration.fromstring('-PT5H'))
self.parser._xsd_version = '1.1'
try:
self.check_value('fn:current-dateTime()', context=context,
expected=DateTime.fromdatetime(context.current_dt))
self.check_value(path='fn:current-date()', context=context,
expected=Date.fromdatetime(context.current_dt.date()))
finally:
self.parser._xsd_version = '1.0'
def test_static_base_uri_function(self):
context = XPathContext(root=self.etree.XML('<A/>'))
self.check_value('fn:static-base-uri()', context=context)
parser = XPath2Parser(strict=True, base_uri='http://example.com/ns/')
self.assertEqual(parser.parse('fn:static-base-uri()').evaluate(context),
'http://example.com/ns/')
def test_base_uri_function(self):
context = XPathContext(root=self.etree.XML('<A/>'))
with self.assertRaises(MissingContextError) as err:
self.check_value('fn:base-uri(())')
self.assertIn('XPDY0002', str(err.exception))
self.assertIn('context item is undefined', str(err.exception))
self.check_value('fn:base-uri(9)', MissingContextError)
self.check_value('fn:base-uri(9)', TypeError, context=context)
self.check_value('fn:base-uri()', datatypes.AnyURI(''), context=context)
self.check_value('fn:base-uri(())', context=context)
context = XPathContext(root=self.etree.XML('<A xml:base="/base_path/"/>'))
self.check_value('fn:base-uri()', '/base_path/', context=context)
def test_document_uri_function(self):
document = self.etree.parse(io.StringIO('<A/>'))
context = XPathContext(root=document)
self.check_value('fn:document-uri(())', context=context)
self.check_value('fn:document-uri(.)', context=context)
context = XPathContext(root=document.getroot(), item=document,
documents={'/base_path/': document})
self.check_value('fn:document-uri(.)', context=context)
context = XPathContext(root=document, documents={'/base_path/': document})
self.check_value('fn:document-uri(.)', '/base_path/', context=context)
context = XPathContext(root=document, documents={
'/base_path/': self.etree.parse(io.StringIO('<A/>')),
})
self.check_value('fn:document-uri(.)', context=context)
document = self.etree.parse(io.StringIO('<A xml:base="/base_path/"/>'))
context = XPathContext(root=document)
self.check_value('fn:document-uri(.)', context=context) # xml:base doesn't apply!
document_node = get_node_tree(document, uri='/foo/bar.xml')
context = XPathContext(root=document_node)
self.check_value('fn:document-uri(.)', '/foo/bar.xml', context=context)
# Relative URIs doesn't apply for fn:document-uri
document_node = get_node_tree(document, uri='foo/bar.xml')
context = XPathContext(root=document_node)
self.check_value('fn:document-uri(.)', None, context=context)
def test_doc_functions(self):
root = self.etree.XML("<A><B1><C1/></B1><B2/><B3/></A>")
doc = self.etree.parse(io.StringIO("<a><b1><c1/></b1><b2/><b3/></a>"))
context = XPathContext(root, documents={'tns0': doc})
self.check_value("fn:doc(())", context=context)
self.check_value("fn:doc-available(())", False, context=context)
self.wrong_value('fn:doc-available(xs:untypedAtomic("2"))', 'FODC0002', context=context)
self.wrong_type('fn:doc-available(2)', 'XPTY0004', context=context)
self.check_value("fn:doc('tns0')", context.documents['tns0'], context=context)
self.check_value("fn:doc-available('tns0')", True, context=context)
self.check_value("fn:doc('tns1')", ValueError, context=context)
self.check_value("fn:doc-available('tns1')", False, context=context)
self.parser.base_uri = "/path1"
self.check_value("fn:doc('http://foo.test')", ValueError, context=context)
self.check_value("fn:doc-available('http://foo.test')", False, context=context)
self.parser.base_uri = None
doc = self.etree.XML("<a><b1><c1/></b1><b2/><b3/></a>")
context = XPathContext(root, documents={'tns0': doc})
self.wrong_type("fn:doc('tns0')", 'XPDY0050', context=context)
self.wrong_type("fn:doc-available('tns0')", 'XPDY0050', context=context)
context = XPathContext(root, documents={'file.xml': None})
self.wrong_value("fn:doc('file.xml')", 'FODC0002', context=context)
self.wrong_value("fn:doc('unknown')", 'FODC0002', context=context)
self.check_value("fn:doc-available('unknown')", False, context=context)
dirpath = os.path.dirname(__file__)
self.wrong_value("fn:doc('{}')".format(dirpath), 'FODC0005', context=context)
def test_collection_function(self):
root = self.etree.XML("<A><B1><C1/></B1><B2/><B3/></A>")
doc1 = self.etree.parse(io.StringIO("<a><b1><c1/></b1><b2/><b3/></a>"))
doc2 = self.etree.parse(io.StringIO("<a1><b11><c11/></b11><b12/><b13/></a1>"))
context = XPathContext(root, collections={'tns0': [doc1, doc2]})
collection = context.collections['tns0']
self.check_value("fn:collection('tns0')", collection, context=context)
self.parser.collection_types = {'tns0': 'node()*'}
self.check_value("fn:collection('tns0')", collection, context=context)
self.parser.collection_types = {'tns0': 'node()'}
self.check_value("fn:collection('tns0')", TypeError, context=context)
self.check_value("fn:collection()", ValueError, context=context)
context.default_collection = context.collections['tns0']
self.check_value("fn:collection()", collection, context=context)
self.parser.default_collection_type = 'node()'
self.check_value("fn:collection()", TypeError, context=context)
self.parser.default_collection_type = 'node()*'
context = XPathContext(root)
self.wrong_value("fn:collection('filepath')", 'FODC0002', context=context)
self.wrong_value("fn:collection('dirpath/')", 'FODC0002', context=context)
def test_root_function(self):
root = self.etree.XML("<A><B1><C1/></B1><B2/><B3/></A>")
context = XPathContext(root)
self.check_value("root()", context.root, context=context)
context = XPathContext(root, item=root[2])
self.check_value("root()", context.root, context=context)
with self.assertRaises(TypeError) as err:
context = XPathContext(root, item=10)
self.check_value("root()", context.root, context=context)
self.assertIn('XPTY0004', str(err.exception))
with self.assertRaises(TypeError) as err:
self.check_value("root(7)", root, context=XPathContext(root))
self.assertIn('XPTY0004', str(err.exception))
context = XPathContext(root, variables={'elem': root[1]})
self.check_value("fn:root(())", context=context)
self.check_value("fn:root($elem)", context.root, context=context)
doc = self.etree.XML("<a><b1><c1/></b1><b2/><b3/></a>")
context = XPathContext(root, variables={'elem': doc[1]})
self.check_value("fn:root($elem)", context=context)
context = XPathContext(root, variables={'elem': doc[1]}, documents={})
self.check_value("fn:root($elem)", context=context)
context = XPathContext(root, variables={'elem': doc[1]}, documents={'.': doc})
self.check_value("root($elem)", context.documents['.'], context=context)
doc2 = self.etree.XML("<a><b1><c1/></b1><b2/><b3/></a>")
context = XPathContext(root, variables={'elem': doc2[1]}, documents={'.': doc})
self.check_value("root($elem)", context=context)
context = XPathContext(root, variables={'elem': doc2[1]},
documents={'.': doc, 'doc2': doc2})
self.check_value("root($elem)", context.documents['doc2'], context=context)
if xmlschema is not None:
schema = xmlschema.XMLSchema(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://foo.test">
<xs:element name="root"/>
</xs:schema>"""))
with self.schema_bound_parser(schema.xpath_proxy):
context = self.parser.schema.get_context()
self.check_value("fn:root()", None, context)
def test_error_function(self):
with self.assertRaises(ElementPathError) as err:
self.check_value('fn:error()')
self.assertIn('[err:FOER0000] Unidentified error', str(err.exception))
with self.assertRaises(ElementPathError) as err:
self.check_value('fn:error("err:XPST0001")')
self.assertIn("[err:XPTY0004]", str(err.exception))
with self.assertRaises(ElementPathError) as err:
self.check_value(
"fn:error(fn:QName('http://www.w3.org/2005/xqt-errors', 'err:XPST0001'))"
)
self.assertIn('[err:XPST0001] Parser not bound to a schema', str(err.exception))
with self.assertRaises(ElementPathError) as err:
self.check_value(
"fn:error(fn:QName('http://www.w3.org/2005/xqt-errors', 'err:XPST0001'), "
"'Missing schema')"
)
self.assertIn('[err:XPST0001] Missing schema', str(err.exception))
def test_trace_function(self):
self.check_value('trace((), "trace message")', [])
self.check_value('trace("foo", "trace message")', ['foo'])
@unittest.skipIf(lxml_etree is None, "The lxml library is not installed")
class LxmlXPath2FunctionsTest(XPath2FunctionsTest):
etree = lxml_etree
if __name__ == '__main__':
unittest.main()
|