XML/XML Schema/Salami Slice

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

different types with the same name

File: Data.xml
<?xml version="1.0"?>
<BookOnCars xmlns="http://www.wbex.ru"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation=
                              "http://www.wbex.ru
                               ScopeTest.xsd">
        <Chapter>
                <Title>title</Title>
                <Section>
                    <Title>title</Title>
                </Section>
        </Chapter>
        <Title>A car</Title>
</BookOnCars>
File: ScopeTest.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="BookOnCars">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Chapter">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Title" type="xsd:string"/>
                            <xsd:element name="Section">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="Title" type="xsd:string"/>
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="Title" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="Title">
        <xsd:sequence> 
            <xsd:element name="CarManufacturer" type="xsd:string"/>
            <xsd:element name="Year" type="xsd:gYear"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="Title" type="xsd:string"/>
    <xsd:attribute name="Title" type="xsd:string"/>
</xsd:schema>



Global attibute definitions

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<statement 
 xmlns="http://www.wbex.ru/statement"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/statement Schema.xsd">
           
   <customer name="name 1" number="CUST123" type="VIP"/>
   <orders count="2">
      <order number="ORD100" owner="CUST123" total="500.00">
         <items>
            <item quantity="5" price="100">
              <description>item 1</description>
            </item>
            <item quantity="2" price="50">
              <description>item 2</description>
            </item>
         </items>
      </order>
      <order number="ORD101" owner="CUST123" total="150.00">
         <items>
            <item quantity="6" price="25">
              <description>item 3</description>
            </item>
         </items>
      </order>
   </orders>
</statement>
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:attributeGroup name="CustAttrs">
      <xs:attribute name="number" type="xs:ID" />
    <xs:attribute name="name"   type="xs:string" />
    <xs:attribute name="type"   type="xs:string" use="optional" default="normal"  />
  </xs:attributeGroup >
  <xs:element name="statement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="customer" />
        <xs:element ref="orders" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence />
      <xs:attributeGroup ref="CustAttrs" />      
    </xs:complexType>
  </xs:element>
  <xs:element name="orders">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="order"
         maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="count" 
       type="xs:nonNegativeInteger" />
    </xs:complexType>
  </xs:element>
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="items" />
      </xs:sequence>
      <xs:attribute name="number" type="xs:ID" />
      <xs:attribute name="owner"  type="xs:IDREF" />
      <xs:attribute name="total"  type="xs:decimal" />
    </xs:complexType>
  </xs:element>
  <xs:element name="items">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="item" 
         maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="description" />
      </xs:sequence>
      <xs:attribute name="quantity" type="xs:nonNegativeInteger" />
      <xs:attribute name="price"    type="xs:decimal" />
    </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string" />
</xs:schema>



Ref and Salami Slice

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>



Reuse complex type

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<statement 
 xmlns="http://www.wbex.ru/statement"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/statement statement.xsd">
           
   <customer name="name 1" number="CUST123" type="VIP"/>
   <orders count="2">
      <order number="ORD100" owner="CUST123" total="500.00">
         <items>
            <item quantity="5" price="100">
              <description>item 1</description>
            </item>
            <item quantity="2" price="50">
              <description>item 2</description>
            </item>
         </items>
      </order>
      <order number="ORD101" owner="CUST123" total="150.00">
         <items>
            <item quantity="6" price="25">
              <description>item 3</description>
            </item>
         </items>
      </order>
   </orders>
</statement>
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:element name="statement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="customer" />
        <xs:element ref="orders" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence />
      <xs:attribute name="number" type="xs:ID" />
      <xs:attribute name="name"   type="xs:string" />
      <xs:attribute name="type"   type="xs:string"
       use="optional" default="normal" />
    </xs:complexType>
  </xs:element>
  <xs:element name="orders">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="order"
         maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="count" 
       type="xs:nonNegativeInteger" />
    </xs:complexType>
  </xs:element>
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="items" />
      </xs:sequence>
      <xs:attribute name="number" type="xs:ID" />
      <xs:attribute name="owner"  type="xs:IDREF" />
      <xs:attribute name="total"  type="xs:decimal" />
    </xs:complexType>
  </xs:element>
  <xs:element name="items">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="item" 
         maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="description" />
      </xs:sequence>
      <xs:attribute name="quantity" type="xs:nonNegativeInteger" />
      <xs:attribute name="price"    type="xs:decimal" />
    </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string" />
</xs:schema>



Reuse data type defined

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 ref="DVD" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="DVD">
    <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:schema>



Reuse data type defined in another xml schema file

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:include schemaLocation="customDataType.xsd"/>
  <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>
File: customDataType.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="YesNoType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="no"/>
      <xs:enumeration value="yes"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>



Salami Slice Design

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" type="BookPublication" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="BookPublication">
        <xsd:sequence>
            <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:sequence>
    </xsd:complexType>
</xsd:schema>



Schema for XSchema elements

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<statement 
 xmlns="http://www.wbex.ru/statement"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/statement Schema.xsd">
           
   <customer/>
   <orders>
      <order>
         <items>
            <item>
              <description>item 1</description>
            </item>
            <item>
              <description>item 2</description>
            </item>
         </items>
      </order>
      <order>
         <items>
            <item>
              <description>item 3</description>
            </item>
         </items>
      </order>
   </orders>
</statement>
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:element name="statement">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="customer" />
        <xs:element ref="orders" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence />
    </xs:complexType>
  </xs:element>
  <xs:element name="orders">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="order"
         maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="items" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="items">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="item" 
         maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="description" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string" />
</xs:schema>