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

发布于 2024-09-10 11:11:34 字数 73 浏览 17 评论 0原文

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

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?

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

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

发布评论

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

评论(5

笛声青案梦长安 2024-09-17 11:11:34

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

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

对于 text/xml

符合 [RFC2046],如果收到文本/xml 实体
省略 charset 参数、MIME 处理器和 XML 处理器
必须使用默认字符集值“us-ascii”[ASCII]。在情况下
其中 XML MIME 实体通过 HTTP 传输,默认
字符集值仍然是“us-ascii”。

对于application/xml

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

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

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

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

The difference between text/xml and application/xml is the default character encoding if the charset parameter is omitted:

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).

For text/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".

For application/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.

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

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.

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/xml use UTF-8 or UTF-16).

小兔几 2024-09-17 11:11:34

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

  1. 使用 application/xml 内容类型
  2. 在内容类型中包含字符编码,可能是 UTF -8
  3. 在 XML 文档本身的编码属性中包含匹配的字符编码。

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

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

  1. Content-Type HTTP 标头的 charset 参数中给出的编码
  2. 编码中给出的编码文档中 XML 声明的属性,
  3. utf-8。

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

  1. charset中给定的编码Content-Type HTTP 标头的参数或
  2. us-ascii。

大多数解析器不实现该规范;他们忽略 HTTP 上下文类型,只使用文档中的编码。由于存在如此多格式错误的文件,这种情况不太可能很快改变。

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:

  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.

In terms of the RFC 3023 spec, 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:

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:

  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.

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:

  1. the encoding given in the charset parameter of the Content-Type HTTP header, or
  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.

南街九尾狐 2024-09-17 11:11:34

两者都很好。

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

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

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.

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

不…忘初心 2024-09-17 11:11:34

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

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

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

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

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

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).

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

断念 2024-09-17 11:11:34

这里的其他答案解决了 XML 响应的正确 Content-Type 是什么的一般问题,并得出结论(与 Web 服务响应的 text/xml 与 application/xml 之间有什么区别),text/xmlapplication/xml 都是允许的。然而,没有人指出是否有任何特定于站点地图的规则。

答:没有。站点地图规范为 https://www.sitemaps.org,并使用 Google site: 搜索,您可以确认它不包含单词或短语 mimemimetypecontent-typeapplication/xml ,或text/xml任何地方。换句话说,它完全没有提及应该使用什么内容类型来提供站点地图。

由于站点地图规范中没有直接解决此问题的任何注释,我们可以安全地假设应用与选择任何其他 XML 文档的 Content-Type 时相同的规则 - 即它可能是text/xmlapplication/xml

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

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/xml anywhere. In other words, it is entirely silent on the topic of what Content-Type should be used for serving sitemaps.

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-Type of any other XML document - i.e. that it may be either text/xml or application/xml.

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