I wanted to get xpath of each element in xml file.
xml file:
<root
xmlns="http://www.w3.org/TR/html4/"
xmlns:h="http://www.w3schools.com/furniture">
<table>
<tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</tr>
</table>
</root>
python code: Since null prefix in default namespace is not allowed,i used my own prefix for that.
from lxml import etree
root=etree.parse(open("MyData.xml",'r'))
ns={'df': 'http://www.w3.org/TR/html4/', 'types': 'http://www.w3schools.com/furniture'}
for e in root.iter():
b=root.getpath(e)
print b
r=root.xpath(b,namespaces=ns)
#i need both b and r here
the xpath is like this(output b)
/*
/*/*[1]
/*/*[1]/*[1]
/*/*[1]/*[1]/h:td
i can't get the xpath correctly for elements having default namespace,it shows as * for those elements name. How to get xpath correctly?