XML/XML Schema/complexType

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

Anonymous complex type and sequence

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Books xmlns="http://www.wbex.ru"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
       <Book>
               <Title>title 1</Title>
               <Author>author 1</Author>
               <Date>2008</Date>
               <ISBN>1-11111-111-1</ISBN>
               <Publisher>Publisher 1</Publisher>
       </Book>
       <Book>
               <Title>title 2</Title>
               <Author>author 2</Author>
               <Date>2007</Date>
               <ISBN>0-111-11111-1</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>

</source>
   
  


Build Complex type with simple Type

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <products xmlns ="http://www.wbex.ru"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
     <product>
         <name>name 1</name>
         <image>a.gif</image>
         <description>description 1</description>
         <warranty>lifetime warranty</warranty>
         <cost>1.1</cost>
         <retailer>http://www.wbex.ru</retailer>
     </product>
     <product>
         <name>name 2</name>
         <image>b.gif</image>
         <description>description 2</description>
         <cost>22.00</cost>
         <retailer>http://www.wbex.ru</retailer>
     </product>

</products>

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:element name="products">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element name="product" minOccurs="0" maxOccurs="unbounded">
                   <xsd:complexType>
                       <xsd:sequence>
                           <xsd:element name="name" type="xsd:string"/>
                           <xsd:element name="image" type="xsd:string"/>
                           <xsd:element name="description" type="xsd:string"/>
                           <xsd:element name="warranty" type="xsd:string" minOccurs="0"/>
                           <xsd:element name="weight" type="xsd:positiveInteger" minOccurs="0"/>
                           <xsd:element name="cost" type="money" maxOccurs="unbounded"/>
                           <xsd:element name="retailer" type="xsd:anyURI"/>
                       </xsd:sequence>
                    </xsd:complexType>
               </xsd:element>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>
   <xsd:simpleType name="money">
       <xsd:restriction base="xsd:decimal">
           <xsd:fractionDigits value="2"/>
       </xsd:restriction>
   </xsd:simpleType>

</xsd:schema>

</source>
   
  


complexType mixed="true"

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Letter xmlns="http://www.wbex.ru"

              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation=
                             "http://www.wbex.ru
                              Schema.xsd">
   <Body>
      Dear Sirs:
           This message is <emp> very </emp> useful.
   </Body>

</Letter>

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:element name="Letter">
       <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Body" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType mixed="true">
                        <xsd:sequence>
                            <xsd:element name="emp" type="xsd:string" maxOccurs="unbounded"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>

</xsd:schema>

</source>
   
  


complexType outside the element

   <source lang="xml">

<?xml version="1.0"?> <name xmlns="http://www.wbex.ru/name"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/name Schema.xsd"
 title="Mr.">
 <first>first</first>
 <middle>middle</middle>
 <last>last</last>

</name>

File: Schema.xsd <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"

 xmlns:target="http://www.wbex.ru/name"
 targetNamespace="http://www.wbex.ru/name"
 elementFormDefault="qualified">
 <complexType name="NameType">
   <sequence>
     <element name="first" type="string" />
     <element name="middle" type="string" />
     <element name="last" type="string" />
   </sequence>
   <attribute name="title" type="string" />
 </complexType>
 <element name="name" type="target:NameType" />

</schema>

</source>
   
  


Complex type with complex content

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Books xmlns="http://www.wbex.ru"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
       <Book>
               <Title>title 1</Title>
               <Author>author 1</Author>
               <Date>2008</Date>
               <ISBN>1-11111-111-1</ISBN>
               <Publisher>Publisher 1</Publisher>
       </Book>
       <Book>
               <Title>title 2</Title>
               <Author>author 2</Author>
               <Date>2007</Date>
               <ISBN>0-111-11111-1</ISBN>
               <Publisher>Publisher 2</Publisher>
       </Book>

</Books>

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:complexType name="Publication">
       <xsd:sequence>
           <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
           <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
           <xsd:element name="Date" type="xsd:gYear"/>
       </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="BookPublication">
       <xsd:complexContent>
           <xsd:extension base="Publication">
               <xsd:sequence>
                   <xsd:element name="ISBN" type="xsd:string"/>
                   <xsd:element name="Publisher" type="xsd:string"/>
               </xsd:sequence>
           </xsd:extension>
       </xsd:complexContent>
   </xsd:complexType>
   <xsd:element name="Books">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>

</xsd:schema>

</source>
   
  


Complex type with group reference

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <employees

 xmlns="http://www.wbex.ru/employees"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/employees Schema.xsd"
 source="from where"
 version="1.0">
 <employee person="persondata" tags="tag name">
   <name title="Mr.">
     <first>first</first>
     <last>last</last>
   </name>
   <location>
     <address>USA</address>
     <latitude>1</latitude>
     <longitude>2</longitude>
   </location>
   <phone kind="Home">001-111-1111</phone>
   <knows employees="name of employees"/>
   <description>dataem.
data1strong data</description> </employee>

</employees>

File: Schema.xsd <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"

 xmlns:employees="http://www.wbex.ru/employees"
 targetNamespace="http://www.wbex.ru/employees"
 elementFormDefault="qualified">
 <attributeGroup name="employeeAttributes">
   <attribute name="version" type="decimal" fixed="1.0" />
   <attribute name="source" type="string"/>
 </attributeGroup>
 <element name="employees">
   <complexType>
     <sequence>
       <element name="employee" minOccurs="0" maxOccurs="unbounded">
         <complexType>
           <sequence>
             <element name="name" type="employees:NameType"/>
             <element name="location" type="employees:LocationType"/>
             <element name="phone" type="employees:PhoneType"/>
             <element name="knows" type="employees:KnowsType"/>
             <element name="description" type="employees:DescriptionType"/>
           </sequence>
           <attribute name="tags" type="token"/>
           <attribute name="person" type="string"/>
         </complexType>
       </element>
     </sequence>
     <attributeGroup ref="employees:employeeAttributes"/>
   </complexType>
 </element>
 <complexType name="NameType">
   <group ref="employees:NameGroup"/>
   <attribute name="title" type="string"/>
 </complexType>
 <group name="NameGroup">
   <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>
 </group>
 <complexType name="LocationType">
   <choice minOccurs="0" maxOccurs="unbounded">
     <element name="address" type="string"/>
     <sequence>
       <element name="latitude" type="employees:UnknownStringOrFloatType"/>
       <element name="longitude" type="employees:UnknownStringOrFloatType"/>
     </sequence>
   </choice>
 </complexType>
 <complexType name="PhoneType">
   <simpleContent>
     <extension base="string">
       <attribute name="kind" default="Home">
         <simpleType>
           <restriction base="string">
             <enumeration value="Home"/>
             <enumeration value="Work"/>
             <enumeration value="Cell"/>
             <enumeration value="Fax"/>
           </restriction>
         </simpleType>
       </attribute>
     </extension>
   </simpleContent>
 </complexType>
 
 <complexType name="KnowsType">
   <attribute name="employees" type="string"/>
 </complexType>
 <complexType name="DescriptionType" mixed="true">
   <choice minOccurs="0" maxOccurs="unbounded">
     <element name="em" type="string"/>
     <element name="strong" type="string"/>
     <element name="br" type="string"/>
   </choice>
 </complexType>
 <simpleType name="employeeTagsType">
   <restriction base="string">
     <enumeration value="author"/>
     <enumeration value="xml"/>
     <enumeration value="poetry"/>
     <enumeration value="consultant"/>
     <enumeration value="CGI"/>
     <enumeration value="semantics"/>
     <enumeration value="employees"/>
   </restriction>
 </simpleType>
 <simpleType name="employeeTagsListType">
   <list itemType="employees:employeeTagsType"/>
 </simpleType>
 <simpleType name="UnknownString">
   <restriction base="string">
     <enumeration value="Unknown"/>
   </restriction>
 </simpleType>
 <simpleType name="UnknownStringOrFloatType">
   <union memberTypes="float employees:UnknownString"/>
 </simpleType>

</schema>

</source>
   
  


complexType with restriction

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Books xmlns="http://www.wbex.ru"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation=
                             "http://www.wbex.ru
                              Schema.xsd">
       <Publication xsi:type="MagazineType">
               <Title>title 1</Title>
               <Date>2008</Date>
       </Publication>
       <Publication xsi:type="BookType">
               <Title>title 1</Title>
               <Author>author 1</Author>
               <Date>2008</Date>
               <ISBN>1-11111-111-1</ISBN>
               <Publisher>publisher 1</Publisher>
       </Publication>
       <Publication xsi:type="BookType">
               <Title>title 3</Title>
               <Author>author 3</Author>
               <Date>2008</Date>
               <ISBN>3-33-33333-3</ISBN>
               <Publisher>publisher 3</Publisher>
       </Publication>

</Books> 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:complexType name="PublicationType" abstract="true">
       <xsd:sequence>
           <xsd:element name="Title" type="xsd:string"/>
           <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
           <xsd:element name="Date" type="xsd:gYear"/>
       </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="BookType">
       <xsd:complexContent>
           <xsd:extension base="PublicationType" >
               <xsd:sequence>
                   <xsd:element name="ISBN" type="xsd:string"/>
                   <xsd:element name="Publisher" type="xsd:string"/>
               </xsd:sequence>
           </xsd:extension>
       </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="MagazineType">
       <xsd:complexContent>
           <xsd:restriction base="PublicationType">
               <xsd:sequence>
                   <xsd:element name="Title" type="xsd:string"/>
                   <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="0"/>
                   <xsd:element name="Date" type="xsd:gYear"/>
               </xsd:sequence>
           </xsd:restriction>
       </xsd:complexContent>
   </xsd:complexType>
   <xsd:element name="Publication" type="PublicationType"/>
   <xsd:element name="Books">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element ref="Publication" maxOccurs="unbounded"/>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>

</xsd:schema>

</source>
   
  


complex type with sequence

   <source lang="xml">

<?xml version="1.0"?> <name xmlns="http://www.wbex.ru/name"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/name Schema.xsd"
 title="Mr.">
 <first>f</first>
 <middle>m</middle>
 <last>l</last>

</name>

File: Schema.xsd <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"

 xmlns:target="http://www.wbex.ru/name"
 targetNamespace="http://www.wbex.ru/name"
 elementFormDefault="qualified">
 <element name="name">
   <complexType>
     <sequence>
       <element name="first" type="string" />
       <element name="middle" type="string" />
       <element name="last" type="string" />
     </sequence>
     <attribute name="title" type="string" />
   </complexType>
 </element>

</schema>

</source>
   
  


Define a complexType

   <source lang="xml">

File: Data.xml <?xml version="1.0"?>

      <speed>
         123
      </speed>

File: Schema.xsd <?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" type="myType"/>
 
   <xs:complexType name="myType">
       <xs:sequence>
           <xs:element name="speed" type="xs:string"/>
       </xs:sequence>
   </xs:complexType>

</xs:schema>

</source>
   
  


Definition of a student element uses primitive types for the child elements

   <source lang="xml">

File: Data.xml

   <name>name 1</name>
   <address>1 Street.</address>
   <GPA>3.50</GPA>
   <active>true</active>

File: Schema.xsd <?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" type="myType"/>
 
   <xs:complexType name="myType">
       <xs:sequence>
       <xs:element name="name"    type="xs:string" />
       <xs:element name="address" type="xs:string" />
       <xs:element name="GPA"     type="xs:decimal" />
       <xs:element name="active"  type="xs:boolean" />            
       </xs:sequence>
   </xs:complexType>

</xs:schema>

</source>
   
  


Nested complexType

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <addr:address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

             xsi:schemaLocation="http://www.wbex.ru 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" type="addr:locationType"/>
             <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>

</source>