简体   繁体   English

流式 XML Mime 类型?

[英]Streaming XML Mime Type?

I'm building a web service that emits streaming XML.我正在构建一个发出流式 XML 的 Web 服务。 So, the output will look (at a high level) like this:所以,输出看起来(在高层次上)是这样的:

<fragment1>
    <!-- ... -->
</fragment1>

<fragment2>
    <!-- ... -->
</fragment2>

...and so on. ...等等。 For a normal XML document, you'd use any one of these different MIME types:对于普通的 XML 文档,您可以使用以下任何一种不同的 MIME 类型:

  • application/xml应用程序/xml
  • application/vnd.mycompany.com.description+xml (per this lovely answer ) application/vnd.mycompany.com.description+xml(根据这个可爱的答案
  • text/xml文本/xml

However, those MIME types all assume that the response contains exactly one XML document/fragment.但是,这些 MIME 类型都假定响应只包含一个 XML 文档/片段。 In my case, the response contains zero or more fragments.就我而言,响应包含零个或多个片段。 For this reason, it seems like The Wrong Thing to use one of those MIME types.出于这个原因,使用这些 MIME 类型之一似乎是错误的事情。 A correct handler would (correctly) handle the response as a single XML document and either (a) barf upon arriving at the second fragment, or (b) silently ignore fragments starting at fragment 2.正确的处理程序会(正确地)将响应作为单个 XML 文档处理,并且要么 (a) 在到达第二个片段时 barf,要么 (b) 静默忽略从片段 2 开始的片段。

If that's The Wrong Thing, is one of these MIME types The Right Thing:如果那是错误的事情,是这些 MIME 类型之一正确的事情:

  1. application/octet-stream
  2. application/vnd.mycompany.com.description.streaming+xml
  3. application/vnd.mycompany.com.description+streaming-xml

Or should I use a completely different one?还是我应该使用完全不同的? Also, it would be great if the same "style" of MIME type could be applied to streaming JSON once that data format comes online.此外,如果数据格式上线后可以将 MIME 类型的相同“样式”应用于流式传输 JSON,那就太好了。

EDIT : To give a little more flavor to the question and provide an example of a working implementation I'm trying to emulate, this API is modelled after the Twitter streaming API .编辑:为了给这个问题更多的味道并提供一个我试图模拟的工作实现的例子,这个 API 是在Twitter 流 API之后建模

Sounds like apart from your streaming requirements, your content is actually a Multipart message with several application/xml parts.听起来除了您的流媒体要求之外,您的内容实际上是包含多个application/xml部分的多部分消息 With this layout application/json parts could also be mixed in your message.使用此布局application/json部分也可以混合在您的消息中。

If your individual XML Fragments are part of larger documents take a look at the (somewhat old and understated) XML Fragment Interchange W3C Candidate Recommendation .如果您的个人 XML 片段是较大文档的一部分,请查看(有些陈旧和低调的) XML 片段交换 W3C 候选建议 It defines a nice syntax to wrap fragment bodies together with contextual information about the original document.它定义了一种很好的语法来将片段主体与有关原始文档的上下文信息包装在一起。

There are multiple options depending on the semantic relations in the data, and their structure.根据数据中的语义关系及其结构,有多种选择。

First option: if you have a (continuous) file that can be easily turned into a valid XML document by wrapping it in <elem></elem> tags, it should be application/xml-external-parsed-entity .第一个选项:如果您有一个(连续的)文件,可以通过将其包装在<elem> ... </elem>标签中轻松地将其转换为有效的 XML 文档,则它应该是application/xml-external-parsed-entity This can be anything from a simple text to comments, processing instructions or a list of complex elements.这可以是从简单文本到注释、处理指令或复杂元素列表的任何内容。 You cannot however insert the XML declaration (charset has to be defined via MIME) or any DTD (so the meaning has to be provided by the enclosing document if you rely on the DTD, and you also cannot include any other external parsed entities, unless you use XInclude).但是,您不能插入 XML 声明(必须通过 MIME 定义字符集)或任何 DTD(因此,如果您依赖 DTD,则其含义必须由封闭文档提供,并且您也不能包含任何其他外部解析实体,除非您使用 XInclude)。

I find this suitable for anything that can be described as arbitrary XML content/fragment.我发现这适用于可以描述为任意 XML 内容/片段的任何内容。 It is mostly intended to be used through external parsed entities in DTDs, but works equally well on its own.它主要用于通过 DTD 中的外部解析实体使用,但它本身也同样有效。 Use this if your fragments might not have a single root node.如果您的片段可能没有单个根节点,请使用此选项。 I can think of one caveat with this however: if the stream is infinite, the client will eventually have to terminate it somewhere, and since there is no external boundary specified, it may be terminated in the middle of an element, making it invalid according to its schema.然而,我可以想到一个警告:如果流是无限的,客户端最终将不得不在某处终止它,并且由于没有指定外部边界,它可能会在元素的中间终止,根据到它的架构。

You may also use application/xml and write the start tag yourself, but some parsers may wait for the end of the document if they are configured to process it as a whole.您也可以使用application/xml并自己编写开始标记,但是如果某些解析器配置为整体处理它,则它们可能会等待文档结束。 With application/xml-external-parsed-entity , the best that can be done is to parse it as a stream of individual XML nodes.使用application/xml-external-parsed-entity ,最好的办法是将其解析为单个 XML 节点的流。

Second option: there is the range of multipart types.第二种选择:有多种类型的multipart类型。 This way, you can wrap individual XML documents ( application/xml or specific) or fragments ( application/xml-external-parsed-entity ).这样,您可以包装单个 XML 文档( application/xml或特定的)或片段( application/xml-external-parsed-entity )。 Again, the choice of the inner type depends on whether the individual messages may be treated as standalone XML documents (for example application/svg+xml for "SVG video").同样,内部类型的选择取决于各个消息是否可以被视为独立的 XML 文档(例如application/svg+xml用于“SVG 视频”)。

The choice of the subtype depends on the intended meaning of the whole sequence.子类型的选择取决于整个序列的预期含义。 A stream of grouped individual standalone files may use multipart/mixed (this is the most general of types).分组的单个独立文件流可以使用multipart/mixed (这是最通用的类​​型)。 If the XML data is interlinked in some way, you can use multipart/related and assign identifiers to the individual fragments.如果 XML 数据以某种方式相互链接,您可以使用multipart/related并为各个片段分配标识符。 And lastly, multipart/x-mixed-replace is used if only the last part of the message represents the up-to-date content of the resource (to save individual requests).最后,如果只有消息的最后一部分代表资源的最新内容(以保存单个请求),则使用multipart/x-mixed-replace


For illustration:举例说明:

  • If the response is a stream of text enriched with XHTML markup (converted from a Markdown stream for example), it should be a single application/xml-external-parsed-entity .如果响应是包含 XHTML 标记的文本流(例如从 Markdown 流转换而来),则它应该是单个application/xml-external-parsed-entity

  • If the fragments are attachments, files constantly downloaded from websites or uploaded by users, it should be multipart/mixed .如果片段是附件、经常从网站下载或用户上传的文件,则应该是multipart/mixed

  • If the fragments are nodes in a large or ever growing graph of resources (not just XML), multipart/related should be used.如果片段是大型或不断增长的资源图中的节点(不仅仅是 XML),则应使用multipart/related

  • If the result is a short-lived information, like the current status of some process or continuous measurement of something, it should be multipart/x-mixed-replace .如果结果是一个短暂的信息,比如某个过程的当前状态或某个东西的连续测量,它应该是multipart/x-mixed-replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM