Suppose I have the following test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<test:myXML xmlns:test="http://com/my/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Parent>
<FirstNode name="FirstNodeName"></FirstNode>
<Child1>Test from Child1</Child1>
<SecondNode name="SecondNodeName" type="SecondNodeType">
<Child2>
<GrandChild>Test from GrandChild</GrandChild>
</Child2>
</SecondNode>
</Parent>
</test:myXML>
I'd like to iterate over the whole tree, and get the path of each node, including the attributes. I am able to iterate over the tree and retrieve the path to each node as follows:
from lxml import etree
xmlDoc = etree.parse("test.xml")
root = xmlDoc.getroot()
for node in xmlDoc.iter():
print("path: ", xmlDoc.getpath(node))
As expected, this prints out:
path: /test:myXML
path: /test:myXML/Parent
path: /test:myXML/Parent/FirstNode
path: /test:myXML/Parent/Child1
path: /test:myXML/Parent/SecondNode
path: /test:myXML/Parent/SecondNode/Child2
path: /test:myXML/Parent/SecondNode/Child2/GrandChild
However, as I mentioned, I'd like to somehow print the attributes of said node, and its parents, along with its path. For example, if I want to print the element "Child2", then I'd like for the attributes of each of its parent elements to be displayed as well. Something like:
path: /test:myXML/Parent/SecondNode{name="SecondNodeName" type="SecondNodeType"}/Child2
Is this possible? I'm not too fussed about the namespaces of the root element if that makes it easier.