虽然漂亮的格式 XML <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> 更改此 header <?xml version="1.0" encoding="UTF-8"?>
while pretty formatting XML <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> changes to this header <?xml version="1.0" encoding="UTF-8"?>
我已经编写了代码来漂亮地格式化我的 XML。它工作正常,但 XML 编码为 ISO-8859-1,它更改为 encoding="UTF-8" 我不明白为什么有人能告诉我哪里出错了。
这是 XSL 文件
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是我在 java
中用于漂亮格式的代码片段
private void prettyFormat(File f) {
logger.info("Formatting file: " + f);
try {
File file = new File(outputPath.toString()+"/"+f.getName());
FileCopyUtils.copy(f, file);
File xslFile=null;
javax.xml.transform.Source xmlSource =new javax.xml.transform.stream.StreamSource(f);
StreamResult sr = new StreamResult(new File(outputPath.toString()+"/"+file.getName()));
Transformer transformer = getTransformer();
transformer.transform(xmlSource, sr);
} catch (Exception e) {
logger.error("Failed to format file " + f.getName(), e);
}
}
private Transformer getTransformer() throws TransformerConfigurationException {
File xslFile=null;
javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer trans = transFact.newTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");
if (System.getProperty(BASEDIR) == null || isRunningInTests()) {
xslFile = new File("assembly" + File.separator + "etc" + File.separator + "strip-space.xsl");
} else{
xslFile = new File(getBaseDir() + File.separator + "etc" + File.separator + "strip-space.xsl");
}
logger.info("The path of file:::: " + xslFile);
trans = transFact.newTransformer(
new StreamSource(xslFile));
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("indent", "yes");
return trans;
}
请问是我设置错了属性还是别的什么
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
更改此 header <?xml version="1.0" encoding="UTF-8"?>
但我想要相同的 <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
最好在 XSLT 中直接指定需要的内容。
您可以尝试修改xsl:output
如下:
<xsl:output method="xml" indent="yes" standalone="no" encoding="ISO-8859-1" />
我已经编写了代码来漂亮地格式化我的 XML。它工作正常,但 XML 编码为 ISO-8859-1,它更改为 encoding="UTF-8" 我不明白为什么有人能告诉我哪里出错了。
这是 XSL 文件
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是我在 java
中用于漂亮格式的代码片段 private void prettyFormat(File f) {
logger.info("Formatting file: " + f);
try {
File file = new File(outputPath.toString()+"/"+f.getName());
FileCopyUtils.copy(f, file);
File xslFile=null;
javax.xml.transform.Source xmlSource =new javax.xml.transform.stream.StreamSource(f);
StreamResult sr = new StreamResult(new File(outputPath.toString()+"/"+file.getName()));
Transformer transformer = getTransformer();
transformer.transform(xmlSource, sr);
} catch (Exception e) {
logger.error("Failed to format file " + f.getName(), e);
}
}
private Transformer getTransformer() throws TransformerConfigurationException {
File xslFile=null;
javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer trans = transFact.newTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");
if (System.getProperty(BASEDIR) == null || isRunningInTests()) {
xslFile = new File("assembly" + File.separator + "etc" + File.separator + "strip-space.xsl");
} else{
xslFile = new File(getBaseDir() + File.separator + "etc" + File.separator + "strip-space.xsl");
}
logger.info("The path of file:::: " + xslFile);
trans = transFact.newTransformer(
new StreamSource(xslFile));
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("indent", "yes");
return trans;
}
请问是我设置错了属性还是别的什么
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
更改此 header <?xml version="1.0" encoding="UTF-8"?>
但我想要相同的 <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
最好在 XSLT 中直接指定需要的内容。
您可以尝试修改xsl:output
如下:
<xsl:output method="xml" indent="yes" standalone="no" encoding="ISO-8859-1" />