pjain
pjain

Reputation: 683

Error in retrieving root element in xml using lxml python

I have following xml: val='''

<bookstore>
   <book>
    <title lang="en">Harry Potter</title>
    <price>29.99</price>
  </book>
  <book>
    <title lang="en">Learning XML</title>
    <price>39.95</price>
  </book>
</bookstore>

I want to retrieve first element, i.e. "bookstore" and I am doing:

etree.parse(StringIO(xml1))
val.xpath('bookstore')

but it is giving [] empty list, but same script is working when I am doing for book, val.xpath('book').

How to retrieve first element??

Upvotes: 0

Views: 50

Answers (1)

Helmer
Helmer

Reputation: 439

You could try:

/bookstore  # child

or

//bookstore  # descendant at any depth

I don't think you are targeting the nodes correctly atm

Upvotes: 2

Related Questions