Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML Schema Qualifying / Facets


May 28, 2021 XML Schema


Table of contents


XSD Qualifying / Facets

You can define qualified values for elements or properties in XML.

Qualifiers are used to define acceptable values for XML elements or properties. The qualification of an XML element is called facet.


The qualification of the value

The following example defines an element with a qualification and the name "age". The value of age cannot be less than 0 or higher 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>


The qualification of a set of values

To limit the contents of an XML element to an acceptable set of values, we use enumeration constraints.

The following example defines an element with a qualified name called "car". 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, the type "carType" can be used by other elements because it is not part of the "car" element.


The qualification of a series of values

To define the content limits of XML elements as a series of available numbers or letters, we use pattern constraints.

The following example defines an element with a qualified name called "letter". The acceptable value is only the lowercase letter a - z one of them:

<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 qualification called "initials". The acceptable value is three of the capital letters A - Z:

<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 qualification called "initials". The acceptable values are three of the capital or lowercase letters a - z:

<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 qualification called "choice." The acceptable value is one of the letters x, y, or z:

<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 qualification called "prodid". The acceptable value is a sequence of five Arabic numerals, and the range of each number is 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>


Additional qualifications for a series of values

The following example defines an element with a qualified name called "letter". The acceptable value is zero or more letters in a - z:

<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 qualified name called "letter". A n acceptable value is a pair or more of letters, each consisting of a lowercase letter followed by a capital letter. For example, "sToP" will pass this pattern, but "Stop," "STOP," or "stop" will not:

<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 qualified name called "gender". The acceptable value is 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 called "password". The acceptable value is a line of 8 characters that must be capital or lowercase a -z or the number 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>


The qualification of blank characters

To specify how whitespace characters are handled, we need to use whiteSpace qualification.

The following example defines an element with a qualified name called "address". This whiteSpace qualification is set to "preserve," which means that the XML processor does not remove any blank 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 an element with a qualified name called "address". This whiteSpace qualification is set to "replace," which means that the XML processor removes all blank 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 an element with a qualified name called "address". This whiteSpace qualification is set to "collapse," which means that the XML processor will remove all blank characters (line breaks, carriage returns, spaces, and tabs will be replaced with spaces, spaces at the beginning and end will be removed, and multiple consecutive spaces will be 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>


Limit the length

To limit the length of the element's middle value, we need to use length, maxLength, and minLength qualification.

This example defines an element with a qualified name called "password". Its value must be accurate to 8 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 called "password". It has a minimum value of 5 characters and a maximum of 8 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>


The qualification of the data type

Qualified Describe
enumeration Define a list of acceptable values
fractionDigits Defines the maximum number of scales allowed. Must be greater than or equal to 0.
length Defines the exact number of characters or list items that are allowed. Must be greater than or equal to 0.
maxExclusive Defines the upper limit of the value. The allowed value must be less than this value.
maxInclusive Defines the upper limit of the value. The allowed value must be less than or equal to this value.
maxLength Defines the maximum number of characters or list items allowed. Must be greater than or equal to 0.
minExclusive Defines the lower limit of the value. The allowed value must be greater than this value.
minInclusive Defines the lower limit of the value. The allowed value must be greater than or equal to this value.
minLength Defines the minimum number of characters or list items allowed. Must be greater than or equal to 0.
pattern Defines an exact sequence of acceptable characters.
totalDigits Defines the exact number of arabic numerals allowed. Must be greater than 0.
whiteSpace Defines how blank characters (line breaks, carriage returns, spaces, and tabs) are handled.