开始使用 CDATA 是一个重大变化吗?
为了与客户的应用程序进行交互,我们使用 XML 文档。也就是说,我们通过 HTTP 发送 XML 并以相同的方式接收响应 XML 文档。客户指定了两个 XML 模式来描述请求和回复的格式。一切都工作正常,直到有一天客户开始在响应 XML 中使用 CDATA 部分。我们设置解析器时没有考虑 CDATA 部分,因此我们无法解释它们。
我的问题是:谁在这里犯了错误?我试图在 XML 标准中找到答案,但我仍然不确定。我想我不能规定在 XSD 中使用或不使用 CDATA,对吗?如果是这样,仅仅就 XSD 达成一致还不够,还必须就 CDATA 部分达成单独的协议吗?或者也必须准备好解析 CDATA 和常规文本?
我对你的个人观点和任何官方声明也很感兴趣。谢谢你!
For interaction with a customer's application we use XML documents. That is, we send an XML over HTTP and receive a response XML document the same way. The customer specified two XML schemata that describe the format of both the request and reply. All was working fine, until one day the customer started to use CDATA sections in the response XML. We set up our parser unmindful of CDATA sections, so we failed to interpret them.
My question is: Who made a mistake here? I tried to find an answer in the XML standards, but I'm still not sure. I think I cannot prescribe using or not using CDATA's in an XSD, is that right? If so, is it not enough to agree upon an XSD, but a separate agreement has to be made about CDATA sections? Or one is obliged to be prepared to parse CDATA and regular text as well?
I'm interested in both your personal views and any official statements too. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
许多 XML 解析器将文本和 CDATA 分开,这是不幸的。错误是你的:常规文本块和 CDATA 之间没有语义差异,因此发送者应该可以根据手头文本的需要在它们之间自由选择。
好消息是调整代码应该是一件简单的事情。
Many XML parsers separate out text and CDATA, which is unfortunate. The mistake was yours: there is no semantic difference between regular text chunks and CDATA, so the sender should be free to choose between them based on the needs of the text at hand.
The good news is it should be a simple matter to adapt your code.
CDATA 是 XML 的基本部分。不支持它意味着解析器已损坏(而不是真正的 XML 解析器(它能够处理包含文本、CDATA、实体、其他元素、注释等的元素))。
由于我在另一个答案的评论中提到了这一点,所以我现在很想在这里重复一遍。不期望 XML 中的 CDATA 就像 没想到海里有鱼。
CDATA is a basic part of XML. Failing to support it means the parser is broken (and not a real XML parser (which would be able to cope with elements containing text, CDATA, entities, other elements, comments, etc, etc, etc)).
Since I mentioned it in a comment on another answer, I now have an urge to repeat it here. Not expecting CDATA in XML is like not expecting fish in the sea.