XML/XML Schema/sequence

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

All inner elements in a sequence are referenced

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>41.95</cost>
          <retailer>http://www.wbex.ru</retailer>
      </product>
      <product>
          <name>name 2</name>
          <image>b.gif</image>
          <description>description 2</description>
          <cost>239.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 ref="product" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="product">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="name"/>
                <xsd:element ref="image"/>
                <xsd:element ref="description"/>
                <xsd:element ref="warranty" minOccurs="0"/>
                <xsd:element ref="weight" minOccurs="0"/>
                <xsd:element ref="cost" maxOccurs="unbounded"/>
                <xsd:element ref="retailer"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <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"/>
    <xsd:element name="weight" type="xsd:string"/>
    <xsd:element name="cost" type="xsd:string"/>
    <xsd:element name="retailer" type="xsd:string"/>
</xsd:schema>



Anonymous complexType with sequence

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="airport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="aircraft" maxOccurs="2" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



complexType with empty sequence

File: Schema.xsd
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru/statement"
  xmlns="http://www.wbex.ru/statement">
  
    <xs:attribute name="number" type="xs:ID" />
  <xs:attribute name="name"   type="xs:string" />
  <xs:attribute name="type"   type="xs:string" />
  <xs:element name="statement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="customer" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence />
      <xs:attribute ref="number" />
      <xs:attribute ref="name"  />
      <xs:attribute ref="type" use="optional" default="normal" />
    </xs:complexType>
  </xs:element>
</xs:schema>



Nested complexType and sequence

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>
        <type>F-16</type>
    </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" maxOccurs="2">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="type" type="xsd:string"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Nested sequence

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>41.95</cost>
          <retailer>http://www.wbex.ru</retailer>
      </product>
      <product>
          <name>name 2</name>
          <image>b.gif</image>
          <description>description 2</description>
          <cost>239.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:string" minOccurs="0"/>
                            <xsd:element name="cost" type="xsd:string" maxOccurs="unbounded"/>
                            <xsd:element name="retailer" type="xsd:string"/>
                        </xsd:sequence>
                     </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Reference group in a sequence

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



sequence with only one element

File: Data.xml
<?xml version="1.0"?>
<Aircraft xmlns="http://www.wbex.ru"
          xmlns:xlink="http://www.w3.org/1999/xlink/namespace"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation=
                   "http://www.wbex.ru
                    Schema.xsd">
    <AIRCRAFT-DATA>...</AIRCRAFT-DATA>
</Aircraft>
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">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="AIRCRAFT-DATA" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



sequence with unbounded elements

File: Data.xml
<?xml version="1.0"?>
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="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>
        <Book>
                <Title>title 3</Title>
                <Author>author 3</Author>
                <Date>2004</Date>
                <ISBN>0-11-111111-1</ISBN>
                <Publisher>Publisher 3</Publisher>
        </Book>
</Books>

File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
    <xsd:element name="Books">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Book">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Title"/>
                <xsd:element ref="Author"/>
                <xsd:element ref="Date"/>
                <xsd:element ref="ISBN"/>
                <xsd:element ref="Publisher"/>
            </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>