-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-37534: Adding ability to add Standalone Document Declaration when generating XML documents #14912
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
bpo-37534: Adding ability to add Standalone Document Declaration when generating XML documents #14912
Changes from 7 commits
c37145a
4035046
e7b1c40
e8b1b18
d96fd7a
c291a64
3c8db77
edc492a
70a69a9
b603224
59a3495
3a0594b
162486a
e941fc1
5b8cee5
e108c3d
3b38bbe
51a5f01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,11 @@ class Node(xml.dom.Node): | |
| def __bool__(self): | ||
| return True | ||
|
|
||
| def toxml(self, encoding=None): | ||
| return self.toprettyxml("", "", encoding) | ||
| def toxml(self, encoding=None, standalone=None): | ||
| return self.toprettyxml("", "", encoding, standalone) | ||
|
|
||
| def toprettyxml(self, indent="\t", newl="\n", encoding=None): | ||
| def toprettyxml(self, indent="\t", newl="\n", encoding=None, | ||
| standalone=None): | ||
| if encoding is None: | ||
| writer = io.StringIO() | ||
| else: | ||
|
|
@@ -56,7 +57,7 @@ def toprettyxml(self, indent="\t", newl="\n", encoding=None): | |
| newline='\n') | ||
| if self.nodeType == Node.DOCUMENT_NODE: | ||
| # Can pass encoding only to document, to put it into XML header | ||
| self.writexml(writer, "", indent, newl, encoding) | ||
| self.writexml(writer, "", indent, newl, encoding, standalone) | ||
| else: | ||
| self.writexml(writer, "", indent, newl) | ||
| if encoding is None: | ||
|
|
@@ -1787,12 +1788,21 @@ def importNode(self, node, deep): | |
| raise xml.dom.NotSupportedErr("cannot import document type nodes") | ||
| return _clone_node(node, deep, self) | ||
|
|
||
| def writexml(self, writer, indent="", addindent="", newl="", encoding=None): | ||
| if encoding is None: | ||
| writer.write('<?xml version="1.0" ?>'+newl) | ||
| def writexml(self, writer, indent="", addindent="", newl="", encoding=None, | ||
| standalone=None): | ||
| # In case standalone declaration is set | ||
| if standalone is not None: | ||
| standalone = "standalone='{}'".format('yes' if standalone else 'no') | ||
|
||
| else: | ||
| writer.write('<?xml version="1.0" encoding="%s"?>%s' % ( | ||
| encoding, newl)) | ||
| standalone = '' | ||
|
|
||
| writer.write( | ||
| '<?xml version="1.0" {encoding}{standalone}?>{newline}'.format( | ||
|
||
| encoding="encoding=\"{}\"".format(encoding) if encoding else '', | ||
| standalone=standalone, | ||
| newline=newl) | ||
| ) | ||
|
|
||
| for node in self.childNodes: | ||
| node.writexml(writer, indent, addindent, newl) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1883,3 +1883,4 @@ Robert Leenders | |
| Tim Hopper | ||
| Dan Lidral-Porter | ||
| Ngalim Siregar | ||
| Henrik Harutyunyan | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| When using minidom module to generate XML documents the ability to add Standalone Document Declaration is added. | ||
| All the changes are made to generate a document in compliance with Extensible Markup Language (XML) 1.0 (Fifth Edition) W3C Recommendation (available here: https://www.w3.org/TR/xml/#sec-prolog-dtd). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both values should use the same quotes here. Since both the version and the encoding used double quotes, please also use double quotes for
standalone.Please also add a test that declares an encoding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, missed the test case with an encoding.
Added and the quote styles are made consistent either.