【发布时间】:2018-02-16 02:42:23
【问题描述】:
我已经使用 XML 转换来创建 HTML,并且我正在使用 XML worker 将 html 转换为 PDF。我收到 Invalid nested tag head found, expected closing tag img 错误,因为通过 XML 呈现创建的 HTML 没有关闭 META、IMG 和 HR 标签。请您帮我处理 XML 代码更改,以便我可以对 IMG、META 和 HR 进行关闭标记。
使用的 XML。
Source xml = new StreamSource(new File("C:\\Reports\\ERseouce.xml"));
Source xslt = new StreamSource("C:\\Reports\\ExecutionReport_source.xsl");
StringWriter sw = new StringWriter();
FileWriter fw = new FileWriter("C:\\Reports\\ExecutionReport_source.html");
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer trasform = tFactory.newTransformer(xslt);
trasform.transform(xml, new StreamResult(sw));
fw.write(sw.toString());
fw.close();
xslt 代码
<xsl:template match="ROOT">
<html>
<head>
<img style="float: left;" src="logo.gif" alt="logo" />
<h1 style="font-size:60px;text-align: center;">Test Execution
Report</h1>
</head>
<body>
<table>
<tr>
<td><xsl:value-of select="@script_name" /></td>
</tr>
<tr>
<td><xsl:value-of select="@test_date" /></td>
</tr>
<tr>
<td><xsl:value-of select="@total_pass_count" /></td>
</tr>
<tr>
<td><xsl:value-of select="@total_fail_count" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
HTML 输出:
<html>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<img style="float: left;" src="logo.gif" alt="logo" >
<h1 style="font-size:60px;text-align: center;">Test Execution
Report</h1>
</head>
<body>
<table>
<tr>
<td>SupplierInquiry</td>
</tr>
<tr>
<td>2/02/2018 19:28:43 PM EST (UTC -5:00)</td>
</tr>
<tr>
<td>12</td>
</tr>
<tr>
<td>0</td>
</tr>
</table>
</body>
</html>
你能帮我解决这个问题吗?
【问题讨论】:
-
您不能将
img和h1标签放在<head>,,,</head>部分内。 -
@JimGarrison:说得好。我已更新 my answer below 以在 XSLT 中显示固定的 HTML。谢谢!