tags:

views:

36

answers:

1

Is it possible get all context nodes used to evalute xpath result ? In below code:

test_xml = """
<r>
    <a/>
    <a>
        <b/>
    </a>
    <a>
        <b/>
    </a>
</r>
"""
test_root = lxml.etree.fromstring(test_xml)
res = test_root.xpath("//following-sibling::*[1]/b")
for node in res:
    print test_root.getroottree().getpath(node)

Result are: /r/a[2]/b /r/a[3]/b

Is it possible to get all context nodes used in above xpath evaluation:

/r/a[1] /r/a[2] /r/a[2]/b

and for second result:

/r/a[2] /r/a[3]/b /r/a[3]/b

? When using child axis I could get those nodes from working on element_tree.getpath(elem) but what abouth other axes ? TIA, regards

A: 

If I am understanding your question correctly, you are trying to just get the ancestor nodes of particular nodes. The XPATH expression for this would be:

//particular-element/ancestor::*

such as:

/r/a[2]/b/ancestor::*
sw