通过java在xslt转换中获取错误

我正在尝试一个xsl转换,现在由一个java对象生成一个中间XML,并且再次传递XML以生成一个新的XML,而在xsl转换中,我收到错误,让我告诉你在我的下面我试过的一段代码。

下面是我的类,首先我创建对象,然后通过xstream生成中间XML

InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage(); invoiceReferenceNotificationMessage.setInvoiceReference("SM/8315"); invoiceReferenceNotificationMessage.setABSReference("IRMA5456R157311"); invoiceReferenceNotificationMessage.setCurrency("GBP"); invoiceReferenceNotificationMessage.setInvoiceAmount(255646); invoiceReferenceNotificationMessage.setPaidAmount(1246565); invoiceReferenceNotificationMessage.setBalanceAmount(0); invoiceReferenceNotificationMessage.setValueDate(new Date()); invoiceReferenceNotificationMessage.setRemarks("abc"); InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage(); invoiceReferenceNotificationMessage1.setInvoiceReference("SM/1655"); invoiceReferenceNotificationMessage1.setABSReference("I1575656311"); invoiceReferenceNotificationMessage1.setCurrency("EUR"); invoiceReferenceNotificationMessage1.setInvoiceAmount(25655546); invoiceReferenceNotificationMessage1.setPaidAmount(1255565645); invoiceReferenceNotificationMessage1.setBalanceAmount(0); invoiceReferenceNotificationMessage1.setValueDate(new Date()); invoiceReferenceNotificationMessage1.setRemarks("abERRc"); Transformer xsltTransformer = null; Mail m = new Mail(); m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage); XStream xstream = new XStream(); xstream.alias("brokermail",Mail.class); String abc = xstream.toXML(m); //System.out.println(abc); m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1); xstream.alias("brokermail",Mail.class); String def = xstream.toXML(m); //System.out.println(def); String t =abc+def; System.out.println(t); 

上面的代码生成以下XML

    SM/82973409/0315 IRMAR43157311 GBP 25446.0 12435.0 0.0 2015-05-21 21:58:04.439 IST abc    SM/13435 I157343411 EUR 2554546.0 12534545.0 0.0 2015-05-21 21:58:04.439 IST abERRc   

现在我必须通过传递上面生成的XML然后生成一个我正在通过xalan进行的新XML来进行xsl转换。 下面是我尝试但我的最终XML没有生成,因为我收到错误请告知如何克服这个,我使用的是xalan api

 System.out.println("******%%%%%%%%%%%%%*************"); String t =abc+def; System.out.println(t); Source msgStreamSource = null; ByteArrayInputStream byteStream = new ByteArrayInputStream(t.getBytes()); BufferedInputStream buffMsg = new BufferedInputStream(byteStream); msgStreamSource = new StreamSource(buffMsg); Transformer xsltTransformer1 = null; StringWriter strWriter = new StringWriter(); // Result stream Result xmlOutput = new StreamResult(strWriter); // String to hold transformed xml String transformedMessage = null; TransformerFactory transFact = TransformerFactory.newInstance(); try { //*******#########****** error is on this below line ********###########*********** xsltTransformer1 = transFact.newTransformer(msgStreamSource); //*******#########****** error is on this below line ********###########*********** // perform XSL transformation xsltTransformer1.transform(msgStreamSource, xmlOutput); // get the transformed xml transformedMessage = strWriter.toString(); } finally { strWriter.close(); } System.out.println(transformedMessage); } 

我得到的错误请告知如何克服这个问题

 [Fatal Error] :12:15: The markup in the document following the root element must be well-formed. ERROR: 'The markup in the document following the root element must be well-formed.' FATAL ERROR: 'Could not compile stylesheet' Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source) 

您将XML(不是具有2个根元素的有效文档)传递给newTransformer ,而不是传递XSLT样式表。 编译器甚至没有抱怨该文档不是有效的样式表,它抱怨它是格式错误的XML(XSLT样式表本身就是一个XML文档)。 我担心你必须阅读TransformerFactory 文档 。