gpt4 book ai didi

java - Java 中的缩进 XML 是否应该遵守 `xml:space="保留“`?

转载 作者:行者123 更新时间:2023-12-01 14:27:51 26 4
gpt4 key购买 nike

我正在用 Java 美化/缩进一些 XML:

<div xml:space="default"><h1 xml:space="default">Indenting mixed content in Java</h1><p xml:space="preserve">Why does indenting mixed content (like this paragraph) add whitespace around <a href="http://www.stackoverflow.com" xml:space="preserve"><strong>this strong element</strong></a>?</p></div>

当我美化XML时,我不希望<a>的内容中添加空格。元素,所以我指定了 xml:space="preserve"希望变压器保留其中的空白。

但是,当我转换 XML 时,我得到:

<div>
<h1 xml:space="default">Indenting mixed content in Java</h1>
<p>Why does indenting mixed content (like this paragraph) add whitespace around <a href="http://www.stackoverflow.com">
<strong xml:space="preserve">this strong element</strong>
</a>?</p>
</div>

... <a> 之间有额外的空格和 <strong>元素。 (不仅如此,</a> 关闭标记与它的开放标记也笨拙地不一致。)

如何防止美化器添加空白?难道我做错了什么?这是我正在使用的 Java 代码:

import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import java.io.StringWriter;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;

public class XmlExample {

public static void main(String[] argv) {
Document xmlDoc = parseXml("<div xml:space=\"default\">" +
"<h1 xml:space=\"default\">Indenting mixed content in Java</h1>" +
"<p xml:space=\"preserve\">Why does indenting mixed content (like this paragraph) add whitespace around " +
"<a href=\"http://www.stackoverflow.com\" xml:space=\"preserve\"><strong>this strong element</strong></a>?" +
"</p>" +
"</div>");
String xmlString = xmlToString(xmlDoc.getDocumentElement());
System.out.println(xmlString);
}

public static Document parseXml(String xml) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
return doc;
}
catch(Exception e) {
throw new RuntimeException(e);
}
}

public static String xmlToString(Element el) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
DOMSource source = new DOMSource(el);
transformer.transform(source, new StreamResult(writer));
return writer.getBuffer().toString().trim();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}

}

最佳答案

如果您使用符合 XSLT 1.0 或 XSLT 2.0 规范的序列化程序,那么它应该尊重 xml:space (即,在 xml:space="preserve"的范围内,应该抑制缩进)。 XSLT 2.0 规范在这一点上比 XSLT 1.0 更加明确,并且使其成为“必须”而不是“应该”的要求。

您正在使用 JAXP 身份转换而不是 XSLT 转换; JAXP 规范中有对 XSLT 1.0 规范的引用,但它有点模糊。

如果您使用 Saxon,您应该会得到所需的行为。 Saxon 还允许您使用 SUPPRESS_INDENTATION 输出参数抑制特定元素的缩进,因此您甚至不必在正在序列化的文档中包含 xml:space。

关于java - Java 中的缩进 XML 是否应该遵守 `xml:space="保留“`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17053091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com