This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: xml.etree.ElementTree.write does not support `standalone` option
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, docs@python, eli.bendersky, ericvergnaud, kj, scoder, twowolfs
Priority: normal Keywords:

Created on 2021-09-30 19:58 by twowolfs, last changed 2022-04-11 14:59 by admin.

Messages (9)
msg402981 - (view) Author: ed wolf (twowolfs) Date: 2021-09-30 19:58
When executing the following command after modifiy an xml file an error is prodcued.

import xml.etree.ElementTree as ET
rtexmlFile = 'Fox_CM3550A_SWP1_Rte_ecuc.arxml'
rte_ecu_tree = ET.parse(rtexmlFile)
root = rte_ecu_tree.getroot()

rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration="True", default_namespace="None" method="xml",short_empty_elements="True" )

ValueError: cannot use non-qualified names with default_namespace option

The documentation for the ElementTree.write function indicates the following format for this command but this format does not seem to wrok

The write command does not also take into account when having standalone in the xml defintion. For ex,

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

ElementTree.write will not add standalone back to the xml file

Is this a bug in version 3.7?


write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml", *, short_empty_elements=True)
Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

The output is either a string (str) or binary (bytes). This is controlled by the encoding argument. If encoding is "unicode", the output is a string; otherwise, it’s binary. Note that this may conflict with the type of file if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa.
msg404251 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-10-19 00:00
Ed: something looks a bit odd with the call to `write()` that you used:

> rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration="True", default_namespace="None" method="xml",short_empty_elements="True" )

Note that default val for `default_namespace` is None, but you have it quoted, and you also have quoted xml_declaration and short_empty_elements. Try re-running with these arg values unquoted.
msg404286 - (view) Author: ed wolf (twowolfs) Date: 2021-10-19 10:48
Hi Andrew

I removed the quotes and still see an issue with the standalone not being added to the xml declaration. I set the command as follows

rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration=True, default_namespace=None, method="xml",short_empty_elements=True )

The xml declaration came out as
<?xml version='1.0' encoding='UTF-8'?>

but should be
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

standalone did not get added to the declaration.
msg404295 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-10-19 13:17
Ed: it seems ElementTree.write does not support `standalone` option, but both minidom (https://docs.python.org/3/library/xml.dom.minidom.html) and lxml (https://lxml.de/) support it. Are either of those suitable for your usecase?
msg404297 - (view) Author: ed wolf (twowolfs) Date: 2021-10-19 13:33
Will ElementTree.write be updated to correct this issue?
msg404304 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-10-19 13:50
Ed: I can look into adding it, but not sure when. If you can make the case that minidom and lxml are not suitable workarounds for this, it will be more likely that me or someone else will add this option; but note that as this is a new feature, it will only go into Python 3.11 .
msg415198 - (view) Author: Eric Vergnaud (ericvergnaud) Date: 2022-03-14 21:54
This is not a feature request, it's a bug fix request, so should be fixed asap.

Why is it a bug ?
XML spec says that "the default namespace does not apply to attribute names" (see section 6.3), therefore having a simple attribute name when using a default namespace is a perfectly valid scenario.

Raising a ValueError in add_qname (line 864) is therefore incorrect if the qname being added is a simple name of an attribute

not sure if lxml is able to parse very large documents (>4g) but I'll try it
msg415199 - (view) Author: Eric Vergnaud (ericvergnaud) Date: 2022-03-14 22:00
lxml tostring does not support the default_namespace value so not an option
msg415204 - (view) Author: Eric Vergnaud (ericvergnaud) Date: 2022-03-14 22:29
Actually there are 2 distinct issues here:
 - ValueError: cannot use non-qualified names with default_namespace option
 - lack of 'standalone' option when writing XML PI
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89499
2022-03-21 20:37:17ned.deilysetnosy: + scoder, eli.bendersky
2022-03-14 22:29:44ericvergnaudsetmessages: + msg415204
2022-03-14 22:00:39ericvergnaudsetmessages: + msg415199
2022-03-14 21:54:30ericvergnaudsetnosy: + ericvergnaud
messages: + msg415198
2021-10-19 13:50:24andrei.avksetassignee: docs@python ->
messages: + msg404304
2021-10-19 13:46:58andrei.avksettitle: xml.tree.ElementTree.write does not support `standalone` option -> xml.etree.ElementTree.write does not support `standalone` option
2021-10-19 13:33:59twowolfssetmessages: + msg404297
2021-10-19 13:18:14andrei.avksettitle: Issue with xml.tree.ElementTree.write -> xml.tree.ElementTree.write does not support `standalone` option
2021-10-19 13:17:38andrei.avksetnosy: + kj
components: + Library (Lib), - Documentation
2021-10-19 13:17:01andrei.avksetmessages: + msg404295
2021-10-19 10:48:34twowolfssetmessages: + msg404286
2021-10-19 00:00:17andrei.avksettype: performance -> behavior
2021-10-19 00:00:04andrei.avksetnosy: + andrei.avk
messages: + msg404251
2021-09-30 19:58:41twowolfscreate