source: exist/trunk/python/elementtree-1.3a6-20070310-badc/unittests/test_xpath.py @ 3578

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070310-badc/unittests/test_xpath.py@3578
Revision 3578, 3.4 KB checked in by pjkersha, 11 years ago (diff)

Latest releases from Fredrik Lundh. 10 March release has exclusive C14N support with namespace prefixes.

Line 
1# $Id: test_xpath.py 3340 2007-12-19 00:44:05Z fredrik $
2
3from support import *
4from elementtree import ElementPath
5
6def xpath_tokenizer(p, ns=None):
7    """
8    Test the XPath tokenizer.
9
10    >>> # tests from the xml specification
11    >>> xpath_tokenizer("*")
12    ['*']
13    >>> xpath_tokenizer("text()")
14    ['text', '()']
15    >>> xpath_tokenizer("@name")
16    ['@', 'name']
17    >>> xpath_tokenizer("@*")
18    ['@', '*']
19    >>> xpath_tokenizer("para[1]")
20    ['para', '[', '1', ']']
21    >>> xpath_tokenizer("para[last()]")
22    ['para', '[', 'last', '()', ']']
23    >>> xpath_tokenizer("*/para")
24    ['*', '/', 'para']
25    >>> xpath_tokenizer("/doc/chapter[5]/section[2]")
26    ['/', 'doc', '/', 'chapter', '[', '5', ']', '/', 'section', '[', '2', ']']
27    >>> xpath_tokenizer("chapter//para")
28    ['chapter', '//', 'para']
29    >>> xpath_tokenizer("//para")
30    ['//', 'para']
31    >>> xpath_tokenizer("//olist/item")
32    ['//', 'olist', '/', 'item']
33    >>> xpath_tokenizer(".")
34    ['.']
35    >>> xpath_tokenizer(".//para")
36    ['.', '//', 'para']
37    >>> xpath_tokenizer("..")
38    ['..']
39    >>> xpath_tokenizer("../@lang")
40    ['..', '/', '@', 'lang']
41    >>> xpath_tokenizer("chapter[title]")
42    ['chapter', '[', 'title', ']']
43    >>> xpath_tokenizer("employee[@secretary and @assistant]")
44    ['employee', '[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']']
45
46    prefixes
47    >>> xpath_tokenizer("spam:egg", dict(spam="http://spam"))
48    ['{http://spam}egg']
49    >>> xpath_tokenizer("./spam:egg", dict(spam="http://spam"))
50    ['.', '/', '{http://spam}egg']
51    >>> xpath_tokenizer(".//spam:egg", dict(spam="http://spam"))
52    ['.', '//', '{http://spam}egg']
53
54    clark notation
55    >>> xpath_tokenizer("{http://spam}egg")
56    ['{http://spam}egg']
57    >>> xpath_tokenizer("./spam.egg")
58    ['.', '/', 'spam.egg']
59    >>> xpath_tokenizer(".//{http://spam}egg")
60    ['.', '//', '{http://spam}egg']
61    """
62    out = []
63    for op, tag in ElementPath.xpath_tokenizer(p, ns):
64        out.append(op or tag)
65    return out
66
67# --------------------------------------------------------------------
68# jquery path tests
69
70target = ET.parse("unittests/jquery.xml").getroot()
71
72def t(description, path):
73    return [
74        elem.get("id") for elem in target.findall(path) if elem.get("id")
75        ]
76
77def jquery_xpath():
78    """
79
80    >>> t("All P Elements", ".//p")
81    ['firstp', 'ap', 'sndp', 'en', 'sap', 'first']
82    >>> t("Absolute Path", "./body")
83    ['body']
84    >>> t("Absolute Path w/ *", "./*")
85    ['body']
86    >>> t("Long Absolute Path", "./body/dl/div/div/p")
87    ['sndp', 'en', 'sap']
88    >>> t("Absolute and Relative Paths", ".//p")
89    ['firstp', 'ap', 'sndp', 'en', 'sap', 'first']
90    >>> t("All Children, Explicit", ".//code/*")
91    ['anchor1', 'anchor2']
92    >>> t("All Children, Implicit", ".//code/")
93    ['anchor1', 'anchor2']
94    >>> t("Attribute Exists", ".//a[@title]")
95    ['google']
96    >>> t("Attribute Equals", ".//a[@rel='bookmark']")
97    ['simon1']
98    >>> t("Parent Axis", ".//p/..")
99    ['main', 'foo']
100    >>> t("Sibling Axis", ".//p/../")
101    ['firstp', 'ap', 'foo', 'first', 'firstUL', 'empty', 'form', 'floatTest', 'iframe', 'lengthtest', 'table', 'fx-queue', 'fx-tests', 'sndp', 'en', 'sap']
102    >>> t("Sibling Axis", ".//p/../*")
103    ['firstp', 'ap', 'foo', 'first', 'firstUL', 'empty', 'form', 'floatTest', 'iframe', 'lengthtest', 'table', 'fx-queue', 'fx-tests', 'sndp', 'en', 'sap']
104    >>> t("Has Children", ".//p[a]")
105    ['firstp', 'ap', 'en', 'sap']
106
107    """
Note: See TracBrowser for help on using the repository browser.