File: advanced.py

package info (click to toggle)
elementpath 5.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,360 kB
  • sloc: python: 35,725; xml: 40; makefile: 13
file content (30 lines) | stat: -rwxr-xr-x 1,015 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

def main() -> None:
    from io import StringIO
    from xml.etree import ElementTree
    from elementpath import XPath2Parser, XPathToken, XPathContext, DocumentNode

    parser = XPath2Parser()
    token = parser.parse('/root/(: comment :) child[@attr]')
    assert isinstance(token, XPathToken)
    assert token.tree == '(/ (/ (root)) ([ (child) (@ (attr))))'
    assert token.source == '/root/child[@attr]'

    root = ElementTree.XML('<root><child/><child attr="10"/></root>')
    context = XPathContext(root)
    value = token.evaluate(context)
    print(value)

    token = parser.parse('concat("foo", " ", "bar")')
    assert context.root is not None and token.evaluate() == 'foo bar'

    doc = ElementTree.parse(StringIO('<root><child1/><child2/><child3/></root>'))
    context = XPathContext(doc)  # error?
    assert isinstance(context.root, DocumentNode)
    assert context.document is context.root
    assert context.item is context.root


if __name__ == '__main__':
    main()