问题
While transforming XML to XML using XSLT I have included doctype in output XML. But a line break is created in the output XML doctype declaration.
XSLT:
<xsl:output method="xml" doctype-system="book3.dtd" doctype-public="-//Atypon//DTD test//EN" version="1.0" encoding="UTF-8" indent="no"/>
output XML after using XSLT:
<!DOCTYPE book
PUBLIC "-//Atypon//DTD test//EN" "book3.dtd">
Space and line break are created in between book and public in doctype declaration.
I need the doctype declaration in a single line.
Can anyone help me to do this?
回答1:
When you say you "need" it to be on one line, this means that you must be processing the resulting XML using a parser that attaches significance to things (like ignorable whitespace) that should be considered insignficant.
The correct solution to your problem is to process XML only with conformant XML parsers, then you won't need to try and tweak the serialized format to fit with the restrictions your non-conformant parser imposes.
However, the Saxon serializer is designed to be customized if you really have to. Call Configuration.setSerializerFactory()
to register a subclass of SerializerFactory
which overrides the newXMLEmitter()
method to create a subclass of XMLEmitter
, and in your subclass of XMLEmitter
, override the writeDocType()
method to format the DOCTYPE declaration in the way you want it.
来源:https://stackoverflow.com/questions/46171974/remove-space-in-between-doctype-in-xml-using-xslt