如何规范
;和;在java中创建xml元素时
我正在使用org.w3c.dom.Document
和在创建文本节点时构建xml
Document xmlDocument;//This has document
Node xmlNode = Document xmlDocument.getDocumentElement();
xmlNode.appendChild(getDocument().createTextNode(value));
如果文本值具有"abc\r\n"
,则插入\r
xml字符代码" "
以代替\r
。因此,在xml上应用xslt转换时
[Fatal Error] :103:320: Character reference " " is an invalid XML character.
同样,我对""
这个字符也有疑问,下面是我的xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template
match="*[not(descendant-or-self::*[text()[normalize-space()] | @*])]">
<!-- process element and descendants in mode for debugging -->
<xsl:apply-templates select="." mode="debug" />
</xsl:template>
<xsl:template match="*" mode="debug">
<xsl:message terminate="no">
Deleting
<xsl:value-of select="local-name()" />
.
</xsl:message>
<xsl:apply-templates select="*" mode="debug" />
</xsl:template>
</xsl:stylesheet>
我还有另一个问题,在生成xml之后,它将从子元素中删除名称空间声明,如:
<head xmlns:test="">
<body>
<p-elm xmlns:xyz=""></p-elm>
</body>
从<p-elm>
删除xml命名空间
任何建议将被告知:)
应用转换的一段代码:
DOMSource domSource = new DOMSource(getDocument());
TransformerFactory tf = TransformerFactory.newInstance();
if(source != null){
//Templates template = tf.newTemplates(source);
Templates template = tf.newTemplates(new StreamSource(this.getClass().getResourceAsStream(source)));
serializer = template.newTransformer();
}else{
serializer = tf.newTransformer();
}
if (outputProperties != null) {
Iterator iter = outputProperties.entrySet().iterator();
while (iter.hasNext()) {
Entry entry = (Entry) iter.next();
serializer.setOutputProperty((String) entry.getKey(), (String) entry.getValue());
}
}
//XmlUtil.prettyFormat(reader, writer);
serializer.transform(domSource, streamResult);
这里source
是xslt path
# 1 楼答案
根据XML specification (section 2.2),}字符,即使在Java中使用

不是有效的XML字符,但
是有效的,不应给出错误。这意味着你不能放任何东西 ^XML文档中的{\2
字符是有效的有效XML字符的定义为:
是一个回车符(与XML中的
或Java中的\r
相同)我试着运行你的代码,看看会发生什么,但它没有编译,而且它丢失了太多的部分,我无法用手轻松修复