开发者

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.

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 ...

Any suggestions ? Thanks in advance !


This is a restriction of XSD Schema. I'll quote from: http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/ and then comment:


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. Moreover, the group's children must all be individual elements (no groups), and no element in the content model may appear more than once, i.e. 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

<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. 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. 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. It soon extends to non-deterministic parsing models. So the restriction was to either 0 or 1 and for no siblings.

SGML had a more powerful model using the & connector but not all systems implemented it.

EDIT If you wish to validate your XML at this level you could use Schematron as a second validator after the schema. It's based on XSLT and this type of constraint is easier to express.


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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜