I'm trying to develop a Python script in order to extract easily XPath of elements in a XML or HTML file.
For instance, Imagine we have the XML file below (test.xml) for which we would like to get the XPATH of "blue" :
<root>
<element>
<name>Element1</name>
<contains>
<element>
<name>color</name>
<value-ref>/Colors/red</value-ref>
</element>
</contains>
</element>
<element>
<name>Colors</name>
<contains>
<element>
<name>red</name>
<value>0xFF0000</value>
</element>
<element>
<name>blue</name>
<value>0x0000FF</value>
</element>
</contains>
</element>
</root>
I tried to use LXML, but I'm bit lost :
from lxml import etree
doc = etree.parse('test.xml')
tree = etree.ElementTree(doc.getroot())
How can I get the XPath of the element in tree with text="blue"?
Thank you, Thomas