New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xml.dom.minidom toprettyxml: omit whitespace for text-only elements #48397
Comments
For XML elements containing only text data, it would be nice if <person> Becomes: <person> From what I understand the handling of whitespace within XML elements is |
Also needed here. While pretty-printing should be able to insert |
To clarify: ... it should never alter the content of (i.e. insert whitespace into) |
Could you please apply that patch? People are starting to use non-standard libraries to process xml files because of this issue for example this man is using lxml or pyxml:
http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/ Is there any problem with that patch? |
@thomas: could you provide a unit test to go with your patch. |
This one is bug. |
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. |
New changeset 086ca132e161 by R David Murray in branch '3.2': New changeset fa0b1e50270f by R David Murray in branch 'default': |
New changeset 406c5b69cb1b by R David Murray in branch '2.7': |
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. |
The patch seems wrong to me:
>>> d = minidom.parseString('<foo><bar>AAA</bar>BBB<bar>CCC</bar></foo>')
>>> print(d.toprettyxml())
<?xml version="1.0" ?>
<foo>
<bar>AAA </bar>
BBB <bar>CCC </bar>
</foo> Even if the newlines are gone, the indentation before the closing tag is preserved. Also a newline is added before the text node BBB. It would be good to check what the XML standard says about the whitespace. I'm pretty sure HTML has well defined rules about it, but I don't know if that's the same for XML. FWIW the link in msg102247 contains a different fix (not sure if it's any better), and also a link to an article about XML and whitespace: http://www.oracle.com/technetwork/articles/wang-whitespace-092897.html (the link seems broken in the page). |
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. |
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.) |
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. |
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: Here both the text nodes are printed on a newline and they are indented. With your patch it should be: I'm not sure there's any reason to prefer the second option though. |
New changeset 7262f8f276ff by Ezio Melotti in branch '2.7': New changeset 5401daa96a21 by Ezio Melotti in branch '3.2': New changeset cb6614e3438b by Ezio Melotti in branch 'default': |
I committed my patch with a few more tests. This should be fixed now. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: