xml-rs
xml-rs copied to clipboard
No way to emit doctype when writing an XML file
There doesn't appear to be any way to emit a doctype when writing an XML file. XmlEvent
doesn't contain any way to represent one. This is a problem for me.
#133 talks about supporting doctypes already, but that's from the perspective of parsing. Adding support for emitting them should be a lot simpler.
As I see it, changes in parsing and writing should be symmetric, because the respective data structures (writer::XmlEvent and reader::XmlEvent) are supposed to be convertible to each other. For now, I've implemented the inner()
method which was discussed in another issue; I suppose it could help with your use case.
It's easy enough to write the doctype element using the XmlEvent::characters
method. However, it is then emitted before the start-document element, whereas it should be afterwards, but before any other element. Using flush
has no effect because it works on the underlying File rather than the Emitter.
I've pushed a one-line change that causes the document declaration to be output before any text is output using the XmlEvent::characters
method. However, this highlights another problem: the Writer
configuration can't be changed once the Writer
is created. Creating a new Writer
resets the internal state, and you get an extra document declaration in the output. Avoiding this requires altering the write_document_declaration
value in the new configuration. All a bit kludgy IMHO.
@brackleian your change is released in 0.8.4. Unfortunately I don't think I'll be able to work on solving the entire issue, since I don't have capacity to work on xml-rs anymore.
Hi Vladimir,
On Wed, 28 Jul 2021 21:10:02 -0700 Vladimir Matveev @.***> wrote:
@brackleian your change is released in 0.8.4. Unfortunately I don't think I'll be able to work on solving the entire issue, since I don't have capacity to work on xml-rs anymore.
Many thanks for that, 0.8.4 works well.
So you won't mind if I hack around with it some more, to try and get a better fix? :)
Cheers, Austin.
@brackleian I don't mind, but unfortunately I can't tell when I'll be able to take a look at any changes and merge them.
Overall, I feel that a better rewrite of the whole thing is needed, and that's what I started in one of the branches, but it is clear to me that I won't be able to finish it in foreseeable future.
Should a note in the README be added about how to add the doctype declaration?
I am doing as follows.
let mut w = EventWriter::new(file);
w.write(XmlEvent::StartDocument {
version: xml::common::XmlVersion::Version10,
encoding: Some("UTF-8"),
standalone: Some(false),
})?;
write!(w.inner_mut(), "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">")?;