Skip to content

BUG: Add decl when xml_declaration=True and pretty_print=False for et… #40162

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

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pandas/io/formats/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ def build_tree(self) -> bytes:
if self.pretty_print:
self.out_xml = self.prettify_tree()

if not self.xml_declaration:
if self.xml_declaration:
self.out_xml = self.add_declaration()
else:
self.out_xml = self.remove_declaration()

if self.stylesheet is not None:
Expand Down Expand Up @@ -416,6 +418,23 @@ def prettify_tree(self) -> bytes:

return dom.toprettyxml(indent=" ", encoding=self.encoding)

def add_declaration(self) -> bytes:
"""
Add xml declaration.

This method will add xml declaration of working tree. Currently,
xml_declaration is supported in etree starting in Python 3.8.
"""
decl = f'<?xml version="1.0" encoding="{self.encoding}"?>\n'

doc = (
self.out_xml
if self.out_xml.startswith(b"<?xml")
else decl.encode(self.encoding) + self.out_xml
)

return doc

def remove_declaration(self) -> bytes:
"""
Remove xml declaration.
Expand Down
14 changes: 8 additions & 6 deletions pandas/tests/io/xml/test_to_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,7 @@ def test_xml_declaration_pretty_print():
assert output == expected


@td.skip_if_no("lxml")
def test_no_pretty_print_with_decl():
def test_no_pretty_print_with_decl(parser):
expected = (
"<?xml version='1.0' encoding='utf-8'?>\n"
"<data><row><index>0</index><shape>square</shape>"
Expand All @@ -879,7 +878,7 @@ def test_no_pretty_print_with_decl():
"</row></data>"
)

output = geom_df.to_xml(pretty_print=False, parser="lxml")
output = geom_df.to_xml(pretty_print=False, parser=parser)
output = equalize_decl(output)

# etree adds space for closed tags
Expand All @@ -889,8 +888,7 @@ def test_no_pretty_print_with_decl():
assert output == expected


@td.skip_if_no("lxml")
def test_no_pretty_print_no_decl():
def test_no_pretty_print_no_decl(parser):
expected = (
"<data><row><index>0</index><shape>square</shape>"
"<degrees>360</degrees><sides>4.0</sides></row><row>"
Expand All @@ -900,7 +898,11 @@ def test_no_pretty_print_no_decl():
"</row></data>"
)

output = geom_df.to_xml(xml_declaration=False, pretty_print=False)
output = geom_df.to_xml(xml_declaration=False, pretty_print=False, parser=parser)

# etree adds space for closed tags
if output is not None:
output = output.replace(" />", "/>")

assert output == expected

Expand Down