XML Tutorial/XML Schema/Sequence

Материал из Web эксперт
Перейти к: навигация, поиск

Fixed Order: All Elements Required

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
<xs:element name = "Count">
   <xs:complexType>
      <xs:sequence>
         <xs:element name = "One" type = "xs:string" />
         <xs:element name = "Two" type = "xs:string" />
         <xs:element name = "Three" type = "xs:string" />
      </xs:sequence>
   </xs:complexType>
</xs:element>
</xs:schema>
So the following example would be a conforming structure:
<Count>
   <One></One>
   <Two></Two>
   <Three></Three>
</Count>
But this would not:
<Count>
   <Two></Two>
   <One></One>
</Count>


Fixed Order Optional Elements

<?xml version="1.0" ?>
<schema xmlns="http://www.w3.org/2001/10/XMLSchema"
targetNamespace="http://www.wbex.runs/"
xmlns:end="http://www.wbex.ru/">
    <element name = "Add">
       <complexType>
           <sequence>
             <element name = "Namespace" minOccurs = "0" maxOccurs = "1" />
             <element name = "FilePath" />
          </sequence>
       </complexType>
    </element>
</schema>
So, both of the following would be valid instances:
<Add>
   <Namespace>http://www.wbex.ru/books</Namespace>
   <FilePath>http://www.wbex.ru/books/books.xsd</FilePath>
</Add>
<Add>
   <FilePath>http://www.wbex.ru/books/books.xsd</FilePath>
</Add>


Require Both Elements or None

File: Schema.xsd
<?xml version = "1.0" ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   
    <xs:element name="Book">
       <xs:complexType>
          <xs:sequence minOccurs = "0" maxOccurs = "1">
                <xs:element name="Title" type="xs:string" />
                <xs:element name="Author" type="xs:string"  />
          </xs:sequence>
       </xs:complexType>
    </xs:element>
   
</xs:schema>
So the following two examples would be allowed:
<Book></Book>
<Book>
   <Title>XML</Title>
   <Author>author</Author>
</Book>
But the third is invalid because it only contains one of the child elements:
<Book>
   <Title>Professional XML Schema</Title>
</Book>


Requiring Elements in a Mixed Model

File: Data.xml
<data paperID = "43374">
  A <partA>B</partA>C<partB accepted = "true">D</partB>
</data>
File: Schema.xsd
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   <xs:element name = "data">
      <xs:complexType  mixed = "true">
         <xs:sequence>
            <xs:element name = "partA" type = "xs:string" />
            <xs:element name = "partB" type = "partBType" />
         </xs:sequence>
         <xs:attribute name = "paperID" type = "xs:integer" />
      </xs:complexType>
   </xs:element>
   
   <xs:complexType name = "partBType">
      <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name = "accepted" type = "xs:boolean" />
        </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
   
</xs:schema>


Requiring Elements to Appear in Sequence

A sequence determines the order in which its contained elements may appear in an XML document.
A sequence can also contain other sequences, choices or references to named groups.
A sequence may be contained in a complex type definition in other sequences, or in a set of choices or in named group definitions.
It"s legitimate for a sequence to contain only one element.
 
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xsd:complexType name="animalType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" minOccurs="2" />
      <xsd:element name="subspecies" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>


Requiring Repeating Sequences of Elements

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
    <xs:element name = "data" >
       <xs:complexType>
          <xs:sequence minOccurs = "3" maxOccurs = "3">
                <xs:element name = "One" type = "xs:string" />
                <xs:element name = "Two" type = "xs:string" />
                <xs:element name = "Three" type = "xs:string" />
          </xs:sequence>
       </xs:complexType>
    </xs:element>
</xs:schema>
So, the following is the only allowable conforming instance document:
<data>
   <One>First came</One>
   <Two>Second came</Two>
   <Three>Third came</Three>
   <One>First came</One>
   <Two>Second came</Two>
   <Three>Third came</Three>
   <One>First came</One>
   <Two>Second came</Two>
   <Three>Third came</Three>
</data>


<sequence> Declarations

<sequence minOccurs="non negative number" 
          maxOccurs="non negative number or unbounded"> 
A sample sequence might appear as follows: 
<sequence> 
    <element name="first" type="string" minOccurs="1" maxOccurs="unbounded"/> 
    <element name="middle" type="string" minOccurs="0" maxOccurs="1"/> 
    <element name="last" type="string"/> 
</sequence> 
By utilizing a <sequence> to specify your content model, you indicate that the elements must appear 
  within your instance document in the sequence, or order, specified. 
For example, the following would be legal: 
<first>first</first> 
<middle>middle</middle> 
<last>last</last> 
The following, however, would be illegal: 
<last>last</last> 
<middle>middle</middle> 
<first>first</first>


The minOccurs and maxOccurs attributes can be used with compositors

<xsd:complexType name="nameType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="firstName" type="xsd:string"/>
   <xsd:element name="middleName" type="xsd:string"/>
   <xsd:element name="lastName" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
By adding the maxOccurs attribute, you allow for the following structure to occur in the document instance:
<firstName>A</firstName>
<middleName>B</middleName>
<lastName>C</lastName>
<firstName>D</firstName>
<middleName>E</middleName>
<lastName>F</lastName>
<firstName>G</firstName>
<middleName>H</middleName>
<lastName>I</lastName>


The sequence compositor allows you to require a sequence for child elements in a content model

<name>
  <firstName>A</firstName>
  <middleName>B</middleName>
  <lastName>C</lastName>
</name>
To require that the child elements occur in that order, we would have to use the sequence compositor:
<xsd:element name="name">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="firstName" type="xsd:string"/>
    <xsd:element name="middleName" type="xsd:string"/>
    <xsd:element name="lastName" type="xsd:string"/>
   </xsd:sequence>
  </xsd:complexType>
</xsd:element>