我应该为 XML 站点地图发送什么 Content-Type 值?
我以为我应该发送“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
text/xml 和 application/ 之间的区别如果省略 charset 参数,xml 是默认字符编码:
对于 text/xml :
对于application/xml:
因此,如果省略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:
For text/xml:
For application/xml:
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).
根据经验,让所有 Web 服务器、代理和客户端浏览器正确处理文档的最安全方法可能如下:
就 RFC 3023 规范而言,某些浏览器无法正确实现该规范,主要内容类型的差异在于客户端应如何处理字符编码,如下所示:
对于 application/xml、application/xml-dtd、application/xml-external-parsed-entity 或 application/ 的任何一种子类型xml 例如 application/atom+xml、application/rss+xml 或 application/rdf+xml,字符编码按以下顺序确定:
对于text/xml、text/xml-external-parsed-entity或像text/foo+xml这样的子类型,文档内XML声明的encoding属性被忽略,字符编码为:
大多数解析器不实现该规范;他们忽略 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:
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:
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:
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.
两者都很好。
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.
text/xml 用于如果以文本形式呈现而无需进一步处理的话对人类有意义的文档,application/xml 用于其他所有内容
— 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
— http://www.ietf.org/rfc/rfc2376.txt
这里的其他答案解决了 XML 响应的正确
Content-Type
是什么的一般问题,并得出结论(与 Web 服务响应的 text/xml 与 application/xml 之间有什么区别),text/xml
和application/xml
都是允许的。然而,没有人指出是否有任何特定于站点地图的规则。答:没有。站点地图规范为 https://www.sitemaps.org,并使用 Google
site: 搜索,您可以确认它不包含单词或短语 mime、mimetype、content-type、application/xml ,或text/xml任何地方。换句话说,它完全没有提及应该使用什么内容类型来提供站点地图。
由于站点地图规范中没有直接解决此问题的任何注释,我们可以安全地假设应用与选择任何其他 XML 文档的
Content-Type
时相同的规则 - 即它可能是text/xml
或application/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 bothtext/xml
andapplication/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 whatContent-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 eithertext/xml
orapplication/xml
.