- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过简单的代码解析和重写简单的 xml,会发生一些奇怪的事情
输入:
<html>
<input></input>
</html>
给出输出(格式不正确):
<html>
<input>
</html>
同样的事情发生在 <input/> 或
。
它不会出现在
代码很经典:
// READ XML
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new StringReader(_xml_source)));
// WRITE XML
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(document), new StreamResult(buffer));
String output = buffer.toString();
这是一个已知错误吗?
最佳答案
XSLT 定义了一个 output method , 可以是 xml
, html
, 或 text
.
规范说默认的输出方式应该是html
如果根节点是 <html>
, 否则应该是 xml
.
随着 xml
方法,你会得到<input/>
.
随着 html
方法,你会得到<input>
, 因为 HTML specification这么说。
如果需要,您可以明确给出输出方法:
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
这样一个带有 <html>
的文档根节点将输出 XML,即 <input/>
.
引述
The default for the
method
attribute is chosen as follows. If
- the root node of the result tree has an element child,
- the expanded-name of the first element child of the root node (i.e. the document element) of the result tree has local part
html
(in any combination of upper and lower case) and a null namespace URI, and- any text nodes preceding the first element child of the root node of the result tree contain only whitespace characters,
then the default output method is
html
; otherwise, the default output method isxml
. The default output method should be used if there are noxsl:output
elements or if none of thexsl:output
elements specifies a value for themethod
attribute.
Some HTML element types have no content. For example, the line break element
BR
has no content; its only role is to terminate a line of text. Such empty elements never have end tags. The document type definition and the text of the specification indicate whether an element type is empty (has no content) or, if it can have content, what is considered legal content.
关于java - TransformerFactory 破坏 <html> 标签内的 <input> 和 <br> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35898677/
我们的 CMS 将换行符输出为 (愚蠢,我知道,但在语法上是正确的(?)) 这转换为 在 chrome 和 IE10 中到 在火狐中。 所有浏览器都将其显示为两个换行符。 为什么不是翻译成或者只是
我正在尝试将一堆 HTML 文档转换为 XML 合规性(通过 java 方法),并且有很多 (1) 未封闭或 (2) 包含属性的标签。由于某种原因,我使用的正则表达式无法处理包含属性的标签。这是代码
如何转换字符串 "" to "" using java's String.replaceAll(String, String) method? 我都试过了: str.replaceAll("+
这个问题的答案是 community effort 。编辑现有答案以改进这篇文章。它目前不接受新的答案或交互。 我试过检查 other answers ,但我仍然感到困惑——尤其是在看到 W3scho
问题是我无法通过生产者脚本将消息发送到集群内的任何代理。 该设置是一个单一的 Zookeeper 服务器,使用默认设置在 IP (192.168.10.2:2181) 上运行。 此外,还有 3 个代理
我正在尝试使用 python 和 Beautiful Soup 4 用新行替换某些 html 中的每个中断标记。 该文档有 , 和标签,但由于 Beautiful Soup 处理标签的方式,每当它找到
我正在使用带有模板工具包的 perl 生成网页,我在 HTML 文件上更改了一些 JS 代码,编辑后有 显示在 UI 上,我不知道如何,奇怪的是没有 源码中,只有在Inspect element On
这是 HTML 页面的完整源代码: one two three four 谁能解释一下为什么我在IE8或chrome浏览页面时,“三”和“四”之间多了一个空行? 我认为标准应该让所有
我正在使用 并且它按预期在 chrome 和 IE 中正常工作,但在 firefox 中结果很奇怪。当我在 firefox 中看到代码时,它替换了 与 . 我还在某处读到替代语法 XML 允许在许
我有将 BBCode 替换为 html 的代码,当我想替换标签时出现问题 或 [br /]在 [pre=html] code [/pre] 内. Regex exp; string str; str
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
我正在使用这个(jQuery)来替换所有 与清除验证错误: $("").replaceAll(""); 但它不会减少任何验证错误。验证器是否检查原始来源? 最佳答案 JQuery 仅在文档呈现后才
我想获取 break html 中的文本,但这不是用段落换行,只是break内的文本。 Division 是我唯一的标识符,它是“Msg”。我怎样才能获得每个元素之一? H
Hood switch - (automatic transmission only).<br><br>Hood Switch (2013 CX-5 - not requir
我想选择 br 后面没有跟另一个 br 的元素。 例如。 sometextadsf 我想选择这两个元素。 sometextadsf 我只想选择 2 个 br 元素,而不是 4 个。 我看过这个问题,但
这个问题在这里已经有了答案: HTML 5: Is it , , or ? (18 个答案) 关闭 9 年前。 当我开始从 w3schools 学习 HTML 时,他们正在使用这个 .当我开始学习
我是 nagios 新手,我已经在我的 Linux 机器上安装了 nagios 3。 我想安装 nagios check_procs 插件。有人可以建议我吗。谢谢 最佳答案 您可以从依赖于您使用的 L
我有一个段落有一些单一的 和双 . 我想添加所有单个 , 但不是双 . 这怎么可能? 如果我用 p br {display:none} 它隐藏了所有的 br 标签。但是,当我有两个 br 时,我希
之间的主要区别是什么?和 在 html 标记中?有人可以解释两者的主要区别,因为这让我感到困惑。预先感谢您的所有评论。 :) 最佳答案 在实践中,不存在。就或 . 但是,区别在于位置,并且对所有
我如何使用JavaScript来检测 成为一个 ? 我试过: jQuery('body').html().replace(/(\\r\n){3, }/g,"\n"); 但这对我不起作用。 最佳
我是一名优秀的程序员,十分优秀!