Html 使用 XSLT 设置 HTML5 文档类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3387127/
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 03:58:06  来源:igfitidea点击:

Set HTML5 doctype with XSLT

htmlxsltdoctypexhtml-transitionalxdv

提问by Jon Hadley

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

我将如何通过 XSLT将文件的 doctype干净地设置为 HTML5 <!DOCTYPE html>(在本例中为集体.xdv

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

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

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

回答by Dirk Vollmar

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

我认为这目前只能通过将 doctype 写为文本来支持:

<?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'>&lt;!DOCTYPE html&gt;</xsl:text>
    <html>
    </html>
  </xsl:template>

</xsl:stylesheet>

This will produce the following output:

这将产生以下输出:

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

回答by hsivonen

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

要使用简单的 HTML doctype <!DOCTYPE html>,您必须使用以下disable-output-escaping功能:<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>。但是,它disable-output-escaping是XSLT 中的一个可选功能,因此您的 XSLT 引擎或序列化管道可能不支持它。

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:outputelement withoutusing a doctype-publicattribute at all.

出于这个原因,HTML5 提供了一种替代文档类型,用于与不支持 HTML5 的 XSLT 版本(即所有当前存在的 XSLT 版本)和其他具有相同问题的系统兼容。替代文档类型是<!DOCTYPE html SYSTEM "about:legacy-compat">. 要输出此文档类型,请使用元素doctype-system="about:legacy-compat"上的属性,而根本使用属性。xsl:outputdoctype-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>

回答by Jim Michaels

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

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

回答by stephanme

With Saxon 9.4 you can use:

使用 Saxon 9.4,您可以使用:

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

This generates:

这会产生:

<!DOCTYPE HTML>

回答by Jirka Kosek

Use doctype-system instead of doctype-public

使用 doctype-system 而不是 doctype-public

回答by Laurence Rowe

You mustuse 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 nottrigger this compatibility mode

如果您希望 XHTML 输出与 HTML5 一致,您必须使用 XHTML 1.0 Strict 作为文档类型,libxml2 的 xml 序列化程序具有由 XHTML 1.0 文档类型触发的特殊输出模式,以确保输出与 XHTML 兼容(例如,<br />而不是<br/><div></div>而不是<div/>)。doctype-system="about:legacy-compat"不会触发此兼容模式

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">&lt;!DOCTYPE html&gt;</xsl:text>, though this will need plumbing in at the appropriate place as XDV does not support this yet.

如果您对 html 输出感到满意,那么设置<xsl:output method="html">应该是正确的。然后您可以使用 设置文档类型<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>,尽管这将需要在适当的位置插入,因为 XDV 尚不支持此功能。

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

事实上,它似乎<xsl:output method="html"/>也没有真正的帮助——这将导致<br/>输出为<br></br>.

回答by Jon Hadley

This variation of Jirka Kosek's advice, via Advanced XDV themingon Plone.orgseems to work for me in collective.xdv.

这种变化尔卡Kosek的建议,通过先进XDV主题化Plone.org似乎为我工作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>

回答by Laurence Rowe

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.

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

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

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

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

<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-publicis not overwritten.

默认情况下,因此当您添加第二个时<xsl:output doctype-system="about:legacy-compat"/>doctype-public不会覆盖前一个。

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.

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

回答by Rob

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

很抱歉只提供链接,但这已在 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-problems

回答by Anil Kumar Gupta

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