为什么 Opera 将我的网页解析为 XML?

发布于 2024-08-31 06:20:51 字数 1489 浏览 4 评论 0原文

我刚刚尝试在 Opera(版本 10.50)中查看我的网站 http://www.logmytime.de/给我一个“xml解析失败错误”并拒绝显示网页。

我可以选择“将文档重新解析为 HTML”,然后页面就可以正常工作,但这很难解决我的问题。

奇怪的是,在设置 HTML(而不是 XTHML)doctype 后仍然出现错误:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">

我检查了浏览器的源输出,以确保我没有对 Doctype 犯任何错误,我什至在 Firebug 中查看了相同的网页,它显示了 text/html 的 Content-Type; 。

那么,为什么 Opera 仍然尝试将我的网页解析为 XML?

谢谢,

阿德里安

编辑:只是为了澄清:我不是在问我的网页上的错误是什么。我明白为什么这不是有效的 XHTML。但是,我也使用 javascript 微模板引擎,它的模板永远不会有效的 XML,这就是为什么我需要浏览器将整个网站解析为 HTML,而不是 XHTML。为了演示这一点,我只是在网页中插入了一个示例模板。

<script type="text/html" id="StopWatchTemplate" > 

<h1><a href="#" onclick="TimeEntriesList.EditTimeEntry('<#=timeEntryID#>')"><#=currentlyRunning?"Aktueller":"Letzter"#> Stoppuhr-Zeiteintrag</a></h1>
<%-- Stoppuhr - Ende--%>

</script>

在 Opera 中打开页面时,您可以看到模板现在会产生 XML 解析错误,即使页面的文档类型仍然是 HTML。

编辑 2::只是为了让这一点更清楚:我不是在问为什么我的网页不是有效的 XHTML。我问为什么 Opera 尝试将其解析为 XHTML,尽管有 HTML 文档类型。

编辑3::请不要再发布任何答案,我已经找到了原因并且记录如下。

I just tried viewing my website http://www.logmytime.de/ in Opera (version 10.50) it gives me an "xml parsing failed error" and refuses to display the web page.

I can choose to "Reparse the document as HTML" and then the page works fine, but that's hardly a solution to my problem.

The weird thing is that the error still occurs after setting a HTML (instead of XTHML) doctype:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">

I checked the source output from the browser to make sure I did not make any mistake with the Doctype I even viewed the same web page in Firebug and it shows a Content-Type of text/html; .

So, why does Opera still try to parse my web page as XML?

Thanks,

Adrian

Edit: Just to clarify: I am not asking what the error on my web page is. I understand why this is not valid XHTML. However, I am also using the javascript micro templating engine, and it's templates are never valid XML, which is why I need the browser to parse my entire web site as HTML, not XHTML. In order to demonstrate this, I just inserted an example template into the web page.

<script type="text/html" id="StopWatchTemplate" > 

<h1><a href="#" onclick="TimeEntriesList.EditTimeEntry('<#=timeEntryID#>')"><#=currentlyRunning?"Aktueller":"Letzter"#> Stoppuhr-Zeiteintrag</a></h1>
<%-- Stoppuhr - Ende--%>

</script>

When opening the page in Opera, you can see that the template now produces XML parsing errors even though the doctype for the page is still HTML.

Edit 2:: Just to make this even clearer: I am not asking why my web page is not valid XHTML. I am asking why Opera tries to parse it as XHTML despite the HTML doctype.

Edit3:: Please do not post any more answers, I have found the cause of this and documented it below.

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

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

发布评论

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

评论(10

乖乖公主 2024-09-07 06:20:51

您的文档不是有效的 HTML 文档。因此,浏览器应该拒绝它。不幸的是,由于历史性事故,大多数浏览器不会拒绝无效文档,而是尝试修复它们(通常会产生相当糟糕的结果),以便作者甚至不会注意到他的文档已损坏。

值得庆幸的是,通过 XHTML,浏览器供应商决定解决这个问题,并实际上拒绝无效文档。在您的情况下,您将使用 application/xhtml+xml MIME 类型以 XHTML 形式交付文档:

# curl --head http://www.logmytime.de/
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 12529
Content-Type: application/xhtml+xml; charset=utf-8
              ^^^^^^^^^^^^^^^^^^^^^
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 2.0
X-AspNet-Version: 2.0.50727
Set-Cookie: Referrer=None; path=/
X-Powered-By: ASP.NET
Date: Tue, 04 May 2010 16:08:40 GMT

So, the browser rejects your document (as it should). When you switch over to HTML, then it tries to fix your broken HTML.

现在,您已将 DOCTYPE 更改为 HTML 4.01,但您仍然以 XHTML 形式提供它。您现在所取得的成就是,浏览器拒绝您的文档有两个原因:它仍然无效,因为您尚未修复实际的错误 DOCTYPE 和 MIME 类型不匹配。

解决此问题的正确方法是简单地修复无效的文档,而不是为了让浏览器解析损坏的文档而摆弄 DOCTYPE 和 MIME 类型标记并删除第 172 行无关的 class 属性。 [顺便说一句:谁写了该文档?缩进和格式太糟糕了。]

Your document is not a valid HTML document. So, the browser should reject it. Unfortunately, due to a historic accident, most browsers do not reject invalid documents, but rather try to fix them (usually with pretty crappy results), so that the authro never even notices that his document is broken.

Thankfully, with XHTML, the browser vendors decided to fix that, and actually reject invalid documents. In your case, you are delivering your document as XHTML with the application/xhtml+xml MIME type:

# curl --head http://www.logmytime.de/
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 12529
Content-Type: application/xhtml+xml; charset=utf-8
              ^^^^^^^^^^^^^^^^^^^^^
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 2.0
X-AspNet-Version: 2.0.50727
Set-Cookie: Referrer=None; path=/
X-Powered-By: ASP.NET
Date: Tue, 04 May 2010 16:08:40 GMT

So, the browser rejects your document (as it should). When you switch over to HTML, then it tries to fix your broken HTML.

Now, you have changed your DOCTYPE to HTML 4.01, but you are still delivering it as XHTML. All you have achieved now is that there are two reasons for the browser to reject your document: it's still invalid because you haven't fixed the actual bug and the DOCTYPE and the MIME type don't match up.

Instead of mucking around with DOCTYPEs and MIME types in order to get the browser to parse your broken document, the correct way to solve this problem would be to simply fix the invalid markup and remove the extraneous class attribute on line 172. [BTW: who wrote that document? The indentation and formatting is awful.]

紧拥背影 2024-09-07 06:20:51

您已指定两次“class”属性。

替代文本

来自格式良好的约束:唯一Att规范

属性名称不得在同一开始标记或空元素标记中出现多次。

You have the "class" attribute specified two times.

alt text

From Well-formedness constraint: Unique Att Spec:

An attribute name MUST NOT appear more than once in the same start-tag or empty-element tag.

伤痕我心 2024-09-07 06:20:51

您得到了正确的答案(HTTP 内容类型标头强制进行 XML 解析),并且它似乎已修复。我将添加一个小提示,告诉您如何从 Opera 本身找出问题所在。两种可能的方式:

1) 信息面板

默认情况下该面板是不可见的,但是如果您打开左侧的面板栏(如果看不到它,请按 F4 进行切换),然后单击小底部的加号,您可以在菜单中启用“信息”。

信息面板显示有关当前打开的页面的一些分类信息,包括编码和 MIME 类型。

2) Opera Dragonfly

按 Ctrl-Shift-I 打开开发人员工具(或通过菜单选择“工具”>“高级”>“Opera Dragonfly”)

转到“网络”选项卡,然后重新加载站点。您将看到请求并可以查看标头。将此与 Firebug 中的相应信息进行比较,您会发现内容类型标头的差异。 (在这里您还会看到 Opera 发送一个包含“application/xhtml+xml”的“Accept”标头。这意味着“嗨,服务器,如果您碰巧拥有真正的 XHTML 格式的此文件,我会理解这一点。”。也许您的服务器端框架看到了该标头并错误地响应了 XHTML 内容类型,即使内容无效?)

You got the correct answer (HTTP content-type header mandating XML parsing) and it seems it's fixed. I'll just add a minor hint on how you can figure out what's wrong from within Opera itself. Two possible ways:

1) Info panel

This is not visible by default, but if you open the panel bar on the left (press F4 to toggle if you don't see it), then click the small plus sign at the bottom, you can enable "Info" in the menu.

The info panel shows some assorted information about the page currently open, including encoding and MIME type.

2) Opera Dragonfly

Press Ctrl-Shift-I to open developer tools (or go through menus to Tools > Advanced > Opera Dragonfly)

Go to "Network" tab, then re-load site. You will see the request and can review the headers. Comparing this with corresponding information from Firebug would have shown you the difference in Content-type headers. (Here you will also see that Opera sends an "Accept" header that contains "application/xhtml+xml". This means "Hi server, if you happen to have this file in real XHTML format I would understand that just fine.". Perhaps your server-side framework saw that header and wrongly responded with the XHTML content-type even though the content was invalid?)

痴情换悲伤 2024-09-07 06:20:51

如果其他人遇到同样的问题:根据 DeveloperArt 的建议,可以使用页面元素中的简单 ContentType="text/html" 属性来修复它。

编辑:该问题实际上是由我在网络项目中使用的 mobile.Browser 文件的错误引起的。上面的解决方法有效,但在我的情况下并不是必需的。请参阅此答案了解更多详情。

In case someone else has the same problem: As suggested by DeveloperArt it can be fixed with a simple ContentType="text/html" attribute in the page element.

Edit: The problem was in fact caused by a bug with the mobile.Browser file I am using in my web project. The workaround above works, but it is not really necessary in my case. See this answer for more details.

苹果你个爱泡泡 2024-09-07 06:20:51

看起来服务器正在为不同的用户代理提供不同的 mime 类型。 Firefox 正在获取text/html,但Opera(以及根据Jörg W Mittag 的说法是curl)正在获取application/xhtml+xml。您的网站有内容协商代码吗?

It seems like the server is serving a different mime types to different user-agents. Firefox is getting text/html but Opera (and curl according to Jörg W Mittag) is getting application/xhtml+xml. Do you have any content-negotiation code for your site?

心欲静而疯不止 2024-09-07 06:20:51

尝试从另一台电脑上尝试,以确保您没有遇到缓存问题。

Try from another PC to make sure that you're not hitting a cache issue.

梦断已成空 2024-09-07 06:20:51

页面代码缓存在您的浏览器中,这就是您继续看到错误的原因。您最初看到该错误是因为您的代码可能无效。

The page code is cached in your browser, which is why you are continuing to see the error. You originally saw the error, because your code is likely not valid.

败给现实 2024-09-07 06:20:51

这是因为你已经告诉它...

<html xmlns="http://www.w3.org/1999/xhtml">

It is because you've kind of told it to...

<html xmlns="http://www.w3.org/1999/xhtml">
眼眸 2024-09-07 06:20:51

application/xhtml+xml

如果服务器以 application/xhtml+xml 形式发送页面,浏览器将按照规范的要求将其解析为 XML。当解析为 XML 时,第一个 XML 格式良好的错误将停止解析,并且客户端(浏览器)通常会显示错误消息。

text/html

text/html 的解析器更加宽容(由于 html 发展的历史)。

更改 mime 类型

要更改服务器发送的内容类型,您必须覆盖 HTTP 标头值:Content-Type。这可以通过服务器端的脚本语言来完成,有时也可以通过服务器的配置(例如 Apache)来完成。我不知道 Microsoft-IIS/7.5 如何在 URI 基础上指定。

内容类型:application/xhtml+xml;字符集=utf-8
或者
内容类型:text/html;字符集=utf-8

application/xhtml+xml

If the server sends the page as application/xhtml+xml, the browser parses it as XML as required by specification. When parsing as XML, the first XML well-formedness mistake will stop the parsing and the client (browser) usually displays an error message.

text/html

The parsers for text/html are more tolerant (due to the history of html development).

Changing the mime type

To change the content type sent by the server, you have to override the HTTP header value: Content-Type. This can be done through scripting language on the server side or sometimes in the configuration of the server such as Apache for example. I do not know how Microsoft-IIS/7.5 can specify on a URI base.

Content-Type: application/xhtml+xml; charset=utf-8
or
Content-Type: text/html; charset=utf-8

半透明的墙 2024-09-07 06:20:51

这种情况主要发生在 ASP.NET 中,因为它将 Opera 的内容类型设置为 application/xhtml+xml。为了克服这个问题。您需要将内容类型设置为text/html。解决此问题的最佳方法是将以下代码添加到 App_Browser 文件中 Opera 的 .browser 配置文件中。

<功能名称=“preferredRenderingMime”值=“text/html”/>
<功能名称=“preferredRenderingType”值=“html32”/>
<功能名称=“SupportsXhtmlRendering”值=“false”/>

This mostly occurs with ASP.NET as it sets content type for opera as application/xhtml+xml. In order to over come this issue. You need to set content type to text/html. The best way to fix this issue is to add following code to .browser config file for opera in App_Browser file.

<capability name="preferredRenderingMime" value="text/html" />
<capability name="preferredRenderingType" value="html32" />
<capability name="SupportsXhtmlRendering" value="false" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文