You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
xml.dom.minidom toprettyxml: omit whitespace for text-only elements #48397
For XML elements containing only text data, it would be nice if
toprettyxml could omit the whitespace it normally injects before & after
the text, e.g.
<person>
<first-name>
Bob
</first-name>
</person>
Becomes:
<person>
<first-name>Bob</first-name>
</person>
From what I understand the handling of whitespace within XML elements is
application-defined, so I'm classifying this as a nice-to-have feature.
However it should be noted that in our particular case, the existing
behaviour caused a few problems with a third-party system which treated
whitespace as being significant.
Also needed here. While pretty-printing should be able to insert
non-significant whitespace BETWEEN xml elements, it should never alter
the content of (i.e. insert whitespace into) existing text elements.
jgarrison commented on Jul 16, 2009
jgarrisonmannequin
Mannequin
More actions
To clarify:
... it should never alter the content of (i.e. insert whitespace into)
existing text elements that contain non-whitespace characters.
m-samia commented on Apr 3, 2010
m-samiamannequin
Mannequin
More actions
Could you please apply that patch? People are starting to use non-standard libraries to process xml files because of this issue
@thomas: could you provide a unit test to go with your patch.
techtonik commented on Nov 24, 2010
techtonikmannequin
Mannequin
More actions
This one is bug.
dankenigsberg commented on Oct 1, 2011
dankenigsbergmannequin
Mannequin
More actions
Here's another take on fixing this bug, with an accompanying unit test. Personally, I'm monkey-patching xml.dom.minidom in order to avoid it, but please consider fixing it properly upstream.
This looks correct to me, and it tested out fine on the test suite (and the provided test failed without the provided fix), so I committed it.
I have a small concern that the change in output might be a bit radical for a bug fix release, but it does seem to me that this is clearly a bug. If people think it shouldn't go in the bug fix releases let me know and I'll back it out.
Thanks, Enzio, for pointing out that former patch is wrong. It is also quite naive, since the whole NATURE of toprettyprint() is to add whitespace to Text nodes. Tomas Lee's http://bugs.python.org/file11832/minidom-toprettyxml-01.patch made an effort to touch only "simple" Text nodes, that are confined within a single <elem></elem>.
btw, http://www.w3.org/TR/xml/#sec-white-space is a bit vague on how should a parser deal with whitespace, and seems to allow non-preservation of text nodes. Preserving "simple" text nodes is allowed, too, and is more polite to applications reading the prettyxml.
Heh, you happened to post your patch at a time when I wanted something to do as a break from something I didn't want to do...and I *thought* I understood the problem, after reading the various links. But clearly I didn't. We don't have someone who has stepped forward to be xml maintainer, so I just went ahead and committed it.
I should find time to look at your new patch some time today, or perhaps Ezio will have time. (Clearly minidom doesn't have enough tests.)
techtonik commented on Nov 15, 2011
techtonikmannequin
Mannequin
More actions
I would revert this patch (leaving several test cases though) until a proper fix is available.
Yeah, I just haven't found time to do the revert yet (my first naive attempt using hg commands failed and I haven't found time to figure it out or do the reverse-patch method).
Here is a new patch based on Dan's last patch.
Correct me if I'm wrong, but it seems to me that it's not possible for a node to have only text-nodes as children and yet have more than one child; i.e. you can't have two or more adjacent text nodes, because they would be considered as a single text node. I therefore changed the check with all() to check if there's only a child and if it's a text node.
I also added a test that checks where the \n and \t are added, because testing only that the DOM is preserved is not enough.
dankenigsberg commented on Nov 15, 2011
dankenigsbergmannequin
Mannequin
More actions
Technically, adjacent Text nodes are not illegal, but preserving this oddity in pretty-print is impossible. It is perfectly fine to pretty-print only the simple case of len()==1.
I did some tests, creating an element ('elem') that contains two adjacent text nodes ('text'). With my latest patch the prettyprint is:
<?xml version="1.0" ?>
<elem>
text
text
</elem>
Here both the text nodes are printed on a newline and they are indented.
With your patch it should be:
<?xml version="1.0" ?>
<elem>texttext</elem>
I'm not sure there's any reason to prefer the second option though.
python-dev commented on Nov 18, 2011
python-devmannequin
Mannequin
More actions
New changeset 7262f8f276ff by Ezio Melotti in branch '2.7': bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg. http://hg.python.org/cpython/rev/7262f8f276ff
New changeset 5401daa96a21 by Ezio Melotti in branch '3.2': bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg. http://hg.python.org/cpython/rev/5401daa96a21
Activity
thomaslee commentedon Oct 19, 2008
For XML elements containing only text data, it would be nice if
toprettyxml could omit the whitespace it normally injects before & after
the text, e.g.
<person>
<first-name>
Bob
</first-name>
</person>
Becomes:
<person>
<first-name>Bob</first-name>
</person>
From what I understand the handling of whitespace within XML elements is
application-defined, so I'm classifying this as a nice-to-have feature.
However it should be noted that in our particular case, the existing
behaviour caused a few problems with a third-party system which treated
whitespace as being significant.
jgarrison commentedon Jul 15, 2009
Also needed here. While pretty-printing should be able to insert
non-significant whitespace BETWEEN xml elements, it should never alter
the content of (i.e. insert whitespace into) existing text elements.
jgarrison commentedon Jul 16, 2009
To clarify:
... it should never alter the content of (i.e. insert whitespace into)
existing text elements that contain non-whitespace characters.
m-samia commentedon Apr 3, 2010
Could you please apply that patch? People are starting to use non-standard libraries to process xml files because of this issue
Is there any problem with that patch?
BreamoreBoy commentedon Jul 26, 2010
@thomas: could you provide a unit test to go with your patch.
techtonik commentedon Nov 24, 2010
This one is bug.
dankenigsberg commentedon Oct 1, 2011
Here's another take on fixing this bug, with an accompanying unit test. Personally, I'm monkey-patching xml.dom.minidom in order to avoid it, but please consider fixing it properly upstream.
python-dev commentedon Oct 1, 2011
New changeset 086ca132e161 by R David Murray in branch '3.2':
bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
http://hg.python.org/cpython/rev/086ca132e161
New changeset fa0b1e50270f by R David Murray in branch 'default':
merge bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
http://hg.python.org/cpython/rev/fa0b1e50270f
python-dev commentedon Oct 1, 2011
New changeset 406c5b69cb1b by R David Murray in branch '2.7':
bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
http://hg.python.org/cpython/rev/406c5b69cb1b
bitdancer commentedon Oct 1, 2011
This looks correct to me, and it tested out fine on the test suite (and the provided test failed without the provided fix), so I committed it.
I have a small concern that the change in output might be a bit radical for a bug fix release, but it does seem to me that this is clearly a bug. If people think it shouldn't go in the bug fix releases let me know and I'll back it out.
Thanks for the patch, Dan.
2 remaining items
dankenigsberg commentedon Oct 2, 2011
Oh dear.
Thanks, Enzio, for pointing out that former patch is wrong. It is also quite naive, since the whole NATURE of toprettyprint() is to add whitespace to Text nodes. Tomas Lee's http://bugs.python.org/file11832/minidom-toprettyxml-01.patch made an effort to touch only "simple" Text nodes, that are confined within a single <elem></elem>.
I did not expect http://bugs.python.org/file23286/minidom-Text-toprettyxml.patch to get in so quickly, after the former one spent several years on queue. However now is time to fix it, possible by my second patch.
dankenigsberg commentedon Oct 2, 2011
btw, http://www.w3.org/TR/xml/#sec-white-space is a bit vague on how should a parser deal with whitespace, and seems to allow non-preservation of text nodes. Preserving "simple" text nodes is allowed, too, and is more polite to applications reading the prettyxml.
bitdancer commentedon Oct 2, 2011
Heh, you happened to post your patch at a time when I wanted something to do as a break from something I didn't want to do...and I *thought* I understood the problem, after reading the various links. But clearly I didn't. We don't have someone who has stepped forward to be xml maintainer, so I just went ahead and committed it.
I should find time to look at your new patch some time today, or perhaps Ezio will have time. (Clearly minidom doesn't have enough tests.)
techtonik commentedon Nov 15, 2011
I would revert this patch (leaving several test cases though) until a proper fix is available.
bitdancer commentedon Nov 15, 2011
Yeah, I just haven't found time to do the revert yet (my first naive attempt using hg commands failed and I haven't found time to figure it out or do the reverse-patch method).
ezio-melotti commentedon Nov 15, 2011
Here is a new patch based on Dan's last patch.
Correct me if I'm wrong, but it seems to me that it's not possible for a node to have only text-nodes as children and yet have more than one child; i.e. you can't have two or more adjacent text nodes, because they would be considered as a single text node. I therefore changed the check with all() to check if there's only a child and if it's a text node.
I also added a test that checks where the \n and \t are added, because testing only that the DOM is preserved is not enough.
dankenigsberg commentedon Nov 15, 2011
Technically, adjacent Text nodes are not illegal, but preserving this oddity in pretty-print is impossible. It is perfectly fine to pretty-print only the simple case of len()==1.
ezio-melotti commentedon Nov 18, 2011
I did some tests, creating an element ('elem') that contains two adjacent text nodes ('text'). With my latest patch the prettyprint is:
<?xml version="1.0" ?>
<elem>
text
text
</elem>
Here both the text nodes are printed on a newline and they are indented.
With your patch it should be:
<?xml version="1.0" ?>
<elem>texttext</elem>
I'm not sure there's any reason to prefer the second option though.
python-dev commentedon Nov 18, 2011
New changeset 7262f8f276ff by Ezio Melotti in branch '2.7':
bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg.
http://hg.python.org/cpython/rev/7262f8f276ff
New changeset 5401daa96a21 by Ezio Melotti in branch '3.2':
bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg.
http://hg.python.org/cpython/rev/5401daa96a21
New changeset cb6614e3438b by Ezio Melotti in branch 'default':
bpo-4147: merge with 3.2.
http://hg.python.org/cpython/rev/cb6614e3438b
ezio-melotti commentedon Nov 18, 2011
I committed my patch with a few more tests. This should be fixed now.
Thanks for the report and the patch!