Java Transformer class 在转换时复制一些 xml 节点属性

Java Transformer class duplicates some xml node attributes when transforming

我在 Java 中创建了这个简单的函数:

public static String prettify(Document xml) {
   String resultValue;
   StreamResult xmlOutput;

    try {
        StringWriter stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer(); 
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

        Source source = new DOMSource(xml);

        transformer.transform(source, xmlOutput); 
        resultValue = xmlOutput.getWriter().toString();
    } catch (Exception e) {
        resultValue = "";
    }
    return resultValue;
}

这是一个非常简单的函数,所以我可以得到一个缩进 XML。问题是,对于这种特定情况,它在文档元素处复制了属性……很奇怪。这个案例是一个 XML 文档,其中包含 XML:

<?xml version='1.0'?><Document xmlns='urn:iso:std:iso:20022:tech:xsd:pain.008.001.02' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><CstmrDrctDbtInitn><GrpHdr>A</GrpHdr></CstmrDrctDbtInitn></Document>

我得到:

"<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <CstmrCdtTrfInitn>
      <GrpHdr>
        A
      </GrpHdr>
 </CstmrCdtTrfInitn>

如您所见, 的两个属性都是重复的!我试过使用输出属性,但什么也没有。 例如,如果我向 中添加另一个属性,比如 "test='whatever'",则该特定属性不会重复。 任何帮助将不胜感激

感谢@forty-two 的支持。我从 http://ftp.cixug.es/apache/xalan/xalan-j/binaries/ 下载了最新版本的 Xalan (2.7.2) 并添加了库:

serializer.jar
xalan.jar
xercesImpl.jar
xml-apis.jar

到我的项目,现在一切都按预期工作。