使用 XSLT 设置 HTML5 文档类型

发布于 2024-09-12 14:48:57 字数 529 浏览 6 评论 0 原文

如何通过 XSLT 将文件的文档类型干净设置为 HTML5 (在本例中使用 collective.xdv)

以下是我的 Google foo 能够找到的最好的:

<xsl:output
    method="html"
    doctype-public="XSLT-compat"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

产生:

<!DOCTYPE html PUBLIC "XSLT-compat" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

How would I cleanly set the doctype of a file to HTML5 <!DOCTYPE html> via XSLT (in this case with collective.xdv)

The following, which is the best my Google foo has been able to find:

<xsl:output
    method="html"
    doctype-public="XSLT-compat"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

produces:

<!DOCTYPE html PUBLIC "XSLT-compat" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

那小子欠揍 2024-09-19 14:48:57

我认为目前只能通过将文档类型写为文本来支持:

<?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" encoding="utf-8" indent="yes" />

  <xsl:template match="/">
    <xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
    <html>
    </html>
  </xsl:template>

</xsl:stylesheet>

这将产生以下输出:

<!DOCTYPE html>
<html>
</html>

I think this is currently only supported by writing the doctype out as text:

<?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" encoding="utf-8" indent="yes" />

  <xsl:template match="/">
    <xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
    <html>
    </html>
  </xsl:template>

</xsl:stylesheet>

This will produce the following output:

<!DOCTYPE html>
<html>
</html>
你是年少的欢喜 2024-09-19 14:48:57

要使用简单的 HTML 文档类型 ,您必须使用 disable-output-escaping 功能:。但是,disable-output-escaping 是 XSLT 中的一个可选功能,因此您的 XSLT 引擎或序列化管道可能不支持它。

因此,HTML5 提供了一种替代文档类型,以与不支持 HTML5 的 XSLT 版本(即所有当前存在的 XSLT 版本)以及具有相同问题的其他系统兼容。替代文档类型是 。要输出此文档类型,请在 xsl:output 元素上使用属性 doctype-system="about:legacy-compat"无需使用 < code>doctype-public 属性根本没有。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" doctype-system="about:legacy-compat"/>
   ...
   <html>
   </html>
</xsl:stylesheet>

To use the simple HTML doctype <!DOCTYPE html>, you have to use the disable-output-escaping feature: <xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>. However, disable-output-escaping is an optional feature in XSLT, so your XSLT engine or serialization pipeline might not support it.

For this reason, HTML5 provides an alternative doctype for compatibility with HTML5-unaware XSLT versions (i.e. all the currently existing versions of XSLT) and other systems that have the same problem. The alternative doctype is <!DOCTYPE html SYSTEM "about:legacy-compat">. To output this doctype, use the attribute doctype-system="about:legacy-compat" on the xsl:output element without using a doctype-public attribute at all.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" doctype-system="about:legacy-compat"/>
   ...
   <html>
   </html>
</xsl:stylesheet>
明月夜 2024-09-19 14:48:57
<xsl:output
     method="html"
     doctype-system="about:legacy-compat"
     encoding="UTF-8"
     indent="yes" />

此输出

<!DOCTYPE html SYSTEM "about:legacy-compat">

被修改为我对 http://ukchill.com/technology/generate-html5-using- 的修复xslt/

<xsl:output
     method="html"
     doctype-system="about:legacy-compat"
     encoding="UTF-8"
     indent="yes" />

this outputs

<!DOCTYPE html SYSTEM "about:legacy-compat">

this is modified as my fix to http://ukchill.com/technology/generating-html5-using-xslt/

无边思念无边月 2024-09-19 14:48:57

使用 Saxon 9.4,您可以使用:

<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes" />

这会生成:

<!DOCTYPE HTML>

With Saxon 9.4 you can use:

<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes" />

This generates:

<!DOCTYPE HTML>
羁〃客ぐ 2024-09-19 14:48:57

使用 doctype-system 而不是 doctype-public

Use doctype-system instead of doctype-public

夜光 2024-09-19 14:48:57

如果您希望 XHTML 输出与 HTML5 一致,则必须使用 XHTML 1.0 Strict 作为文档类型,libxml2 的 xml 序列化程序具有由 XHTML 1.0 文档类型触发的特殊输出模式,可确保输出与 XHTML 兼容(例如
而不是

而不是 < div>>)。 doctype-system="about:legacy-compat" 确实不会触发此兼容模式

如果您对 html 输出感到满意,则设置 应该做正确的事情。然后,您可以使用 <!DOCTYPE html> 设置文档类型,尽管这样需要在适当的地方安装管道,因为 XDV 尚不支持这一点。

事实上,似乎 也没有真正帮助 - 这将导致
输出为

You must use XHTML 1.0 Strict as the doctype if you want XHTML output consistent with HTML5, libxml2's xml serializer has a special output mode triggered by the XHTML 1.0 doctypes that ensures output is XHTML compatible, (e.g. <br /> rather than <br/>, <div></div> rather than <div/>). doctype-system="about:legacy-compat" does not trigger this compatibility mode

If you are happy with html output, then setting <xsl:output method="html"> should do the right thing. You can then set the doctype with <xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>, though this will need plumbing in at the appropriate place as XDV does not support this yet.

In fact it seems <xsl:output method="html"/> does not really help either - this will result in <br/> being output as <br></br>.

对风讲故事 2024-09-19 14:48:57

Jirka Kosek 的建议的变体,来自 高级 XDV 主题 Plone.org 似乎对我有用 集体.xdv

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

This variation of Jirka Kosek's advice, via Advanced XDV theming on Plone.org seems to work for me in collective.xdv.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output
      doctype-public="HTML"
      doctype-system=""/>
</xsl:stylesheet>
何以心动 2024-09-19 14:48:57

这是一条评论,但我没有足够的业力点将其放在正确的位置。叹。

我很高兴这可能是完成我想要的事情的正确的、标准驱动的方式(我已经对此表示赞同)。但前者不受支持(我的处理器崩溃了),后者仍然导致“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”在我的文档类型中。正如 @Jirka Kosek 所建议的,我认为我的 XSLT 处理器可能已损坏。

不,您的 XSLT 处理器没有损坏,只是 XDV 添加了:

,因此当您添加第二个 时,前一个 >doctype-public 不会被覆盖。

请注意,XHTML 1.0 strict 被列为过时的允许文档类型字符串,因此使用此文档类型并仍然将其称为 HTML5 是完全可以接受的。

This is a comment, but I do not have enough karma points to put it in the correct place. Sigh.

I appreciate this is probably the correct, standards driven way to accomplish what I want (I've upvoted it as such). But the former isn't supported (my processor falls over) and the latter still results in "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" in my doctype. As @Jirka Kosek suggested, I think my XSLT processor might be broken.

No, your XSLT processor is not broken, it's just that XDV adds:

<xsl:output method="xml" indent="no" omit-xml-declaration="yes" media-type="text/html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

by default, so when you add a second <xsl:output doctype-system="about:legacy-compat"/> the previous doctype-public is not overwritten.

Note that XHTML 1.0 strict is listed as an obsolete permitted doctype string, so it is perfectly acceptable to use this doctype and still call it HTML5.

冰雪梦之恋 2024-09-19 14:48:57

抱歉,仅提供链接,但这已在 WHATWG 小组中讨论过,但我已经好几个月没有处理过它了。 Ian Hickson 和一些 XML 专家对此进行了讨论:
http://lists.w3.org/Archives/Public/ public-html/2009Jan/0640.html
http://markmail.org/message/64aykbbsfzlbidzl
这是实际的问题编号:
http://www.w3.org/html/wg/tracker/issues/ 54
这是这个讨论
http://www.contentwithstyle.co.uk/content/ xslt-and-html-5-问题

Sorry to only provide links but this was discussed among the WHATWG group but it's been many months since I've dealt with it. Here Ian Hickson and some XML experts discuss this:
http://lists.w3.org/Archives/Public/public-html/2009Jan/0640.html
http://markmail.org/message/64aykbbsfzlbidzl
and here is the actual issue number:
http://www.w3.org/html/wg/tracker/issues/54
and here's this discussion
http://www.contentwithstyle.co.uk/content/xslt-and-html-5-problems

℉絮湮 2024-09-19 14:48:57

使用此标签

<xsl:output method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="XSLT-compat" indent="yes"/>

Use this tag

<xsl:output method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="XSLT-compat" indent="yes"/>
弄潮 2024-09-19 14:48:57

如果保存为 html5.xml,以下代码将作为独立模板使用:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="html5.xml"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
            >
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
      <xsl:text>hi</xsl:text>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

参考

The following code will work as a standalone template if saved as html5.xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="html5.xml"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
            >
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
      <xsl:text>hi</xsl:text>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

References

橘虞初梦 2024-09-19 14:48:57

这就是我用来生成兼容的 html5 文档类型(获取 saxons html5,否则做遗留的事情)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/REC-html40">

    <xsl:output
        method="html"
        version="5.0"
        doctype-system="about:legacy-compat"
        encoding="UTF-8"
        indent="yes" />

that's what i use to generate a compatible html5 doctype (getting saxons html5 out, otherwise doing the legacy thing)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/REC-html40">

    <xsl:output
        method="html"
        version="5.0"
        doctype-system="about:legacy-compat"
        encoding="UTF-8"
        indent="yes" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文