XML/XML Schema/element

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

Define element type outside

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
         xmlns:ex="http://www.wbex.ru"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation=
                              "http://www.wbex.ru
                               Schema.xsd">
    <aircraft>F-16</aircraft>
</airport>
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="aircraft" type="xsd:string"/>
    <xsd:element name="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="aircraft" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Element referencing another element

File: Data.xml
<?xml version="1.0"?>
<AttributeExamples xmlns="http://www.wbex.ru"
                   xmlns:i="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <image/>                 
    <image i:src="http://www.wbex.ru/style/logo.png"/>
</AttributeExamples>
File: Schema.xml
<?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="image">
        <xsd:complexType>
            <xsd:attribute ref="src"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:attribute name="src" type="xsd:anyURI"/>
    <xsd:element name="AttributeExamples">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="image" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Element with anonymous complexType and sequence

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">
  <fullName>
    <first>first</first>
    <middle>middle</middle>
    <last>last</last>
  </fullName>
  <contacts>
    <phone 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:nameType"/>
            <xsd:element name="middle" type="addr:nameType" minOccurs="0"/>
            <xsd:element name="last" type="addr:nameType"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="contacts" type="addr:contactsType" minOccurs="0"/>
    </xsd:sequence>
  <xsd:attributeGroup ref="addr:nationality"/>
  </xsd:complexType>
 </xsd:element>
 
 <xsd:complexType name="nameType">
  <xsd:simpleContent>
    <xsd:extension base="xsd:string"/>
  </xsd:simpleContent>
 </xsd:complexType>
 
  <xsd:complexType name="contactsType">
    <xsd:sequence>
      <xsd:element name="phone" minOccurs="0">
        <xsd:complexType>
          <xsd:complexContent>
            <xsd:restriction base="xsd:anyType">
              <xsd:attribute name="number" type="xsd:string"/>
            </xsd:restriction>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
 <xsd:attributeGroup name="nationality">
  <xsd:attribute name="language" type="xsd:language"/>
 </xsd:attributeGroup>
</xsd:schema>



Element with anonymous simpleType

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
                   xmlns:ex="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation=
                              "http://www.wbex.ru
                               Schema.xsd">
    <aircraft/>              
    <aircraft>F-16</aircraft>
</airport>
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="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="aircraft" default="Boeing 747"  maxOccurs="2">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="Boeing 747"/>
                            <xsd:enumeration value="F-16"/>
                            <xsd:enumeration value="A-10"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Element with fixed value

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
         xmlns:ex="http://www.wbex.ru"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <aircraft/>                
    <aircraft>Boeing 747</aircraft>
</airport>
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="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="aircraft" fixed="Boeing 747"  maxOccurs="2" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



element with maxOccurs="unbounded"

File: Data.xml
<?xml version="1.0"?>
<Book xmlns="http://www.wbex.ru"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation= "http://www.wbex.ru Schema.xsd">
    <Title>title 1</Title>
    <Author>author 1</Author>
    <Author>author 2</Author>
    <Date>1993</Date>
    <ISBN>1-11111-111-1</ISBN>
    <Publisher>publisher 1</Publisher>
</Book>

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="Book" type="bookType"/>
        
    <xsd:complexType name="bookType">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="Author" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
            <xsd:element name="Date" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="ISBN" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="Publisher" type="xsd:string" minOccurs="1" maxOccurs="1"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>



Reference element as type

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
                   xmlns:ex="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <aircraft/>                
    <aircraft>F-16</aircraft>  
</airport>
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="aircraft" default="Boeing 747" type="xsd:string"/>
    <xsd:element name="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="aircraft" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Reference element based on simpleType

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
                   xmlns:ex="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation= "http://www.wbex.ru Schema.xsd">
    <aircraft/>              
    <aircraft>F-16</aircraft>
</airport>
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="aircraft" default="Boeing 747">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="Boeing 747"/>
                <xsd:enumeration value="F-16"/>
                <xsd:enumeration value="A-10"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>
    <xsd:element name="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="aircraft" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Schema for elements with inner elements and attribute

File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="BOOK">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="TITLE" type="xsd:string"/>
            <xsd:element name="AUTHOR" type="xsd:string"/>
            <xsd:element name="BINDING" type="xsd:string"/>
            <xsd:element name="PAGES" type="xsd:positiveInteger"/>
            <xsd:element name="PRICE" type="xsd:decimal"/>
         </xsd:sequence>
         <xsd:attribute name="InStock" type="xsd:boolean" 
            use="required"/>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

File: Data.xml

<?xml version="1.0"?>
<BOOK InStock="true">
   <TITLE>title 1</TITLE>
   <AUTHOR>author 1</AUTHOR>
   <BINDING>trade paperback</BINDING>
   <PAGES>73</PAGES>
   <PRICE>1.95</PRICE>
</BOOK>



XML schema for list of tags with identical structure

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
  <DVD id="1">
    <title>title 1</title>
    <format>Movie</format>
    <genre>Classic</genre>
  </DVD>
  <DVD id="2">
    <title>Contact</title>
    <format>Movie</format>
    <genre>Science fiction</genre>
  </DVD>
</library>

File: Schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="DVD" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="format" type="xs:string"/>
              <xs:element name="genre" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:integer" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>