XML Tutorial/XML Schema/Salami Slice

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

An XML Schema Document Using Named Type Definitions

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  targetNamespace="http://www.wbex.ru/namespaces/pub"
  xmlns="http://www.wbex.ru/namespaces/pub">
  <xsd:simpleType name="isbnType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[0-9]{10}" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:complexType name="pubType">
    <xsd:sequence>
      <xsd:element ref="book" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="bookType">
    <xsd:sequence>
      <xsd:element ref="title" />
      <xsd:element ref="author" />
      <xsd:element ref="description" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="publications" type="pubType" />
  <xsd:element name="book" type="bookType" maxOccurs="unbounded" />
  <xsd:element name="title" type="xsd:string" />
  <xsd:element name="author" type="xsd:string" />
  <xsd:element name="description" type="xsd:string" />
</xsd:schema>


extend the complex type that we have defined, creating new types for Customer and Employee:

<?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:complexType name = "PersonType">
       <xs:sequence>
          <xs:element name = "Name" type = "xs:string" />
          <xs:element name = "Address" type = "xs:string" />
          <xs:element name = "Econtact" type = "xs:string" />
       </xs:sequence>
    </xs:complexType>
       
    <xs:element name = "Customer">
       <xs:complexType>
       <xs:complexContent>
          <xs:extension base = "PersonType">
             <xs:sequence>
                <xs:element name = "AccountDetails" type = "xs:string" />
             </xs:sequence>
             <xs:attribute name = "customerID" type = "xs:string" />
          </xs:extension>
       </xs:complexContent>
       </xs:complexType>
    </xs:element>
       
    <xs:element name = "Employee">
       <xs:complexType>
       <xs:complexContent>
          <xs:extension base = "PersonType">
             <xs:sequence>
                <xs:element name = "JobRole" type = "xs:string" />
                <xs:element name = "Department" type = "xs:string" />
             </xs:sequence>
          </xs:extension>
       </xs:complexContent>
       </xs:complexType>
    </xs:element>
</xs:schema>


how XML Schema allows us to re-use names, and give them different content models.

<?xml version = "1.0" ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
           targetNamespace = "http://www.wbex.ru/books"
           xmlns:book = "http://www.wbex.ru/books"
           elementFormDefault = "qualified">
   
   <xs:element name = "Book">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="TitleInformation" type = "book:TitleInformationType" />
            <xs:element name="Chapter" type = "book:ChapterType" minOccurs = "1" maxOccurs = "unbounded" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   
   <xs:complexType name = "TitleInformationType">
      <xs:sequence>
         <xs:element name = "Title" type = "xs:string" />
         <xs:element name = "Author" type = "xs:string" />
         <xs:element name = "Publisher" type = "xs:string" />
      </xs:sequence>
   </xs:complexType>
   
   <xs:complexType name = "ChapterType">
      <xs:sequence>
         <xs:element name = "Title">
            <xs:complexType>
               <xs:simpleContent>
                  <xs:extension base = "xs:string">
                     <xs:attribute name = "indexMark" type = "xs:ID" use = "required" />
                  </xs:extension>
               </xs:simpleContent>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
      <xs:attribute name = "chapterNumber" type = "xs:integer" />
   </xs:complexType>
   
</xs:schema>


Re-use in Text-only Element Content Models

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 = "AccountReviewYear">
       <xs:simpleType>
          <xs:restriction base = "xs:gYear">
             <xs:pattern value="200[1-9]"/>
          </xs:restriction>
       </xs:simpleType>
    </xs:element>
     
    <xs:simpleType name = "fixedLengthFieldType">
       <xs:restriction base = "xs:string">
          <xs:minLength value = "0" />
          <xs:maxLength value = "256" />
       </xs:restriction>
    </xs:simpleType>
       
    <xs:element name = "ClientName" type = "fixedLengthFieldType" />
    <xs:element name = "BranchName" type = "fixedLengthFieldType" />
</xs:schema>


Salami Slice design Define global element declarations that are later referenced.

<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="Book">
  <xsd:complexType>
   <xsd:sequence>
   <xsd:element ref="title"/>
    <xsd:element ref="author"/>
    <xsd:element ref="description"/>
   </xsd:sequence>
  </xsd:complexType>
</xsd:element>


Schema reuse

File: Data.xml
<?xml version = "1.0" ?>
<Customer customerID = "242552">
   <Name>
      <FirstName>A</FirstName>
      <Middle>G</Middle>
      <LastName>C</LastName>
   </Name>
   <Address>
      <Street1>10 Place</Street1>
      <Town>D</Town>
      <City>E</City>
      <State>F</State>
      <Country>Australia</Country>
      <Postcode>2021</Postcode>
   </Address>
</Customer>
File: Data2.xml
<?xml version = "1.0" ?>
<Employee employeeID = "133">
   <Name>
      <FirstName>A</FirstName>
      <Middle>G</Middle>
      <LastName>B</LastName>
   </Name>
   <Address>
      <Street1>10 Place</Street1>
      <Town>small</Town>
      <City>big</City>
      <State>NS</State>
      <Country>Australia</Country>
      <Postcode>2021</Postcode>
   </Address>
</Employee>
File: Schema.xsd
<?xml version = "1.0" ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   
   <xs:element name = "Customer">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref = "Name" />
            <xs:element ref = "Address" />
         </xs:sequence>
         <xs:attribute name = "customerID" type = "integer" />
      </xs:complexType>
   </xs:element>
   
   <xs:element name = "Employee">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref = "Name" />
            <xs:element ref = "Address" />
         </xs:sequence>
         <xs:attribute name = "employeeID" type = "integer" />
      </xs:complexType>
   </xs:element>
   
   <xs:element name = "Name">
      <xs:complexType>
         <xs:sequence>
            <xs:element name = "FirstName" type = "string" />
            <xs:element name = "Middle" type = "string" />
            <xs:element name = "LastName" type = "string" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   
   <xs:element name = "Address">
      <xs:complexType>
         <xs:sequence>
            <xs:element name = "Street1" type = "string" />
            <xs:element name = "Town" type = "string" />
            <xs:element name = "City" type = "string" />
            <xs:element name = "State" type = "string" />
            <xs:element name = "Country" type = "string" />
            <xs:element name = "Postcode" type = "string" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   
</xs:schema>