通过java进行xslt转换时出现异常
正如您所看到的,我现在在下面的java类中使用xstream rite 在下面的java类中,我从对象生成xml通过java进行xslt转换时出现异常,java,xml,xslt,xalan,Java,Xml,Xslt,Xalan,正如您所看到的,我现在在下面的java类中使用xstream rite 在下面的java类中,我从对象生成xml InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage(); invoiceReferenceNotificationMessage.setInvoiceReference("SM/829709/0
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("SM/829709/0315");
invoiceReferenceNotificationMessage.setABSReference("IRMAR157311");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(2546);
invoiceReferenceNotificationMessage.setPaidAmount(1245);
invoiceReferenceNotificationMessage.setBalanceAmount(0);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abc");
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage1.setInvoiceReference("SM/15");
invoiceReferenceNotificationMessage1.setABSReference("I157311");
invoiceReferenceNotificationMessage1.setCurrency("EUR");
invoiceReferenceNotificationMessage1.setInvoiceAmount(255546);
invoiceReferenceNotificationMessage1.setPaidAmount(125545);
invoiceReferenceNotificationMessage1.setBalanceAmount(0);
invoiceReferenceNotificationMessage1.setValueDate(new Date());
invoiceReferenceNotificationMessage1.setRemarks("abERRc");
Mail m = new Mail();
String xslFilePath1 = "C:\\xsl\\rok.xsl";
m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage);
XStream xstream = new XStream();
xstream.alias("arokermail",Mail.class);
String abc = xstream.toXML(m);
m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
xstream.alias("arokermail",Mail.class);
String def = xstream.toXML(m);
String t =abc+def;
因此,上面的代码生成以下xml
<arokermail>
<invoiceReferenceNotificationMessage>
<InvoiceReference>SM/829709/0315</InvoiceReference>
<ABSReference>IRMAR157311</ABSReference>
<Currency>GBP</Currency>
<InvoiceAmount>2546.0</InvoiceAmount>
<PaidAmount>1245.0</PaidAmount>
<BalanceAmount>0.0</BalanceAmount>
<ValueDate>2015-05-22 15:44:14.741 IST</ValueDate>
<Remarks>abc</Remarks>
</invoiceReferenceNotificationMessage>
</arokermail><arokermail>
<invoiceReferenceNotificationMessage>
<InvoiceReference>SM/15</InvoiceReference>
<ABSReference>I157311</ABSReference>
<Currency>EUR</Currency>
<InvoiceAmount>255546.0</InvoiceAmount>
<PaidAmount>125545.0</PaidAmount>
<BalanceAmount>0.0</BalanceAmount>
<ValueDate>2015-05-22 15:44:14.741 IST</ValueDate>
<Remarks>abERRc</Remarks>
</invoiceReferenceNotificationMessage>
</arokermail>
现在请建议如何在生成的xml上应用此xsl生成的xml存储在名为t的字符串变量中
String t =abc+def;
下面是我通过调用函数和传递参数所做的尝试
String t =abc+def; //generated xml
String xslFilePath1 = "C:\\xsl\\rok.xsl";";
String outputhtml =null;
try
{
st.transform(t, xslFilePath1, outputhtml);
}
catch (TransformerConfigurationException e)
{
System.err.println("TransformerConfigurationException");
System.err.println(e);
}
catch (TransformerException e)
{
System.err.println("TransformerException");
System.err.println(e);
}
正在调用的函数如下所示
public void transform(String dataXML, String inputXSL, String outputHTML)
throws TransformerConfigurationException, TransformerException
{
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslStream = new StreamSource(inputXSL);
Transformer transformer = factory.newTransformer(xslStream);
StreamSource in = new StreamSource(dataXML);
StreamResult out = new StreamResult(outputHTML);
transformer.transform(in, out);
System.out.println("The generated HTML file is:" + outputHTML);
}
the error that i am getting is ..
TransformerException
javax.xml.transform.TransformerException: Result object passed to ''{0}'' is invalid.
我已经更新了xsl,所以我的rok.xsl如下所示
String xslFilePath1 = "C:\\xsl\\rok.xsl";
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
Hello,
<br>
</br>
<br>
</br>
Please be advised of the following payment details
<font color="black" face="Arial" size="2" >
<br>
<br>
<table border="1" cellpadding="3" cellspacing="0">
<tr bgcolor="lightblue">
<td nowrap="nowrap">Invoice Reference</td>
<td nowrap="nowrap">ABSReference</td>
<td nowrap="nowrap">Currency</td>
<td nowrap="nowrap">Invoice Amount</td>
<td nowrap="nowrap">Paid Amount</td>
<td nowrap="nowrap">Balance Amount</td>
<td nowrap="nowrap">Value Date</td>
<td nowrap="nowrap">Remarks</td>
</tr>
<xsl:for-each select="arokermail/invoiceReferenceNotificationMessage">
<tr>
<td nowrap="nowrap"><xsl:value-of select="InvoiceReference" /></td>
<td nowrap="nowrap"><xsl:value-of select="ABSReference" /></td>
<td nowrap="nowrap"><xsl:value-of select="Currency" /></td>
<td nowrap="nowrap"><xsl:value-of select="InvoiceAmount" /></td>
<td nowrap="nowrap"><xsl:value-of select="PaidAmount" /></td>
<td nowrap="nowrap"><xsl:value-of select="BalanceAmount" /></td>
<td nowrap="nowrap"><xsl:value-of select="ValueDate" /></td>
<td nowrap="nowrap"><xsl:value-of select="Remarks" /></td>
</tr>
</xsl:for-each>
<TR>
</TR>
</table>
</br>
</br>
<p>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
</br>
</br>
</p>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
你好
请注意以下付款详情
发票参考
ABSReference
通货
发票金额
已付金额
余额
起息日
评论
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
由
String t =abc+def; //generated xml
它的格式不好。特别是,您通过连接两个现有XML文档创建了一个文档,因此它没有一个根节点。我不太清楚您想要实现什么(可能是转换每个XML文档,然后连接),但是您将两个文档连接起来肯定是无效的。问题在于
StreamResult
的构造函数中,您正在传递null
。这就是它所期望的
public StreamResult(String systemId)
Construct a StreamResult from a URL.
Parameters:
systemId - Must be a String that conforms to the URI syntax.
您应该使用另一个构造函数。如果您的xml和xsl是正确的(并且t包含您的xml),那么这应该是可行的。请注意,StringBufferInputStream
已被弃用,但它是一个快速而肮脏的解决方案):
你能不能先说明一下请帮我更正一下xml,因为它缺少的东西每一半生成的xml看起来格式都很好,但如果将两个文档加在一起,就会得到一些非xml的东西,因为它没有一个顶级节点。谢谢,那么,请告诉我如何组合并生成完整的xml。在这种情况下,您需要查看XSL并设计XSL期望的文档。我的第一个问题是,XSL希望文档的根节点是什么。这将给你一个关于如何使用itI的线索我已经更新了my arok.xsl,请你查看更新的帖子谢谢Vancethanks,请您在上面的代码中进行更改,以便我能提前了解更多感谢您能突出显示引发该异常的特定行吗?TransformerException javax.xml.transform.TransformerException:传递给{0}的结果对象无效。这是我能看到的唯一异常,您能告诉我如何通过xalan进行xsl转换吗?还有其他方法吗way@BrianAgnew是否可以使用任何其他可能的方法进行xsl转换请建议您需要突出显示引发异常的代码中的特定行。也就是说,它在哪条线上爆炸?
TransformerFactory factory = TransformerFactory.newInstance();
//File object with path to your xsl file
StreamSource xslStream = new StreamSource(new File("C:\\xsl\\rok.xsl"));
Transformer transformer = factory.newTransformer(xslStream);
//Read xml from string
StreamSource in = new StreamSource(new StringBufferInputStream(t));
//Prints to stdout
StreamResult out = new StreamResult(System.out);