user3280723
user3280723

Reputation: 11

etree.strip_tags returning 'None' when trying to strip tag

Script:

print entryDetails

for i in range(len(entryDetails)):
    print etree.tostring(entryDetails[i])

    print etree.strip_tags(entryDetails[i], 'entry-details')

Output:

[<Element entry-details at 0x234e0a8>, <Element entry-details at 0x234e878>]
<entry-details>2014-02-05 11:57:01</entry-details>
None
<entry-details>2014-02-05 12:11:05</entry-details>
None

How is etree.strip_tags failing to strip the entry-details tag? Is the dash in the tag name affecting it?

Upvotes: 1

Views: 665

Answers (1)

mzjn
mzjn

Reputation: 50957

strip_tags() does not return anything. It strips off the tags in-place.

The documentation says: "Note that this will not delete the element (or ElementTree root element) that you passed even if it matches. It will only treat its descendants.".

Demo code:

from lxml import etree

XML = """
<root>
 <entry-details>ABC</entry-details>
</root>"""

root = etree.fromstring(XML)
ed = root.xpath("//entry-details")[0]
print ed
print

etree.strip_tags(ed, "entry-details")       # Has no effect 
print etree.tostring(root)
print

etree.strip_tags(root, "entry-details")     
print etree.tostring(root)

Output:

<Element entry-details at 0x2123b98>

<root>
 <entry-details>ABC</entry-details>
</root>

<root>
 ABC
</root>

Upvotes: 1

Related Questions