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

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

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

Deriving by Restriction

   <source lang="xml">

<xsd:complexType name="nameType">

  <xsd:element name="firstName" type="xsd:string"/>
  <xsd:element name="middleName" type="xsd:string"/>
  <xsd:element name="lastName" type="xsd:string"/>

</xsd:complexType> <xsd:complexType name="nameTypeRestricted">

  <xsd:restriction base="nameType">
    <xsd:element name="firstName" type="xsd:string"/>
    <xsd:element name="lastName" type="xsd:string"/>


Deriving Custom Simple Types

   <source lang="xml">

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


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></source>

restriction specifies values for zero or more constraining facets

   <source lang="xml">

<xsd:element name="grades">

  <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"/>


Restriction types are declared using the <restriction> declaration.

   <source lang="xml">

<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">

   <restriction base="string">
     <enumeration value="Home" />
     <enumeration value="Work" />
     <enumeration value="Cell" />
     <enumeration value="Fax" />