XML/XML Schema/simpleType

Материал из Web эксперт
Версия от 08:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

attribute based on simpleType

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
  <firstelement position="1">
    <level1 children="0">
      This is level 1 of the nested elements
    </level1>
  </firstelement>
  <secondelement position="2">
    <level1 children="1">
      <level2>This is level 2 of the nested elements</level2>
    </level1>
  </secondelement>
</data>
File: Schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">
  <xs:element name="firstelement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="level1" />
      </xs:sequence>
      <xs:attribute name="position" type="xs:boolean"
        use="required" />
    </xs:complexType>
  </xs:element>
  <xs:element name="level1">
    <xs:complexType mixed="true">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="level2" />
      </xs:choice>
      <xs:attribute name="children" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:NMTOKEN">
            <xs:enumeration value="0" />
            <xs:enumeration value="1" />
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:element name="level2" type="xs:string" />
  <xs:element name="data">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="firstelement" />
        <xs:element ref="secondelement" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="secondelement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="level1" />
      </xs:sequence>
      <xs:attribute name="position" type="xs:byte" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>



Create simpleType with restriction on another simple type

File: Data.xml
<?xml version="1.0"?>
<addr:address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.wbex.ru 
      address-schema.xsd"
    xmlns:addr="http://www.wbex.ru"
    addr:language="en"
    addr:ssn="123-45-6789">
  <fullName>
    <first>W</first>
    <middle>S</middle>
    <last>M</last>
  </fullName>
  <contacts>
    <phone addr:location="home" addr:number="111.222.3333"/>
  </contacts>
</addr:address>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru"
  xmlns:addr="http://www.wbex.ru"
  attributeFormDefault="qualified">
 <xsd:element name="address">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="fullName">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="first" type="addr:nameComponent"/>
            <xsd:element name="middle" type="addr:nameList"
                minOccurs="0"/>
            <xsd:element name="last" type="addr:nameComponent"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="contacts" type="addr:contactsType" minOccurs="0"/>
    </xsd:sequence>
  <xsd:attributeGroup ref="addr:nationality"/>
  <xsd:attribute name="ssn" type="addr:ssn"/>
  </xsd:complexType>
 </xsd:element>
 
 <xsd:complexType name="nameComponent">
  <xsd:simpleContent>
    <xsd:extension base="addr:nameString"/>
  </xsd:simpleContent>
 </xsd:complexType>
 <xsd:complexType name="nameList">
  <xsd:simpleContent>
    <xsd:extension base="addr:nameListType"/>
  </xsd:simpleContent>
 </xsd:complexType>
 
 <xsd:simpleType name="nameListType">
  <xsd:list itemType="addr:nameString"/>
 </xsd:simpleType>
 
 <xsd:simpleType name="nameString">
  <xsd:restriction base="xsd:string">
    <xsd:maxLength value="50"/>
  </xsd:restriction>
 </xsd:simpleType>
 
 <xsd:simpleType name="ssn">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="\d\d\d-\d\d-\d\d\d\d"/>
  </xsd:restriction>
 </xsd:simpleType>
 
  <xsd:complexType name="contactsType">
    <xsd:sequence>
      <xsd:element name="phone" minOccurs="0">
        <xsd:complexType>
          <xsd:complexContent>
            <xsd:restriction base="xsd:anyType">
              <xsd:attribute name="location">
                <xsd:simpleType>
                  <xsd:union memberTypes="addr:locationType xsd:NMTOKEN"/>
                </xsd:simpleType>
              </xsd:attribute>
              <xsd:attribute name="number" type="xsd:string"/>
            </xsd:restriction>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="locationType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="work"/>
      <xsd:enumeration value="home"/>
      <xsd:enumeration value="mobile"/>
    </xsd:restriction>      
  </xsd:simpleType>
  
 <xsd:attributeGroup name="nationality">
  <xsd:attribute name="language" type="xsd:language"/>
 </xsd:attributeGroup>
</xsd:schema>



Define simple type outside

File: Data.xml
<?xml version="1.0"?>
<Books xmlns="http://www.wbex.ru">
        <Book>
                <Title>title 1</Title>
                <Author>author 1</Author>
                <Date>1998</Date>
                <ISBN>1-11111-111-1</ISBN>
                <Publisher>publisher 1</Publisher>
        </Book>
        <Book>
                <Title>title 2</Title>
                <Author>author 2</Author>
                <Date>1977</Date>
                <ISBN>2-222-22222-2</ISBN>
                <Publisher>publisher 2</Publisher>
        </Book>
</Books>
File: Schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:element name="Books">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Book">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Title" type="xsd:string"/>
    <xsd:element name="Author" type="xsd:string"/>
    <xsd:element name="Date" type="xsd:string"/>
    <xsd:element name="ISBN" type="xsd:string"/>
    <xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>



Derived simple type

File: Data.xml
<?xml version="1.0"?>
<data xmlns="http://www.wbex.ru"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
        <Drawing>
                <Week>July 1</Week>
                <Numbers>21 3 67 8 90 12</Numbers>
        </Drawing>
        <Drawing>
                <Week>July 8</Week>
                <Numbers>55 31 4 57 98 22</Numbers>
        </Drawing>
</data>

File: Schema.xsd
<?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:simpleType name="OneToNinetyNine">
        <xsd:restriction base="xsd:positiveInteger">
            <xsd:maxInclusive value="99"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <xsd:simpleType name="NumbersList">
        <xsd:list itemType="OneToNinetyNine"/>
    </xsd:simpleType>
    
    <xsd:simpleType name="LotteryNumbers">
        <xsd:restriction base="NumbersList">
            <xsd:length value="6"/>
        </xsd:restriction>
    </xsd:simpleType>
    
         
    <xsd:element name="data">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="Drawing" minOccurs="0" maxOccurs="unbounded">
                     <xsd:complexType>
                         <xsd:sequence>
                             <xsd:element name="Week" type="xsd:string"/>
                             <xsd:element name="Numbers" type="LotteryNumbers"/>
                         </xsd:sequence>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>