开发者

How to output <!DOCTYPE html> with XSLT [duplicate]

开发者 https://www.devze.com 2023-03-12 08:58 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Set HTML5 doctype with XSLT
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Set HTML5 doctype with XSLT

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

<?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

"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?

--EDIT--

<?xml version="1.0" enc开发者_高级运维oding="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>


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

<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:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消