通过java进行xslt转换时出现异常 [英] getting exception while doing xslt transformation through java
问题描述
我有下面的 java 类,我现在正在使用 xstream rite,如您所见在下面的 java 类中,我从对象中生成 xml
I have the below java class in which i am using xstream rite now as you can see that in the below java class i am generating xml from the object
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
so the above code generates the following 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>
现在我还创建了一个 xsl,我将其存储在名为 rok.xsl 的计算机中,我想将其应用于生成的 xml 作为 xsl 转换的一部分,因此我将下面的 xsl 存储在一个变量中,如下所示..
now i have also created an xsl which i have stored in my computer named rok.xsl which i want to apply on generated xml as a partof xsl transformation so the below xsl i have stored in a variable as shown below..
String xslFilePath1 = "C:\\xsl\\rok.xsl";
现在请告知如何在生成的 xml 上应用此 xsl 生成的 xml 存储在名为 t 的字符串变量中;
now please advise how to apply this xsl on generated xml the generated xml is stored in a string variable named t ;
String t =abc+def;
下面是我通过调用函数和传递参数所尝试的
below is what i have tried by calling a function and passing parameters
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 如下所示..
i have updated the xsl so my rok.xsl is like shown below..
<?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>
推荐答案
问题出在 StreamResult
的构造函数中,你在那里传递了 null
.这就是它所期望的
The problem is in the constructor of StreamResult
, you are passing null
there. And this is what it expects
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
已被弃用,但可以作为一种快速而肮脏的解决方案):
You should use another constructor. This should work, if your xml and xsl is correct (and t contains your xml). Note that StringBufferInputStream
is deprecated but works as a quick-and-dirty solution):
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);
这篇关于通过java进行xslt转换时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!