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()
|