Vinay Surve
Vinay Surve

Reputation: 85

How to get text value of XML node when it has child nodes

I have scenario like this:

<fetch>
   <xyz:match selector="ph-%"/>
   tools.build.compiler
</fetch>

Now the XML node <fetch> has both child node and text value. I want to extract the text value of <fetch> node. I am using python and Lxml for parsing the XML. I used the LXML's element.text but I am getting text value as None. Can any one tell what mistake I am doing?

Upvotes: 2

Views: 179

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213578

You need the tail member of the child.

def get_text(node):
    text = [node.text] + [child.tail for child in node]
    return ''.join(x for x in text if x is not None)

The schema you are using is a little weird. LXML is designed to work with more traditional ways of encoding data into XML, although it supports general XML parsing the .tail usage will always be a little weird unless your schema are straightforward.

Upvotes: 1

Related Questions