Skip to content
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

No way to emit doctype when writing an XML file #168

Open
lilyball opened this issue Apr 30, 2018 · 7 comments
Open

No way to emit doctype when writing an XML file #168

lilyball opened this issue Apr 30, 2018 · 7 comments

Comments

@lilyball
Copy link

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.

@netvl
Copy link
Owner

netvl commented May 16, 2018

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.

@brackleian
Copy link

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.

brackleian pushed a commit to brackleian/xml-rs that referenced this issue May 14, 2021
The 'characters' writer event could not be used to output text after
the document declaration, only before it or after the first XML element.
This change ensures that the document declaration will be output before
the text.  Arbitrary text can still be output at the start of the file,
if needed, by writing directly using the inner_mut method.

Addresses issue netvl#168.
@brackleian
Copy link

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.

netvl added a commit that referenced this issue Jul 29, 2021
Fix issue #168:  Force XML document declaration to be output before 'characters' event
@netvl
Copy link
Owner

netvl commented Jul 29, 2021

@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.

@brackleian
Copy link

brackleian commented Jul 29, 2021 via email

@netvl
Copy link
Owner

netvl commented Jul 30, 2021

@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.

@saona-raimundo
Copy link

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\">")?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants