I have a requirement to convert XML to HTML document. Am trying to convert XML to HTML with the help of XSLT.
In my XSLT i have few charaters like é & € while parsing into HTML document its giving below error.
The entity "eacute" was referenced, but not declareded for é.
The entity "euro" was referenced, but not declareded. for €
Please find my XSLT file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/receipt">
<html>
<head>
</head>
<body>
<table style="width: 100%;">
<tr>
<td style="text-align: center; width: 25%;">
</td>
<td style="font-size: 12px; vertical-align: top; width: 25%;">
46-68 Kléber<br/>
Tél: +1 <br/>
Fax: +1
</td>
<td style="font-size: 12px; vertical-align: top; width: 25%;">
725 € <br />
</td>
<td style="text-align: center; width: 25%;">Date <xsl:value-of select="date" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Am writting Java code for XML to HTML parsing. Please find my code below.
public static StringWriter getOrderAcknowledgementPDF(JSONObject obj) throws IOException, DocumentException
{
StringBuilder xml = new StringBuilder();
String header="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
StringBuilder sb = new StringBuilder();
sb.append(header);
JSONObject json = new JSONObject(obj.toString());
xml.append(header);
xml.append("\n");
xml.append("<delivery_receipt>");
xml.append(XML.toString(json));
xml.append("</delivery_receipt>");
System.out.println("XML--->"+xml);
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("D:/Phase2/UAT_Phase/Order_Acknowledgement/template.xsl")));
} catch (TransformerConfigurationException | TransformerFactoryConfigurationError e1) {
throw ExceptionUtils.handle((Exception) e1);
}
StringWriter sw = new StringWriter();
try {
transformer.transform(new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8"))), new StreamResult(sw));
} catch (TransformerException e) {
throw ExceptionUtils.handle(e);
}
return sw;
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String orderid=req.getParameter("item");
String reportname = "OrderAcknowledgement";
resp.setHeader("Content-Disposition", "attachment; filename=" + reportname + ".pdf");
StringWriter result = service.getOrderAcknowledgementPDF(orderid);
File file = File.createTempFile("Order_Acknowledgement-", ".pdf");
ITextRenderer renderer = new ITextRenderer();
try {
renderer.getFontResolver().addFont("C:/WINDOWS/FONTS/ARIAL.TTF", true);
} catch (DocumentException e) {
throw ExceptionUtils.handle(e);
}
String fileNameWithPath = "D:/" + "PDF-FromHtmlString.pdf";
String str=result.toString().replace("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">", "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
str=str.replace("<br>", "<br />");
System.out.println("after replacing....-->"+str);
XMLResource res = XMLResource.load(new StringReader("<html><head><style>@page { size : 45cm 30cm; }</style></head><body>"+str + "</body></html>"));
resp.setContentType("application/pdf");
org.w3c.dom.Document doc2 = res.getDocument();
renderer.setDocument(doc2, "file:/C:/");
renderer.layout();
if(fileNameWithPath != null)
{
resp.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileNameWithPath));
}
OutputStream browserStream = resp.getOutputStream();
renderer.createPDF(browserStream);
}
}
Am getting error in the below line :
XMLResource res = XMLResource.load(new StringReader("<html><head><style>@page { size : 45cm 30cm; }</style></head><body>"+str + "</body></html>"));
XMLResource res = XMLResource.load(new StringReader("<html><head><style>@page { size : 45cm 30cm; }</style></head><body>"+str + "</body></html>"));line.resultin your Java code declared and initialized? And it looks as if the error is not related to XSLT but rather occurs when you try to feed the HTML result of your XSLT transformation as XML toXMLResource. I don't know that class but its name suggests it is for parsing XML and not HTML.é&€characters without this characters am able to parse and PDF also generating without any issue)XMLResourcethen whether it allows to load HTML instead of XML if you want to go that route. Currently your XSLT creates HTML output and with thathtmloutput method the Transformer can produce the HTML entity references. Of course you can avoid the hassle of needing to convert the HTML output back to XML by simply using a stylesheet that uses<xsl:output method="xml"/>, perhaps additionally using the XHTML namespace<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">.