XML Tutorial/XML Schema/restriction
Содержание
Deriving by Restriction
<source lang="xml">
<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></source>
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" 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></source>
restriction specifies values for zero or more constraining facets
<source lang="xml">
<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></source>
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">
<simpleType> <restriction base="string"> <enumeration value="Home" /> <enumeration value="Work" /> <enumeration value="Cell" /> <enumeration value="Fax" /> </restriction> </simpleType>
</attribute></source>