【问题标题】:XSLT not closing META and IMG tagsXSLT 不关闭 META 和 IMG 标记
【发布时间】: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>

你能帮我解决这个问题吗?

【问题讨论】:

  • 您不能将imgh1 标签放在&lt;head&gt;,,,&lt;/head&gt; 部分内。
  • @JimGarrison:说得好。我已更新 my answer below 以在 XSLT 中显示固定的 HTML。谢谢!

标签: html xml xslt


【解决方案1】:

添加

<xsl:output method="xml"/>

到您的样式表的顶层,以确保在输出标记中关闭所有标签。您可能更喜欢缩进输出,因为 XML 的默认值为“no”:

<xsl:output method="xml" indent="yes"/>

补充说明:

  1. XSLT 中的 HTML 模板会生成无效的 HTML(谢谢, @吉姆加里森); imgh1 不允许在 head 内部。 这是一个修复:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="ROOT">
        <html>
          <head>
            <meta name="description" content="Results of system1 testing"/>
            <title>Test Execution Report</title>
           </head>
          <body>
            <img style="float: left;" src="logo.gif" alt="logo" />
            <h1 style="font-size:60px;text-align: center;">Test Execution
            Report</h1>
            <table>
              <tr>
                <td><xsl:value-of select="@script_name" /></td>
              </tr>
              <!- ... -->
            </table>
          </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>
    
  2. metaimg 元素未关闭的原因 是没有明确的xsl:output/method,HTML 序列化 在给定输出树的 html 根元素的情况下使用;除此以外 默认是使用 XML 序列化。 (谢谢, @MichaelKay。)

  3. 对于xsl:output/@method="xml",XSLT 1.0 不会生成

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    

    但它会传播明确声明的元标记,例如

    <meta name="description" content="Results of system1 testing"/>
    

    对于 XSLT 2.0,使用 xsl:output/@method="xhtml" 来获取 自动生成的meta 元素和结束标签。 (再次感谢, @MichaelKay。)

【讨论】:

  • 注意,如果结果树的最外层元素为“html”,则默认的序列化方式为html,否则为xml。另请注意,如果您使用 method="xml"; 则根本不会生成元标记;使用 XSLT 2.0,您还可以要求提供格式良好的 XML 输出但仍会生成元元素的 method="xhtml"。
  • @MichaelKay:已更新答案以包含您的有用注释。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多