Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:07:10+00:00 2026-06-12T05:07:10+00:00

I have some XML that is generated by a script that may or may

  • 0

I have some XML that is generated by a script that may or may not have empty elements. I was told that now we cannot have empty elements in the XML. Here is an example:

<customer>  
    <govId>
       <id>@</id>
       <idType>SSN</idType>
           <issueDate/>
           <expireDate/>
           <dob/>
           <state/>
           <county/>
           <country/>
    </govId>
    <govId>
        <id/>
        <idType/>
        <issueDate/>
        <expireDate/>
        <dob/>
        <state/>
        <county/>
        <country/>
    </govId>
</customer>

The output should look like this:

<customer>  
    <govId>
       <id>@</id>
       <idType>SSN</idType>        
    </govId>        
</customer>

I need to remove all the empty elements. You’ll note that my code took out the empty stuff in the “govId” sub-element, but didn’t take out anything in the second. I am using lxml.objectify at the moment.

Here is basically what I am doing:

root = objectify.fromstring(xml)
for customer in root.customers.iterchildren():
    for e in customer.govId.iterchildren():
        if not e.text:
            customer.govId.remove(e)

Does anyone know of a way to do this with lxml objectify or is there an easier way period? I would also like to remove the second “govId” element in its entirety if all its elements are empty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:07:11+00:00Added an answer on June 12, 2026 at 5:07 am

    First of all, the problem with your code is that you are iterating over customers, but not over govIds. On the third line you take the first govId for every customer, and iterate over its children. So, you’d need a another for loop for the code to work like you intended it to.

    This small sentence at the end of your question then makes the problem quite a bit more complex: I would also like to remove the second “govId” element in its entirety if all its elements are empty.

    This means, unless you want to hard code just checking one level of nesting, you need to recursively check if an element and it’s children are empty. Like this for example:

    def recursively_empty(e):
       if e.text:
           return False
       return all((recursively_empty(c) for c in e.iterchildren()))
    

    Note: Python 2.5+ because of the use of the all() builtin.

    You then can change your code to something like this to remove all the elements in the document that are empty all the way down.

    # Walk over all elements in the tree and remove all
    # nodes that are recursively empty
    context = etree.iterwalk(root)
    for action, elem in context:
        parent = elem.getparent()
        if recursively_empty(elem):
            parent.remove(elem)
    

    Sample output:

    <customer>
      <govId>
        <id>@</id>
        <idType>SSN</idType>
      </govId>
    </customer>
    

    One thing you might want to do is refine the condition if e.text: in the recursive function. Currently this will consider None and the empty string as empty, but not whitespace like spaces and newlines. Use str.strip() if that’s part of your definition of “empty”.


    Edit: As pointed out by @Dave, the recursive function could be improved by using a generator expression:

    return all((recursively_empty(c) for c in e.getchildren()))
    

    This will not evaluate recursively_empty(c) for all the children at once, but evaluate it for each one lazily. Since all() will stop iteration upon the first False element, this could mean a significant performance improvement.

    Edit 2: The expression can be further optimized by using e.iterchildren() instead of e.getchildren(). This works with the lxml etree API and the objectify API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some code that was generated, mostly likely by svcutil from an XML
I have some XML that is structured like this: <whatson> <productions> <production> <category>Film</category> </production>
I have some XML that I want to parse using the lxml method in
I have some XML files that contain different function information. I am trying to
I have some xml files that contain text, which are displayed on my website.
I am parsing some XML that will have a link such as the following
I have some data in an XML element that looks like this: <?xml version=1.0
I have some code that uses Open XML to open up a .docx file,
I have some data in a database that I need represented in an XML
I have an SSIS (2005) package that transforms some XML data and then imports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.