XML Tutorial/XML Schema/restriction — различия между версиями

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

Текущая версия на 11:26, 26 мая 2010

Deriving by Restriction

<xsd:complexType name="nameType">
  <xsd:sequence>
   <xsd:element name="firstName" type="xsd:string"/>
   <xsd:element name="middleName" type="xsd:string"/>
   <xsd:element name="lastName" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="nameTypeRestricted">
  <xsd:complexContent>
   <xsd:restriction base="nameType">
    <xsd:sequence>
     <xsd:element name="firstName" type="xsd:string"/>
     <xsd:element name="lastName" type="xsd:string"/>
    </xsd:sequence>
   </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>


Deriving Custom Simple Types

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:simpleType name="zipcodeType">
    <xs:restriction base="xs:string">
      <xs:pattern value="\d{5}(-\d{4})?" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="zipcode" type="zipcodeType" />
</xs:schema>

File: Data.xml (Both of these zipcode elements are valid.)
<?xml version="1.0"?>
<zipcode>11111</zipcode>
<zipcode>11111-0987</zipcode>
File: Data.xml (Both of these zipcode elements are invalid.)
<?xml version="1.0"?>
<zipcode>1111-12349</zipcode>
<zipcode>111001</zipcode>


restriction specifies values for zero or more constraining facets

<!-- schema -->
<xsd:element name="grades">
  <xsd:simpleType>
   <xsd:restriction base="xsd:string">
    <xsd:enumeration value="A"/>
    <xsd:enumeration value="B"/>
    <xsd:enumeration value="C"/>
    <xsd:enumeration value="D"/>
    <xsd:enumeration value="F"/>
   </xsd:restriction>
  </xsd:simpleType>
</xsd:element>


Restriction types are declared using the <restriction> declaration.

<restriction base="name of the simpleType you are deriving from"> 

A derived type declared using the <restriction> declaration is a subset of its base type. 
Facets control all simple types within XML Schemas. 
A facet is a single property or trait of a <simpleType>. 
For example, the built-in numeric type nonNegativeIntegerwas created by deriving from the built-in Integer type and setting the facet minInclusiveto zero. 

There are 12 constraining facets, described in the following table: 
Facet                 
minExclusive          
minInclusive          
maxExclusive          
maxInclusive          
totalDigits           
fractionDigits        
length                
minLength             
maxLength             
enumeration           
whiteSpace            
pattern               

Within a <restriction> declaration, you must specify the type you are restricting using the base attribute. 
The base attribute is a reference to a global <simpleType> definition or built-in XML Schema datatype. 
<attribute name="kind">
  <simpleType>
    <restriction base="string">
      <enumeration value="Home" />
      <enumeration value="Work" />
      <enumeration value="Cell" />
      <enumeration value="Fax" />
    </restriction>
  </simpleType>
</attribute>