为什么XHTML不能在前面使用前缀?

时间:2017-04-11 11:57:57

标签: xhtml xml-namespaces semantic-markup

在我看来,这应该是一个有效的XHTML文档:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a:html xmlns:a="http://www.w3.org/1999/xhtml">
    <a:head>
        <a:title>Document title</a:title>
    </a:head>
    <a:body>
        <a:h1>Hello World!</a:h1>
    </a:body>
</a:html>

然而,Firefox和Internet Explorer都没有像XHTML那样处理它,而是显示为文本(标签不可见)。如果我删除名称空间前缀...

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Document title</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

......它变好了。我原以为这两个文件在语义上是相同的。为什么第一个不起作用?

1 个答案:

答案 0 :(得分:3)

您必须确保浏览器获得提示,将文档解释为application/xhtml+xml(或者可能是其中一个XML MIME types),而不是text/html

在服务器上,您可以通过发送相应的Content-Type HTTP标头来实现此目的:

Content-Type: application/xhtml+xml

本地,您通常可以使用.xhtml代替.html作为文件扩展名来实现此目的(但这可能取决于您的系统)。