有没有办法在 XML 模式中强制/保留 XML 元素的顺序?

有没有办法在 XML 模式中强制/保留 XML 元素的顺序?

问题描述:

让我们考虑以下 XML 架构:

Let's consider the following XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    targetNamespace="http://www.example.org/library" 
    elementFormDefault="qualified" 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    xmlns:lib="http://www.example.org/library">

    <element name="library" type="lib:libraryType"></element>

    <complexType name="libraryType">
        <sequence>
            <element name="books" type="lib:booksType"></element>
        </sequence>
    </complexType>

    <complexType name="booksType">
        <sequence>
            <element name="book" type="lib:bookType" 
                     maxOccurs="unbounded" minOccurs="1"></element>
        </sequence>
    </complexType>

    <complexType name="bookType">
        <attribute name="title" type="string"></attribute>
    </complexType>
</schema>

和相应的 XML 示例:

and a corresponding XML example:

<?xml version="1.0" encoding="UTF-8"?>
<lib:library 
    xmlns:lib="http://www.example.org/library" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/library src/library.xsd ">

  <lib:books>
    <lib:book title="t1"/>
    <lib:book title="t2"/>
    <lib:book title="t3"/>
  </lib:books>

</lib:library>

有没有办法保证 <lib:book .../> 元素的顺序被保留?我想确保任何读取 XML 的解析器都将返回指定顺序的书籍,即首先是带有 title="t1" 的书籍,然后是带有 title="t2" 的书籍,最后是带有 title="t3" 的书.

Is there a way to guarantee that the order of <lib:book .../> elements is preserved? I want to be sure that any parser reading the XML will return books in the specified oder, that is first the book with title="t1", then the book with title="t2", and finally the book with title="t3".

据我所知,XML 解析器不需要保留顺序.我想知道是否可以通过 XML Schema 强制执行此操作?对我来说,一个快速的解决方案是向 <lib:book .../> 元素添加一个 index 属性,并将顺序保存委托给读取 XML 的应用程序.

As far as I know XML parsers are not required to preserve order. I wonder whether one can enforce this through XML Schema? One quick solution for me would be adding an index attribute to the <lib:book .../> element, and delegate order preservation to the application reading the XML.

评论?建议?

我知道这很旧,但尽管如此,我也在寻找一种方法来保证 XML 元素的保留顺序.

I know this is very old, but none-the-less, I am also looking for a way to guarantee a preserved order of XML elements.

虽然所有当前的解析器可能会保留顺序,但 XML 定义绝不要求这样做,而且未来的解析器可能会以完全不同的方式处理顺序.

While all current parsers might preserve order, the XML definition in no way requires this, and future parsers might handle the order completely differently.

似乎目前存在的最佳解决方案是给元素一个 'id' 属性,以便在解析后可以在代码中验证排序.

It seems the best solution that exists right now is to give the elements an 'id' attribute so that ordering can be verified in code after being parsed.

当顺序在 XML 文件中已经很明显时,我讨厌必须要求我的用户添加任意​​ id="1"、id="2" 等,但据我所知没有官方标准在 XML 中要求重复元素的特定顺序.

I hate having to require my users to add an arbitrary id="1", id="2", etc. when the order is already obvious in the XML file, but as far as I know there is no official standard in XML for requiring a specific order for repeating elements.

更新

如果您使用 .NET,它有一个属性,您可以设置它来定义读取此类元素的顺序.所以,我想只要您知道将解析 XML 的位置,并且解析器支持强制执行元素按您需要的方式排序,然后可以强制执行顺序.

If you're using .NET, it has a property you can set to define which order it reads such elements in. So, I guess as long as you know where your XML will be parsed, and the parser supports the enforcement of element ordering the way you require, then order can be enforced.

但是,如果第三方尝试使用他们自己的解析器复制您的结果,则可能会出现混淆,因为该顺序是在其他地方配置的,而不是在 XML 中,因此没有其他解析器实现会知道利用该顺序订购.

If, however, a third party were to take and try to replicate your results with their own parser, confusion could ensue since that order is configured somewhere else, not within the XML, therefore no other parser implementation would know to utilize that order.

所以,这一切都可以概括为:(a) 不能在 XML 规范本身内强制执行顺序,但是 (b) 可以并且经常由大量的 XML 解析器强制执行顺序.

So, this can all be summarized that: (a) Order cannot be enforced within the XML specification itself, but (b) Order can be and often is enforced by a large number of XML parsers out there.