Remove space in between doctype in XML using XSLT

房东的猫 提交于 2019-12-13 03:17:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!