简体   繁体   English

如何在XML Schema中获取元素顺序约束?

[英]How to get an elements order constraint in XML Schema?

I've got a XML Schema (xsd file) snippet bellow, in which I want the NAME and ADRESS elements to appear in any order, but always before the FILM sequence. 我有一个XML Schema(xsd文件)片段,其中我希望NAMEADRESS元素以任何顺序出现,但始终在FILM序列之前。

So I've been trying to do like this : 所以我一直试图这样做:

  <xs:element name="ROOM">
    <xs:complexType>
      <xs:all>
        <xs:element ref="NAME"/>
        <xs:element ref="ADRESS"/>
      </xs:all>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="FILM"/>
      </xs:sequence>
      <xs:attribute name="group" type="xs:NCName"/>
      <xs:attribute name="typ" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>

I know that the <xs:all> tag cannot be part of an element with other elements at the same child-level ... but can't find a way out ... 我知道<xs:all>标签不能是同一子级别的其他元素的元素的一部分......但是找不到出路......

Any suggestions ? 有什么建议 ? Thanks in advance ! 提前致谢 !

This is a restriction of XSD Schema. 这是XSD架构的限制。 I'll quote from: http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/ and then comment: 我将引用: http//www.w3.org/TR/2001/REC-xmlschema-0-20010502/ ,然后发表评论:


There exists a third option for constraining elements in a group: All the elements in the group may appear once or not at all, and they may appear in any order. 存在用于约束组中元素的第三选项:组中的所有元素可以出现一次或根本不出现,并且它们可以以任何顺序出现。 The all group (which provides a simplified version of the SGML &-Connector) is limited to the top-level of any content model. all group(提供SGML和-Connector的简化版本)仅限于任何内容模型的顶级。 Moreover, the group's children must all be individual elements (no groups), and no element in the content model may appear more than once, ie the permissible values of minOccurs and maxOccurs are 0 and 1. For example, to allow the child elements of purchaseOrder to appear in any order, we could redefine PurchaseOrderType as: An 'All' Group 此外,组的子项必须都是单独的元素(没有组),并且内容模型中的任何元素都不会出现多次,即minOccurs和maxOccurs的允许值为0和1.例如,允许子元素为purchaseOrder以任何顺序出现,我们可以将PurchaseOrderType重新定义为:“全部”组

 <xsd:complexType name="PurchaseOrderType"> <xsd:all> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:all> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> 

By this definition, a comment element may optionally appear within purchaseOrder, and it may appear before or after any shipTo, billTo and items elements, but it can appear only once. 根据这个定义,注释元素可以选择出现在purchaseOrder中,它可以出现在任何shipTo,billTo和items元素之前或之后,但它只能出现一次。 Moreover, the stipulations of an all group do not allow us to declare an element such as comment outside the group as a means of enabling it to appear more than once. 此外,所有小组的规定不允许我们在小组外宣布诸如评论之类的元素作为使其不止一次出现的手段。 XML Schema stipulates that an all group must appear as the sole child at the top of a content model. XML Schema规定,所有组必须作为内容模型顶部的唯一子项出现。 In other words, the following is illegal: Illegal Example with an 'All' Group 换句话说,以下是非法的:带有“全部”组的非法示例

 <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:all> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element name="items" type="Items"/> </xsd:all> <xsd:sequence> <xsd:element ref="comment" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> 

My recollection of the discussions at the time were that xsd:all was going to be too complicated to model if it was less restricted than this. 我对当时讨论的回忆是, xsd:all如果它的限制比这个要小,那么xsd:all太复杂了。 It soon extends to non-deterministic parsing models. 它很快扩展到非确定性解析模型。 So the restriction was to either 0 or 1 and for no siblings. 所以限制是0或1,没有兄弟姐妹。

SGML had a more powerful model using the & connector but not all systems implemented it. SGML使用&连接器有一个更强大的模型,但并非所有系统都实现了它。

EDIT If you wish to validate your XML at this level you could use Schematron as a second validator after the schema. 编辑如果您希望在此级别验证XML,可以使用Schematron作为模式之后的第二个验证器。 It's based on XSLT and this type of constraint is easier to express. 它基于XSLT,这种类型的约束更容易表达。

Ok, looks like I've found a way eventually : 好吧,看起来我最终找到了一条路:

  <xs:element name="ROOM">
    <xs:complexType>

      <xs:choice>
        <xs:sequence>
          <xs:element ref="NAME"/>
          <xs:element ref="ADRESS"/>
          <xs:element maxOccurs="unbounded" ref="FILM"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="ADRESS"/>
          <xs:element ref="NAME"/>
          <xs:element maxOccurs="unbounded" ref="FILM"/>
        </xs:sequence>
      </xs:choice>

      <xs:attribute name="group" type="xs:NCName"/>
      <xs:attribute name="typ" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>

But I assume that it's not very optimized, considering that if you want to do this with 10 elements, you have to specify all permutations ... 但我认为它不是很优化,考虑到如果你想用10个元素做这个,你必须指定所有的排列......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM