如何在 XSD 中指定有序列表 [英] How to specify an ordered list in XSD

查看:34
本文介绍了如何在 XSD 中指定有序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要订购的数字列表.目前它们在 XML 中是这样的:

I have a list of numbers that need to be ordered. Currently they are in the XML as such:

<value_1>0.2</value_1>
<value_2>0.4</value_2>
<value_3>0.6</value_3>
...
<value_N>1.8</value_N>

有没有更好的方法来做到这一点,以便它可以在 XSD 中很好地定义,并且数据以指定的顺序从解析器返回?

Is there a better way to do this such that it can be well defined in XSD and that the data is returned from a parser in the specified order?

编辑 XSD 片段:

<xs:complexType>
    <xs:sequence>
        <xs:element name="value_1" type="xs:decimal"/>
        <xs:element name="value_2" type="xs:decimal"/>
        <xs:element name="value_3" type="xs:decimal"/>
        ...
        <xs:element name="value_N" type="xs:decimal"/>
    </xs:sequence>
</xs:complexType>

推荐答案

XML 中的元素固有地按照它们在文档中的顺序进行排序.此顺序很重要(与属性的顺序不同)并且将由解析器保留.

Elements in XML are inherently ordered per their order in the document. This order is significant (unlike that of attributes) and will be preserved by parsers.

XML 文档必须有一个根元素,所以让我们将示例元素包装在一个包含元素中:

XML documents must have a single root element, so let's wrap your example elements in a single containing element:

<values>
  <value_1>0.2</value_1>
  <value_2>0.4</value_2>
  <value_3>0.6</value_3>
  <!-- ... -->
  <value_N>1.8</value_N>
</values>

可以为此 XML 编写 XSD:

An XSD can be written for this XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="values">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="value_1" type="xs:decimal"/>
        <xs:element name="value_2" type="xs:decimal"/>
        <xs:element name="value_3" type="xs:decimal"/>
        <!-- ... -->
        <xs:element name="value_N" type="xs:decimal"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

但是一个改进是显而易见的:从组件名称中删除索引号:

But an improvement is immediately obvious: Eliminate the indexing number from the component name:

<values>
  <value>0.2</value>
  <value>0.4</value>
  <value>0.6</value>
  <!-- ... -->
  <value>1.8</value>
</values>

这个改进的 XML 的 XSD 也同样简化(并且实际上也可以处理无限数量的 value 元素):

The XSD for this improved XML is similarly streamlined (and also can actually handle an indefinite number of value elements as well):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="values">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="value" type="xs:decimal" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

现在有人可能会问 XSD 是否可以对元素强制执行 排序 顺序,使得

Now one might ask whether the XSD can enforce a sorted order on the elements such that

<values>
  <value>0.2</value>
  <value>0.4</value>
  <value>0.6</value>
</values>

应该是有效的,但是

<values>
  <value>0.6</value>
  <value>0.2</value>
  <value>0.4</value>
</values>

无效.

这在 XSD 1.0 中是不可能的,但是 XSD 1.1 可以通过断言来表达这样的约束.

This is not possible to do in XSD 1.0, however XSD 1.1 can express such a constraint via assertions.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">
  <xs:element name="values">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="value" type="xs:decimal" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:assert test="every $v in value 
                       satisfies not(number($v) lt 
                                     number($v/preceding-sibling::value[1]))"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

信用:断言测试的想法基于Michael Kay 的想法.

Credit: The idea for the assertion test is based on one from Michael Kay.

这篇关于如何在 XSD 中指定有序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆