I am attempting to remove empty XML elements from an XML, but having an issue with the elements that have attributes but no text values. I can successfully remove the empty XML elements, but cannot preserve the elements with attributes in the final XML. I would like to essentially clean up the XML and remove empty nodes with no text values altogether, but keep the nodes with attributes.
Below is the script I am using, along with the input and (desired) output XMLs....any assistance is most appreciated!
The Script:
from lxml import etree
import os
path = "C:\\users\\mdl518\\Desktop\\"
### Removing empty XML elements
tree = etree.parse(os.path.join(path,"my_file.xml"))
for elem in tree.xpath('//*[not(node())]'):
elem.getparent().remove(elem):
with open(".//new_file.xml","wb") as f:
f.write(etree.tostring(tree, xml_declaration=True, encoding='utf-8')) ## Removes empty XML elements, including the elements with attributes
Input XML:
<?xml version='1.0' encoding='utf-8'?>
<nas:metadata xmlns:nas="http://www.arcgis.com/schema/nas/base"
xmlns:mcc="http://standards.org/iso/19115/-3/mcc/1.0"
xmlns:mdl="http://standards.org/iso/19115/-3/mdl/1.0"
xmlns:mnl="http://standards.org/iso/19115/-3/mnl/1.0">
xmlns:lan="http://standards.org/iso/19115/-3/lan/1.0">
xmlns:lis="http://standards.org/iso/19115/-3/lis/1.0">
xmlns:gam="http://standards.org/iso/19115/-3/gam/1.0">
<mdl:metadataIdentifier>
<mcc:MD_Identifier>
<mnl:type>
<gam:String>The Metadata File</gam:String>
</mnl:type>
<mnl:description codeList="http://arcgis.com/codelist/ScopeCode" codeListValue="dataset"/>
<mnl:address>
<mnl:defaultLocale>
</mnl:defaultLocale>
</mnl:address>
<lan:language>
<lan:type>
<lis:name>English</lis:name>
</lan:type>
</lan:language>
</mcc:MD_Identifier>
<mcc:contactInfo>
<mdl:POC>
<mnl:name>
<lis:person>Tom</lis:person>
</mnl:name>
<mnl:age>
</mnl:age>
<mnl:status>
</mnl:status>
</mdl:POC>
</mcc:contactInfo>
</mdl:metadataIdentifier>
</nas:metadata>
Output XML:
<?xml version='1.0' encoding='utf-8'?>
<nas:metadata xmlns:nas="http://www.arcgis.com/schema/nas/base"
xmlns:mcc="http://standards.org/iso/19115/-3/mcc/1.0"
xmlns:mdl="http://standards.org/iso/19115/-3/mdl/1.0"
xmlns:mnl="http://standards.org/iso/19115/-3/mnl/1.0">
xmlns:lan="http://standards.org/iso/19115/-3/lan/1.0">
xmlns:lis="http://standards.org/iso/19115/-3/lis/1.0">
xmlns:gam="http://standards.org/iso/19115/-3/gam/1.0">
<mdl:metadataIdentifier>
<mcc:MD_Identifier>
<mnl:type>
<gam:String>The Metadata File</gam:String>
</mnl:type>
<mnl:description codeList="http://arcgis.com/codelist/ScopeCode" codeListValue="dataset"/>
<lan:language>
<lan:type>
<lis:name>English</lis:name>
</lan:type>
</lan:language>
</mcc:MD_Identifier>
<mcc:contactInfo>
<mdl:POC>
<mnl:name>
<lis:person>Tom</lis:person>
</mnl:name>
</mdl:POC>
</mcc:contactInfo>
</mdl:metadataIdentifier>
</nas:metadata>
The xml is your question is not well formed, but assuming that's fixed, try changing this line
for elem in tree.xpath('//*[not(node())]'):
to this:
for elem in tree.xpath('//*[not(node())][not(count(./@*))>0]'):
and see if it works.
Edit:
The edited XML in the question still isn't well formed. I tried to fix it and then applied the following:
xml_str = """<?xml version='1.0' encoding='utf-8'?>
<nas:metadata xmlns:nas="http://www.arcgis.com/schema/nas/base"
xmlns:mcc="http://standards.org/iso/19115/-3/mcc/1.0"
xmlns:mdl="http://standards.org/iso/19115/-3/mdl/1.0"
xmlns:mnl="http://standards.org/iso/19115/-3/mnl/1.0"
xmlns:lan="http://standards.org/iso/19115/-3/lan/1.0"
xmlns:lis="http://standards.org/iso/19115/-3/lis/1.0"
xmlns:gam="http://standards.org/iso/19115/-3/gam/1.0">
<mdl:metadataIdentifier>
<mcc:MD_Identifier>
<mnl:type>
<gam:String>The Metadata File</gam:String>
</mnl:type>
<mnl:description codeList="http://arcgis.com/codelist/ScopeCode" codeListValue="dataset"/>
<mnl:address>
<mnl:defaultLocale>
</mnl:defaultLocale>
</mnl:address>
<lan:language>
<lan:type>
<lis:name>English</lis:name>
</lan:type>
</lan:language>
</mcc:MD_Identifier>
<mcc:contactInfo>
<mdl:POC>
<mnl:name>
<lis:person>Tom</lis:person>
</mnl:name>
<mnl:age>
</mnl:age>
<mnl:status>
</mnl:status>
</mdl:POC>
</mcc:contactInfo>
</mdl:metadataIdentifier>
</nas:metadata>
"""
doc = etree.XML(xml_str.encode())
for elem in doc.xpath('//*[not(count(./@*))>0][not(normalize-space(.))]'):
elem.getparent().remove(elem)
print(etree.tostring(doc, xml_declaration=True, encoding='utf-8').decode())
The output I get from the above is the desired output in the question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.