如何用XML创建有序数据

时间:2012-09-27 14:17:42

标签: xml

我想将多边形定义为一系列点,如下所示:

<polygon>
   <point x="0"     y="0" z="0" />
   <point x="100.0" y="100.0" z="100.0" />
   <point x="50.0"  y="100.0" z="50.0"  />
</polygon>

当我在

中阅读时,保留点的顺序非常重要

我写了以下xsd文件:

<xs:element  name="point_type">
   <xs:complexType>
      <xs:attribute name="x" type="xs::double"/>
      <xs:attribute name="y" type="xs::double"/>
      <xs:attribute name="z" type="xs::double"/>
   </xs:complexType>
</xs:element>

<xs:element name="polygon_type">
  <xs:complexType>
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="point" type="point_type"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

但是sombody指出xs:sequence并不一定意味着<points>将按顺序排列。 实际上,http://www.w3schools.com/schema/el_sequence.asp似乎定义了该序列

    <xs:sequence maxOccurs="unbounded">
      <xs:element name="A" type="A_type"/>
      <xs:element name="B" type="B_type"/>
    </xs:sequence>

只会确保B位于A之后

<super>
  <A/>
  <B/>
  <A/>
  <B/>
</super>

并且没有说明{A-B}对的顺序。

有人知道吗? 你能指点我支持一个论点或另一个论点吗?

1 个答案:

答案 0 :(得分:1)

我建议您为这些点添加id属性,以便您可以显式跟踪应用程序中点的顺序,而不是依赖XML解析器以正确的顺序返回元素。我使用的所有XML解析器都遵循元素排序,但我在XML spec中找不到任何关于此的内容。

<polygon>
   <point id="0" x="0"     y="0" z="0" />
   <point id="1" x="100.0" y="100.0" z="100.0" />
   <point id="2" x="50.0"  y="100.0" z="50.0"  />
</polygon>