0

I've been searching for a while. Is there a way to directly retrieve a child based on the text value of a tag?

For example:

 <a>
   <b>
       <c>h</c>
   </b>
   <b>
       <c>j</c>
   </b>
</a>

And say I want to retrieve the child be whose "c" text value == j. Is there a way of doing this other than having to get all the "b" children and looping through them and checking the c value?

bb2
  • 2,872
  • 7
  • 37
  • 45

1 Answers1

5

If you use lxml you can use the XPath expression //c[text()='j']

import lxml.etree as et

doc = '''
<a>
   <b>
       <c>h</c>
   </b>
   <b>
       <c>j</c>
   </b>
</a>
'''

tree = et.fromstring(doc)

tree.xpath("/a/b/c[text()='j']")

This answer is relevant: How do I match contents of an element in XPath (lxml)?

Community
  • 1
  • 1
Acorn
  • 49,061
  • 27
  • 133
  • 172