使用 XSLT 在 XML 中删除文档类型之间的空格 [英] Remove space in between doctype in XML using XSLT

查看:66
本文介绍了使用 XSLT 在 XML 中删除文档类型之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 XSLT 将 XML 转换为 XML 时,我在输出 XML 中包含了 doctype.但是在输出 XML 文档类型声明中创建了一个换行符.

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"/>

使用 XSLT 后输出 XML:

output XML after using XSLT:

<!DOCTYPE book
  PUBLIC "-//Atypon//DTD test//EN" "book3.dtd">

在文档类型声明中,在 book 和 public 之间创建了空格和换行符.

Space and line break are created in between book and public in doctype declaration.

我需要一行中的 doctype 声明.

I need the doctype declaration in a single line.

谁能帮我做这件事?

推荐答案

当您说您需要"将它放在一行上时,这意味着您必须使用对事物赋予重要性的解析器来处理生成的 XML(像可忽略的空格)应该被认为是无关紧要的.

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.

问题的正确解决方案是仅使用符合规范的 XML 解析器处理 XML,然后您就无需尝试调整序列化格式以适应不符合规范的解析器强加的限制.

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.

但是,Saxon 序列化程序可以在您确实需要时进行自定义.调用 Configuration.setSerializerFactory() 注册 SerializerFactory 的子类,该子类覆盖 newXMLEmitter() 方法以创建 XMLEmitter<的子类/code>,并在您的 XMLEmitter 子类中,覆盖 writeDocType() 方法以按照您希望的方式格式化 DOCTYPE 声明.

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.

这篇关于使用 XSLT 在 XML 中删除文档类型之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆