重复元素的 XML 顺序很重要

发布于 2024-12-12 01:20:53 字数 399 浏览 0 评论 0原文

我还没有找到这个确切问题的答案,它困扰着我。

我需要创建一个 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 技术交流群。

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

发布评论

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

评论(1

好听的两个字的网名 2024-12-19 01:20:53

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.

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