Latest web development tutorials

XML Schema defines / Facets

Limited (restriction) is used to define acceptable XML element or attribute values. To define XML elements are called facet.


Limiting values

The following example defines an element with a defined and named "age" of. The value of age can not be lower than 0 or greater than 120:

<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


Defining a set of values

For the contents of the XML element is limited to a set of acceptable values, we would use the enumeration constraint (enumeration constraint).

The following example defines an element with a qualified name "car" is. Only acceptable values ​​are: Audi, Golf, BMW:

<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The above example can also be written as:

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>

Note: In this case, type "carType" can be used by other elements because it is not part of the "car" element.


A series of limited value

To limit the content of an XML element is defined as a series of numbers or letters that can be used, we want to use the mode confinement (pattern constraint).

The following example defines an element with a defined called "letter" of. The only acceptable values ​​lowercase letters a - z one:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The next example defines an element with a defined called "initials" of. Acceptable values ​​are uppercase letters A - Z in which three of them:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The next example also defines an element with a defined called "initials" of. Acceptable values ​​are uppercase or lowercase letters a - z three of them:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The next example defines an element with a limited choice called "acceptable value is the letters x, y or z in a:

<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The next example defines an element with a qualified name "prodid" of. Acceptable values ​​are a sequence of five digits, and each number in the range of 0-9:

<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


A series of values ​​defined for other

The following example defines an element with a defined called "letter" of. Acceptable values ​​are a - z zero or more letters:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The following example defines an element with a defined called "letter" of. Acceptable values ​​are one or more pairs of letters, each pair consists of a lowercase letter followed by an uppercase letter. For example, "sToP" will validate this model, but the "Stop", "STOP" or "stop" can not be verified:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The following example defines an element with a defined called "gender" is. Acceptable values ​​are male or female:

<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The following example defines an element with a qualified name "password" of. Acceptable values ​​are a row of characters consists of eight characters, these characters must be uppercase or lowercase letters a - z or will digital 0--9:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


Blank character defined

To specify white space characters (whitespace characters) approach, we need to use whiteSpace limited.

The following example defines a named "address" element with a defined. This whiteSpace Limited is set to "preserve", which means that the XML processor will not remove any white space characters:

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

This example also defines a named "address" element with a defined. This whiteSpace Limited is set to "replace", which means that the XML processor will remove all white space characters (line breaks, carriage returns, spaces, and tabs):

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

This example also defines a named "address" element with a defined. This whiteSpace Limited is set to "collapse", which means that the XML processor will remove all white space characters (line breaks, carriage returns, spaces, and tabs are replaced with spaces, leading and trailing spaces will be removed, and multiple consecutive spaces are reduced to a single space):

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


Of defined length

To limit the length of the element values, we need to use the length, maxLength and minLength limited.

Elements with a defined and named "password" in this case is defined. Its value must be exactly eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

This example also defines an element with a qualified name "password" of. Its value is a minimum of 5 characters and a maximum of eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


Defined data types

limited description
enumeration Define a list of acceptable values
fractionDigits It defines the maximum allowed number of decimal places. It must be greater than 0.
length Define the exact number of allowed characters or list items. It must be greater than or equal to 0.
maxExclusive Define the upper limit value. Permitted value must be less than this value.
maxInclusive Define the upper limit value. Allowed value must be less than or equal to this value.
maxLength Defines the maximum number of allowed characters or list items. It must be greater than or equal to 0.
minExclusive The lower limit value is defined. The value must be allowed greater than this value.
minInclusive The lower limit value is defined. Allowed value must greater than or equal to this value.
minLength Defines the allowed minimum number of characters or list items. It must be greater than or equal to 0.
pattern Define the exact sequence of acceptable characters.
totalDigits It defines the allowed precision-digit Arabic numerals. It must be greater than 0.
whiteSpace The definition of white space characters (line breaks, carriage returns, tabs and spaces) are handled.