Enable selection of quoting style during etree.tostring

Bug #1800456 reported by Stdedos
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Won't Fix
Undecided
Unassigned

Bug Description

From https://stackoverflow.com/questions/46566216/writing-lxml-etree-with-double-quotes-header-attributes:

======================
I have created a basic xml tree using lxml tutorial:

```
from lxml import etree
root = etree.Element("root")
root.append( etree.Element("child1") )
child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")
print(etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True))
```
This produces the following:

```
<?xml version='1.0' encoding='UTF-8'?>
<root>
  <child1/>
  <child2/>
  <child3/>
</root>
```
My question is, how to produce xml file with double quoted file header, i.e.

```
<?xml version="1.0" encoding="UTF-8"?>
```
======================

The specific issue would be (master at-the-time: 68cf93c4827ea74e46d2aa6809011f96ed9c689a)
https://github.com/lxml/lxml/blob/68cf93c4827ea74e46d2aa6809011f96ed9c689a/src/lxml/serializer.pxi#L276-L285

However, it would be nice if every other instance of "quote style" would be configurable too, if any.

---------
## -- PLEASE PROVIDE THE FOLLOWING INFORMATION: --
Python : sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
lxml.etree : (4, 1, 1, 0)
libxml used : (2, 9, 7)
libxml compiled : (2, 9, 7)
libxslt used : (1, 1, 32)
libxslt compiled : (1, 1, 32)
## -----

Revision history for this message
scoder (scoder) wrote : Re: [Bug 1800456] [NEW] Enable selection of quoting style during etree.tostring

As the SO discussion suggests, the output is perfectly correct. It's the "quality gate" that needs fixing.

Even if there was a way in lxml to change the quoting, it would only apply to a few parts of the output, since most of the serialisation is done by libxml2, which does not have a way to configure it.

Writing your own declaration into a file before serialising the rest of the XML seems like a good enough work-around, though, that gives complete control over the spelling. I don't think there is anything to improve here.

scoder (scoder)
Changed in lxml:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.