重复元素的 XML 顺序很重要
我还没有找到这个确切问题的答案,它困扰着我。
我需要创建一个 XML 架构,其中重复元素的顺序很重要。示例:
<article>
<line>This article was</line>
<line>written by</line>
</article>
我知道上面的示例相当做作,但它说明了我的情况。我必须反序列化执行顺序很重要的一系列命令。我能看到实现此目的的唯一方法是向每个子元素添加 order="[integer]"
属性(上例中的
标记)。不过,这似乎有些繁琐,尤其是当 XML 文档需要手动输入时。还有其他选择吗?
I've not managed to find an answer to this exact question and its bugging me.
I need to create an XML Schema where the order of duplicate elements matter. Example:
<article>
<line>This article was</line>
<line>written by</line>
</article>
I know the above example is rather contrived, but it illustrates my case. I have to deserialize a sequence of commands where the execution order matters. The only way I can see of achieving this is to add an order="[integer]"
attribute to each child element (the <line>
tags in the above example). That seems a bit heavy handed though, especially as the XML documents are going to be typed by hand. Is there an alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 规范对 XML 解析器提出的报告要求非常不完整。例如,它说解析器必须报告空格,但没有说它必须报告其他字符内容。但在实践中,您可以依赖 XML 解析器报告字符内容,并使字符保持正确的顺序,并且以同样的方式,您可以依赖以正确的顺序报告的元素。规范没有强制执行此操作的唯一原因是作者认为这太明显了,无需说明。你的担心是完全没有根据的。
The XML spec is very patchy in the reporting requirements it places on an XML parser. For example, it says that the parser has to report whitespace, but doesn't say it has to report other character content. But in practice, you can rely on XML parsers reporting character content, and keeping characters in the right order, and in the same way you can rely on elements being reported in the right order. The only reason the spec doesn't mandate this is that the authors thought it was too obvious to need saying. Your fears are completely unfounded.