msg157299 - (view) |
Author: Tshepang Lekhonkhobe (tshepang) * |
Date: 2012-04-01 15:28 |
I often miss lxml's "pretty_print=True" functionality. Can you implement something similar.
|
msg157317 - (view) |
Author: Martin v. Löwis (loewis) * |
Date: 2012-04-01 16:49 |
Would you like to provide a patch?
|
msg157320 - (view) |
Author: Eli Bendersky (eli.bendersky) * |
Date: 2012-04-01 17:59 |
Tshepang,
Frankly, there are a lot of issues to solve in ElementTree (it hasn't been given love in a long time...) and such features would be low priority, as I'm not getting much help and am swamped already.
As Martin said, patches can go a long way here...
|
msg157325 - (view) |
Author: Tshepang Lekhonkhobe (tshepang) * |
Date: 2012-04-01 19:08 |
Okay, I will try, even though C scares me.
|
msg157647 - (view) |
Author: Éric Araujo (eric.araujo) * |
Date: 2012-04-06 06:24 |
You may be able to code it entirely in the Python part of the module (adding a new parameter to Element.write and tostring).
|
msg194313 - (view) |
Author: Eli Bendersky (eli.bendersky) * |
Date: 2013-08-03 22:26 |
A patch exists in the duplicate #17372
|
msg194508 - (view) |
Author: Alex Henderson (alex.henderson) * |
Date: 2013-08-05 21:14 |
Proposed patch copied over from duplicate issue 17372.
|
msg194902 - (view) |
Author: Stefan Behnel (scoder) * |
Date: 2013-08-11 16:30 |
Just to reiterate this point, lxml.etree supports a "pretty_print" flag in its tostring() function and ElementTree.write(). It would thus make sense to support the same thing in ET.
http://lxml.de/api.html#serialisation
For completeness, the current signature looks like this:
def tostring(element_or_tree, *, encoding=None, method="xml",
xml_declaration=None, pretty_print=False,
with_tail=True, standalone=None, doctype=None,
exclusive=False, with_comments=True,
inclusive_ns_prefixes=None):
(The last three options are for C14N serialisation.)
|
msg304362 - (view) |
Author: STINNER Victor (vstinner) * |
Date: 2017-10-13 21:16 |
For the record, at 2015-04-02, the bpo-23847 has been marked as a duplicate of this issue.
|
msg304872 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2017-10-24 08:46 |
My thoughts:
1. Whitespaces are significant in XML. Pretty-printed XML is different from the original XML to an XML parser. For some applications some whitespaces around tags are not significant. But this depends on the application and in different parts of the document whitespaces can have different meaning. For example the document can contain a metadata with insignificant whitespaces and marked up text with significant whitespaces. There is a special attribute named xml:space that can signal the meaning of whitespaces for the part of a document.
https://www.w3.org/TR/xml/#sec-white-space
2. In HTML whitespaces around <P> are insignificant, but whitespaces around <I> are significant. Whitespaces inside <PRE>...</PRE> are significant.
3. If strip whitespaces around tags and insert newlines and indentations, shouldn't we strip whitespaces inside the text context? Or preserve newlines but update indentations?
4. If modify whitespaces on output, it may be worth to add an option to ignore insignificant whitespaces on input.
5. Serialization of ElementTree in the stdlib is much slower than in lxml (see issue25881). Perhaps it should be implemented in C. But it should be kept simple for this. Pretty-printing can be implemented as an outher preprocessing operation (for example the original Eli's code indents the tree in-place: http://effbot.org/zone/element-lib.htm#prettyprint) or as a proxy that indents elements on-fly.
|
msg323690 - (view) |
Author: Stefan Behnel (scoder) * |
Date: 2018-08-18 05:06 |
> Serialization of ElementTree in the stdlib is much slower than in lxml (see issue25881). Perhaps it should be implemented in C. But it should be kept simple for this.
Should I say it? That's a first class use case for Cython.
> Pretty-printing can be implemented as an outher preprocessing operation
Agreed. And that would actually be much simpler to implement in C.
|
msg324098 - (view) |
Author: Raymond Hettinger (rhettinger) * |
Date: 2018-08-25 20:00 |
A few more thoughts for consideration:
* We already have a toprettyxml() tool in the minidom package.
* Since whitespace is significant in XML, prettifying changes the content and meaning, so it doesn't round-trip and should only be used for debugging purposes.
* Usually, I recommend using XML viewers such as the one built into the Chrome browser. That provides indentation without changing meaning. It also lets you run searches and conveniently supports folding and unfolding elements. I would rather someone use a viewer rather than something like toprettyxml().
|
msg335306 - (view) |
Author: Clayton Olney (Clayton Olney) |
Date: 2019-02-12 14:36 |
I have a use case where the receiving application is expecting the indentation, and I need to run my code in Lambda. So, lxml is out of the question.
|
msg349326 - (view) |
Author: Raymond Hettinger (rhettinger) * |
Date: 2019-08-10 03:08 |
FWIW, here is the relevant section of the XML specification, https://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space :
"""In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code.
An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content.
"""
OTOH, the java TransformerFactory does support a property, OutputKeys.INDENT, so there is a precedent for this feature request.
Stefan, would you please make a final determination or pronouncement on whether this makes sense for ElementTree or whether it is outside the scope of what the module is trying to accomplish.
|
msg349346 - (view) |
Author: Stefan Behnel (scoder) * |
Date: 2019-08-10 17:06 |
The spec section that Raymond quoted makes it clear that pretty printing is not for everyone. But there are many use cases where it is 1) helpful, 2) leads to correct results, and 3) does not grow the file size excessively. Whoever wants to make use of it is probably in such a situation. I think adding some kind of support in the standard library would be nice, but it should not hurt "normal" uses, especially when a lot of data is involved.
I'll send a PR that adds an indent() function to pre-process trees. Comments welcome.
|
msg350301 - (view) |
Author: Stefan Behnel (scoder) * |
Date: 2019-08-23 14:44 |
New changeset b5d3ceea48c181b3e2c6c67424317afed606bd39 by Stefan Behnel in branch 'master':
bpo-14465: Add an indent() function to xml.etree.ElementTree to pretty-print XML trees (GH-15200)
https://github.com/python/cpython/commit/b5d3ceea48c181b3e2c6c67424317afed606bd39
|
|
Date |
User |
Action |
Args |
2022-04-11 14:57:28 | admin | set | github: 58670 |
2019-08-23 14:45:28 | scoder | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
2019-08-23 14:44:37 | scoder | set | messages:
+ msg350301 |
2019-08-12 13:07:08 | vstinner | set | nosy:
- vstinner
|
2019-08-10 17:06:37 | scoder | set | pull_requests:
+ pull_request14929 |
2019-08-10 17:06:16 | scoder | set | assignee: serhiy.storchaka -> scoder messages:
+ msg349346 versions:
+ Python 3.9, - Python 3.7, Python 3.8 |
2019-08-10 03:08:27 | rhettinger | set | messages:
+ msg349326 |
2019-08-08 15:53:22 | Andrew Grant | set | nosy:
+ Andrew Grant
|
2019-06-22 20:31:20 | mitar | set | nosy:
+ mitar
|
2019-02-12 14:36:01 | Clayton Olney | set | nosy:
+ Clayton Olney messages:
+ msg335306
|
2018-08-25 20:00:03 | rhettinger | set | nosy:
+ rhettinger messages:
+ msg324098
|
2018-08-25 19:22:26 | mcepl | set | pull_requests:
+ pull_request8405 |
2018-08-18 05:06:24 | scoder | set | messages:
+ msg323690 |
2018-08-17 15:23:10 | mcepl | set | versions:
+ Python 3.8 |
2017-10-24 08:46:06 | serhiy.storchaka | set | messages:
+ msg304872 |
2017-10-22 20:32:54 | wolma | set | nosy:
+ wolma
|
2017-10-18 12:19:04 | serhiy.storchaka | set | assignee: serhiy.storchaka
nosy:
+ serhiy.storchaka components:
+ XML versions:
+ Python 3.7, - Python 3.4 |
2017-10-17 07:47:12 | alex.dzyoba | set | stage: patch review pull_requests:
+ pull_request3990 |
2017-10-14 17:31:29 | alex.dzyoba | set | nosy:
+ alex.dzyoba
|
2017-10-13 21:16:29 | vstinner | set | nosy:
+ vstinner messages:
+ msg304362
|
2015-04-02 08:20:47 | serhiy.storchaka | link | issue23847 superseder |
2015-01-27 10:23:02 | martin.panter | set | nosy:
+ martin.panter
|
2015-01-27 09:26:05 | mcepl | set | nosy:
+ mcepl
|
2013-08-11 17:56:38 | eric.snow | set | nosy:
+ eric.snow
|
2013-08-11 16:30:21 | scoder | set | nosy:
+ scoder
messages:
+ msg194902 versions:
+ Python 3.4, - Python 3.3 |
2013-08-05 21:14:48 | alex.henderson | set | files:
+ issue14465.patch
nosy:
+ alex.henderson messages:
+ msg194508
keywords:
+ patch |
2013-08-03 22:26:04 | eli.bendersky | set | messages:
+ msg194313 |
2013-08-03 22:25:43 | eli.bendersky | link | issue17372 superseder |
2012-04-06 06:24:10 | eric.araujo | set | nosy:
+ eric.araujo messages:
+ msg157647
|
2012-04-05 22:20:48 | santoso.wijaya | set | nosy:
+ santoso.wijaya
|
2012-04-01 19:08:10 | tshepang | set | messages:
+ msg157325 |
2012-04-01 17:59:20 | eli.bendersky | set | messages:
+ msg157320 |
2012-04-01 16:49:32 | loewis | set | nosy:
+ loewis messages:
+ msg157317
|
2012-04-01 16:43:46 | r.david.murray | set | type: enhancement |
2012-04-01 15:29:13 | tshepang | set | title: add feature to prettify XML output -> xml.etree.ElementTree: add feature to prettify XML output |
2012-04-01 15:28:03 | tshepang | create | |