'Parsing XML with xpath in Python
I'm using the following code to check on a condition based on value of an xpath. Not all XMLs I've has this xpath. I want to identify which XML has this xpath, and which one don't. I'm using "from lxml import etree"
Sample XML - A:
<x>
<a>
<b>30</b>
</a>
</x>
Sample XML - B:
<x>
<a>
<c>20</c>
</a>
</x>
Code:
check_id = tree.xpath("a/b")
if check_id != None:
is_id = True
else:
is_id = False
I want to print 'True' for XML A and 'False' for XML B. But I'm getting this for both:
[] True
Solution 1:[1]
I think you should use
check_id = tree.xpath("//*/a/b")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego Torres Milano |
