我应该为 XML 站点地图发送什么 Content-Type 值?

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

What Content-Type value should I send for my XML sitemap?

xmlmime-typessitemapxml-sitemap

提问by Kyle

I thought I should send "text/xml", but then I read that I should send "application/xml". Does it matter? Can someone explain the difference?

我以为我应该发送“text/xml”,但后来我读到我应该发送“application/xml”。有关系吗?有人可以解释一下区别吗?

回答by Gumbo

The difference between text/xmland application/xmlis the default character encoding if the charsetparameter is omitted:

text/xmlapplication/xml之间区别是如果省略charset参数,则默认字符编码:

Text/xml and application/xml behave differently when the charset parameter is not explicitly specified. If the default charset (i.e., US-ASCII) for text/xml is inconvenient for some reason (e.g., bad web servers), application/xml provides an alternative (see "Optional parameters" of application/xml registration in Section 3.2).

当未明确指定 charset 参数时,Text/xml 和 application/xml 的行为不同。如果 text/xml 的默认字符集(即 US-ASCII)由于某种原因(例如,糟糕的 Web 服务器)不方便,则 application/xml 提供了替代方案(请参阅第 3.2 节中 application/xml 注册的“可选参数”)。

For text/xml:

对于文本/xml

Conformant with [RFC2046], if a text/xml entity is received with the charset parameter omitted, MIME processors and XML processors MUST use the default charset value of "us-ascii"[ASCII]. In cases where the XML MIME entity is transmitted via HTTP, the default charset value is still "us-ascii".

与 [RFC2046] 一致,如果接收到的 text/xml 实体省略了 charset 参数,则 MIME 处理器和 XML 处理器必须使用“us-ascii”[ASCII] 的默认字符集值。在 XML MIME 实体通过 HTTP 传输的情况下,默认字符集值仍然是“us-ascii”。

For application/xml:

对于应用程序/xml

If an application/xml entity is received where the charset parameter is omitted, no information is being provided about the charset by the MIME Content-Type header. Conforming XML processors MUST follow the requirements in section 4.3.3 of [XML] that directly address this contingency. However, MIME processors that are not XML processors SHOULD NOT assume a default charset if the charset parameter is omitted from an application/xml entity.

如果接收到一个 application/xml 实体,其中省略了 charset 参数,则 MIME Con​​tent-Type 标头不会提供有关该字符集的信息。符合 XML 的处理器必须遵循 [XML] 的第 4.3.3 节中直接解决这种意外情况的要求。但是,如果 application/xml 实体中省略了 charset 参数,则不是 XML 处理器的 MIME 处理器不应假定默认字符集。

So if the charsetparameter is omitted, the character encoding of text/xmlis US-ASCII while with application/xmlthe character encoding can be specified in the document itself.

因此,如果省略charset参数,则text/xml的字符编码为 US-ASCII,而application/xml的字符编码可以在文档本身中指定。

Now a rule of thumb on the internet is: “Be strict with the output but be tolerant with the input.” That means make sure to meet the standards as much as possible when delivering data over the internet. But build in some mechanisms to overlook faults or to guess when receiving and interpreting data over the internet.

现在互联网上的一条经验法则是:“对输出严格,但对输入要宽容。” 这意味着在通过 Internet 传输数据时确保尽可能符合标准。但是建立一些机制来忽略错误或在通过互联网接收和解释数据时进行猜测。

So in your case just pick one of the two types (I recommend application/xml) and make sure to specify the used character encoding properly (I recommend to use the respective default character encoding to play safe, so in case of application/xmluse UTF-8 or UTF-16).

因此,在您的情况下,只需选择两种类型之一(我推荐application/xml)并确保正确指定使用的字符编码(我建议使用各自的默认字符编码以确保安全,因此在application/xml使用的情况下UTF-8 或 UTF-16)。

回答by nas

As a rule of thumb, the safest bet towards making your document be treated properly by all web servers, proxies, and client browsers, is probably the following:

根据经验,让所有 Web 服务器、代理和客户端浏览器正确处理您的文档的最安全方法可能如下:

  1. Use the application/xml content type
  2. Include a character encoding in the content type, probably UTF-8
  3. Include a matching character encoding in the encoding attribute of the XML document itself.
  1. 使用 application/xml 内容类型
  2. 在内容类型中包含字符编码,可能是 UTF-8
  3. 在 XML 文档本身的编码属性中包含匹配的字符编码。

In terms of the RFC 3023spec, which some browsers fail to implement properly, the major difference in the content types is in how clients are supposed to treat the character encoding, as follows:

就某些浏览器未能正确实现的RFC 3023规范而言,内容类型的主要区别在于客户端应该如何处理字符编码,如下所示:

For application/xml, application/xml-dtd, application/xml-external-parsed-entity, or any one of the subtypes of application/xml such as application/atom+xml, application/rss+xml or application/rdf+xml, the character encoding is determined in this order:

对于 application/xml、application/xml-dtd、application/xml-external-parsed-entity 或 application/xml 的任何一种子类型,例如 application/atom+xml、application/rss+xml 或 application/rdf+xml ,字符编码按以下顺序确定:

  1. the encoding given in the charset parameter of the Content-Type HTTP header
  2. the encoding given in the encoding attribute of the XML declaration within the document,
  3. utf-8.
  1. Content-Type HTTP 标头的 charset 参数中给出的编码
  2. 文档中 XML 声明的 encoding 属性中给出的编码,
  3. utf-8。

For text/xml, text/xml-external-parsed-entity, or a subtype like text/foo+xml, the encoding attribute of the XML declaration within the document is ignored, and the character encoding is:

对于 text/xml、text/xml-external-parsed-entity 或类似 text/foo+xml 的子类型,忽略文档内 XML 声明的编码属性,字符编码为:

  1. the encoding given in the charset parameter of the Content-Type HTTP header, or
  2. us-ascii.
  1. Content-Type HTTP 标头的 charset 参数中给出的编码,或
  2. us-ascii。

Most parsers don't implement the spec; they ignore the HTTP Context-Type and just use the encoding in the document. With so many ill-formed documents out there, that's unlikely to change any time soon.

大多数解析器没有实现规范;他们忽略 HTTP Context-Type 并只使用文档中的编码。有这么多格式错误的文件,这不太可能很快改变。

回答by Hendrik Brummermann

both are fine.

两者都很好。

text/xxx means that in case the program does not understand xxx it makes sense to show the file to the user as plain text. application/xxx means that it is pointless to show it.

text/xxx 意味着如果程序不理解 xxx,则将文件以纯文本形式显示给用户是有意义的。application/xxx 表示显示它毫无意义。

Please note that those content-types were originally defined for E-Mail attachment before they got later used in Web world.

请注意,这些内容类型最初是为电子邮件附件定义的,后来才用于 Web 世界。

回答by Quentin

text/xml is for documents that would be meaningful to a human if presented as text without further processing, application/xml is for everything else

text/xml 用于如果以文本形式呈现而无需进一步处理对人类有意义的文档,application/xml 用于其他所有内容

Every XML entity is suitable for use with the application/xml media type without modification. But this does not exploit the fact that XML can be treated as plain text in many cases. MIME user agents (and web user agents) that do not have explicit support for application/xml will treat it as application/octet-stream, for example, by offering to save it to a file.

To indicate that an XML entity should be treated as plain text by default, use the text/xml media type. This restricts the encoding used in the XML entity to those that are compatible with the requirements for text media types as described in [RFC-2045] and [RFC-2046], e.g., UTF-8, but not UTF-16 (except for HTTP).

每个 XML 实体都适用于 application/xml 媒体类型而无需修改。但这并没有利用 XML 在许多情况下可以被视为纯文本这一事实。没有明确支持 application/xml 的 MIME 用户代理(和 Web 用户代理)会将其视为 application/octet-stream,例如,通过提供将其保存到文件中。

要指示默认情况下应将 XML 实体视为纯文本,请使用 text/xml 媒体类型。这将 XML 实体中使用的编码限制为与 [RFC-2045] 和 [RFC-2046] 中描述的文本媒体类型要求兼容的编码,例如 UTF-8,但不是 UTF-16(除了HTTP)。

http://www.ietf.org/rfc/rfc2376.txt

http://www.ietf.org/rfc/rfc2376.txt

回答by Mark Amery

Other answers here address the general question of what the proper Content-Typefor an XML response is, and conclude (as with What's the difference between text/xml vs application/xml for webservice response) that both text/xmland application/xmlare permissible. However, none address whether there are any rules specific to sitemaps.

其他的答案在这里讨论的合适什么的一般问题Content-Type的一个XML响应,并得出结论(如什么是文本/ XML应用VS / XML Web服务为响应之间的差异),这两个text/xmlapplication/xml是允许的。但是,没有说明是否有任何特定于站点地图的规则。

Answer: there aren't. The sitemap spec is https://www.sitemaps.org, and using Google site:searches you can confirm that it does not contain the words or phrases mime, mimetype, content-type, application/xml, or text/xmlanywhere. In other words, it is entirely silent on the topic of what Content-Typeshould be used for serving sitemaps.

回答:没有。站点地图规范是https://www.sitemaps.org,使用 Googlesite:搜索,您可以确认它在任何地方都不包含mimemimetypecontent-typeapplication/xmltext/xml 等词或短语。换句话说,它完全没有提到Content-Type应该使用什么来提供站点地图的话题。

In the absence of any commentary in the sitemap spec directly addressing this question, we can safely assume that the same rules apply as when choosing the Content-Typeof any other XML document - i.e. that it may be either text/xmlor application/xml.

在站点地图规范中没有直接解决这个问题的任何评论的情况下,我们可以安全地假设与选择Content-Type任何其他 XML 文档时相同的规则适用- 即它可以是text/xmlapplication/xml