如何使用 XSLT 输出 <!DOCTYPE html>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6334381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:05:19  来源:igfitidea点击:

How to output <!DOCTYPE html> with XSLT

htmlxslt

提问by Emiliano Poggi

Possible Duplicate:
Set HTML5 doctype with XSLT

可能的重复:
使用 XSLT 设置 HTML5 文档类型

I'm new to xslt and I'm trying to produce an HTML 5 document.

我是 xslt 的新手,我正在尝试生成 HTML 5 文档。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <!DOCTYPE html>

and Firefox gives me the error

和 Firefox 给了我错误

"XML Parsing Error: not well-formed
Location: file:///E:/XSLT-XML-Shema/shipping-transform.xsl
Line Number 6, Column 4: <!DOCTYPE html>

If it's just <html>it works fine. How do I fix this and why does it happen?

如果只是<html>它工作正常。我该如何解决这个问题,为什么会发生?

--EDIT--

- 编辑 -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compact" />
<xsl:template match="/">
        <!DOCTYPE html>

        <head>
                <meta charset="utf-8" />
            <title>Sample Corporation #1</title>
            </head>
            <body>
            Hello this is a test<br />
            Goodbye!
            </body>
            </html>
</xsl:template>

</xsl:stylesheet>

回答by Emiliano Poggi

If you want absolutely the contracted form, your only choice is the disable-output-escapingof xsl:textas linked in the comments above. I think this is a bit dirty, and more, you have to indicate it within a template:

如果你想绝对承包形式,你唯一的选择是disable-output-escapingxsl:text,如上述评论链接。我觉得这有点脏,更重要的是,你必须在模板中指出它:

<xsl:template match="/">
    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>
</xsl:template>

Alternative cleaner solution, W3C defines for HTML5 a specific DOCTYPE legacy string that can be used by HTML generators which can't display the doctype in the shorter format. So, to stay with pure XSLT you can use:

作为替代的清洁解决方案,W3C 为 HTML5 定义了一个特定的 DOCTYPE 遗留字符串,该字符串可由无法以较短格式显示文档类型的 HTML 生成器使用。因此,要保持纯 XSLT,您可以使用:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" doctype-system="about:legacy-compat" />

    <xsl:template match="/">
        <html>
            <head>
                <meta charset="utf-8" />
                <title>Sample Corporation #1</title>
            </head>
            <body>
                Hello this is a test<br />
                Goodbye!
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>