Find an xml element with some specific text using xpath or find in python using lxml
lxml, python, xpath
Solution
Here is one way to do it:
from lxml import etree
# Create an ElementTree instance
tree = etree.parse("bookstore.xml")
# Get all 'book' elements that have a 'name' child with a string value of 'abc'
books = tree.xpath('book[name="abc"]')
# Print name and price of those books
for book in books:
print book.find("name").text, book.find("price").text
Output when using the XML in the question:
abc 30
Problem
I am trying to find all `book` elements with value `abc` i.e. `name` tag value. I used xpath: `val= xml1.xpath('//bookstore/book/name[text()="abc"]')` But it is returning None. ``` <bookstore> <book> <name>abc</name> <price>30</price> </book> <book> <name>Learning XML</name> <price>56</price> </book> </bookstore> ```