User-derived Simple Types

Contact Us or call 1-877-932-8228
User-derived Simple Types

User-derived Simple Types

A schema author can derive a new simple type using the <xs:simpleType> element. This simple type can then be used in the same way that built-in simple types are.

Simple types are derived by restricting built-in simple types or other user-derived simple types. For example, you might want to create a simple type called password that is an eight-character string. To do so, you would start with the xs:string type and restrict its length to eight characters. This is done nesting the <xs:restriction> element inside of the <xs:simpleType> element.

Code Sample:

SimpleTypes/Demos/Password.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Password">
		<xs:restriction base="xs:string">
			<xs:length value="8"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="User">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="PW" type="Password"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/Password.xml
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Password.xsd">
	<PW>MyPasWrd</PW>
</User>

Applying Facets

Simple types can be derived by applying one or more of the following facets.

  • length
  • minLength
  • maxLength
  • pattern
  • enumeration
  • whiteSpace
  • minInclusive
  • minExclusive
  • maxInclusive
  • maxExclusive
  • totalDigits
  • fractionDigits

Controlling Length

The length of a string can be controlled with the length, minLength, and maxLength facets. We used the length facet in the example above to create a Password simple type as an eight-character string. We could use minLength and maxLength to allow passwords that were between six and twelve characters in length.

The schema below shows how this is done. The two XML instances shown below it are both valid, because the length of the password is between six and twelve characters.

Code Sample:

SimpleTypes/Demos/Password2.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Password">
		<xs:restriction base="xs:string">
			<xs:minLength value="6"/>
			<xs:maxLength value="12"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="User">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="PW" type="Password"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/Password2.xml
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Password2.xsd">
	<PW>MyPass</PW>
</User>

Code Sample:

SimpleTypes/Demos/Password2b.xml
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Password2.xsd">
	<PW>MyPassWord</PW>
</User>

Specifying Patterns

Patterns are specified using the xs:pattern element and regular expressions. For example, you could use the xs:pattern element to restrict the Password simple type to consist of between six and twelve characters, which can only be lowercase and uppercase letters and underscores.

Code Sample:

SimpleTypes/Demos/Password3.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Password">
		<xs:restriction base="xs:string">
			<xs:pattern value="[A-Za-z_]{6,12}"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="User">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="PW" type="Password"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/Password3.xml
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="Password3.xsd">
	<PW>MyPassword</PW>
</User>

Working with Numbers

Numeric simple types can be derived by limiting the value to a certain range using minExclusive, minInclusive, maxExclusive, and maxInclusive. You can also limit the total number of digits and the number of digits after the decimal point using totalDigits and fractionDigits, respectively.

Mins and Maxs

The following example shows how to derive a simple type called Salary, which is a decimal between 10,000 and 90,000.

Code Sample:

SimpleTypes/Demos/Employee.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Salary">
		<xs:restriction base="xs:decimal">
			<xs:minInclusive value="10000"/>
			<xs:maxInclusive value="90000"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Salary" type="Salary"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/JohnSmith.xml
<?xml version="1.0"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Employee.xsd">
	<Salary>55000</Salary>
</Employee>

Number of Digits

Using totalDigits and fractionDigits, we can further specify that the Salary type should consist of seven digits, two of which come after the decimal point. Both totalDigits and fractionDigits are maximums. That is, if totalDigits is specified as 5 and fractionDigits is specified as 2, a valid number could have no more than five digits total and no more than two digits after the decimal point.

Code Sample:

SimpleTypes/Demos/Employee2.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Salary">
		<xs:restriction base="xs:decimal">
			<xs:minInclusive value="10000"/>
			<xs:maxInclusive value="90000"/>
			<xs:fractionDigits value="2"/>
			<xs:totalDigits value="7"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Salary" type="Salary"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/MarySmith.xml
<?xml version="1.0"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Employee2.xsd">
	<Salary>55000.00</Salary>
</Employee>

Enumerations

A derived type can be a list of possible values. For example, the JobTitle element could be a list of pre-defined job titles.

Code Sample:

SimpleTypes/Demos/Employee3.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Salary">
		<xs:restriction base="xs:decimal">
			<xs:minInclusive value="10000"/>
			<xs:maxInclusive value="90000"/>
			<xs:fractionDigits value="2"/>
			<xs:totalDigits value="7"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="JobTitle">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Sales Manager"/>
			<xs:enumeration value="Salesperson"/>
			<xs:enumeration value="Receptionist"/>
			<xs:enumeration value="Developer"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Salary" type="Salary"/>
				<xs:element name="Title" type="JobTitle"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/SteveSmith.xml
<?xml version="1.0"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Employee3.xsd">
	<Salary>90000.00</Salary>
	<Title>Sales Manager</Title>
</Employee>

Whitespace-handling

By default, whitespace in elements of the datatype xs:string is preserved in XML documents; however, this can be changed for datatypes derived from xs:string. This is done with the xs:whiteSpace element, the value of which must be one of the following.

  • preserve - whitespace is not normalized. That is to say, it is kept as is.
  • replace - all tabs, line feeds, and carriage returns are replaced by single spaces.
  • collapse - all tabs, line feeds, and carriage returns are replaced by single spaces and then all groups of single spaces are replaced with one single space. All leading and trailing spaces are then removed (i.e, trimmed).

In SimpleTypes/Demos/Password.xsd, we looked at restricting the length of a Password datatype to eight characters using the xs:length element. If whitespace is preserved, then leading and trailing spaces are considered part of the password. In the following example, we set xs:whiteSpace to collapse, thereby discounting any leading or trailing whitespace. As you can see, this allows the XML instance author to format the document without consideration of whitespace.

Code Sample:

SimpleTypes/Demos/Password4.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="Password">
		<xs:restriction base="xs:string">
			<xs:length value="8"/>
			<xs:whiteSpace value="collapse"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="User">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="PW" type="Password"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Code Sample:

SimpleTypes/Demos/Password4.xml
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Password4.xsd">
	<PW>
		12345678
	</PW>
</User>
Next