trad
trad

Reputation: 1

Removing empty xml nodes

I have an xml file that I'm trying to remove empty nodes from with python. When I've tested it to check if a the value is, say, 'shark', it works. But when i check for it being none, it doesn't remove the empty node.

for records in recordList:
        for fieldGroup in records:
            for field in fieldGroup:
                if field.text is None:
                    fieldGroup.remove(field)

Upvotes: 0

Views: 1933

Answers (2)

AlexB1971
AlexB1971

Reputation: 1

For the benefit of others, I'd like to share this.

I trawled various posts and articles attempting to find something that would remove empty nodes from the query results I wrote, but I think it could be used in this instance also.

modify('delete //modules[fn:not(.//text()) = xs:boolean("true")]')

To give some context, the query brought back many empty nodes and I wanted to clean it up.

After populating a XML variable, @textXML, I removed the empty modules nodes using:

    set @testXML.modify('delete //modules[fn:not(.//text()) = xs:boolean("true")]')

    select @testXML;

Upvotes: -2

MattH
MattH

Reputation: 38247

is your friend here.

from lxml import etree

doc = etree.XML("""<root><a>1</a><b><c></c></b><d></d></root>""")

def remove_empty_elements(doc):
  for element in doc.xpath('//*[not(node())]'):
    element.getparent().remove(element)

Then:

>>> print etree.tostring(doc,pretty_print=True)
<root>
  <a>1</a>
  <b>
    <c/>
  </b>
  <d/>
</root>

>>> remove_empty_elements(doc)
>>> print etree.tostring(doc,pretty_print=True)
<root>
  <a>1</a>
  <b/>
</root>

>>> remove_empty_elements(doc)
>>> print etree.tostring(doc,pretty_print=True)
<root>
  <a>1</a>
</root>

Upvotes: 2

Related Questions