Simple Type Uses and Restrictions
R. Alexander Milowski
milowski at sims.berkeley.edu
#1
Uses - Types of Attributes
All attributes have a simple type--no complex types!
You can refer to your own simple type:
<xs:attribute name="href" type="my:domainURI"/>
Or you can embed the type definition
<xs:attribute name="href"> <xs:simpleType> <xs:restriction base="xs:anyURI"> <xs:pattern value="http:.*"/> </xs:resritction> </xs:simpleType> </xs:attribute>
#2
Uses - Types of Elements
Elements can have simple typed content:
You can refer to your own simple type:
<xs:element name="uri" type="my:domainURI"/>
Or you can embed the type definition
<xs:element name="uri"> <xs:simpleType> <xs:restriction base="xs:anyURI"> <xs:pattern value="http:.*"/> </xs:resritction> </xs:simpleType> </xs:element>
#3
Uses - Base Types of Simple Types
You can build up simple types by using your own type as the base.
The facets are added--not replaced:
<xs:simpleType name="cdeURI"> <xs:restriction base="my:absoluteURI"> <xs:pattern value="http://cde.berkeley.edu/.*"/> </xs:resritction> </xs:simpleType>
#4
Uses - Base Types of Complex Types
You can simple type element content by using the simpleContent element.
Here you can define an anonymous simple type:
<xs:complexType name="uriElement"> <xs:simpleContent> <xs:restriction base="my:absoluteURI"> <xs:pattern value="http://cde.berkeley.edu/.*"/> </xs:restriction> </xs:simpleContent> </xs:complexType> <xs:element name="uri" type="my:uriElement"/>
This still creates an anonymous type:
<xs:complexType name="uriElement"> <xs:simpleContent> <xs:restriction base="my:cdeURI"/> </xs:simpleContent> </xs:complexType> <xs:element name="uri" type="my:uriElement"/>
For the above, just use the simple type directly in the element declaration:
<xs:element name="uri" type="my:cdeURI"/>
This avoids the anonymous type and the unnecessary creation of a complex type.
#5
Uses Review
The type of an element.
The type of an attribute.
As the base type of another simple type.
As the base type of another complex type.
#6
Occurrences - For Elements/Attributes
As a global type:
<xs:schema targetNamespace="..."> <xs:simpleType name="myType">...</xs:simpleType> </xs:schema>
Inside an element or attribute declaration:
<xs:schema targetNamespace="..."> <xs:element name="data"> <xs:simpleType>...</xs:simpleType> </xs:element> <xs:complexType name="ElementWithAttribute"> <xs:attribute name="href"> <xs:simpleType>...</xs:simpleType> </xs:attribute> </xs:complexType> </xs:schema>
#7
Occurrences - For Base Types
As the base type declared inside the type:
<xs:simpleType name="uri"> <xs:restriction> <xs:simpleType> <xs:restriction base="xs:anyURI"> <xs:pattern name="\w+:.*"/> </xs:restriction> </xs:simpleType> <xs:enumeration value="http://cde.berkeley.edu/"> <xs:enumeration value="http://sims.berkeley.edu/"> </xs:restriction> </xs:simpleType>
Why you would ever want to do this...
...maybe it is there for completeness: "everywhere you can refer to a type you can give an explicit definition of that type instead"
#8
Occurrences - For Lists
As the type inside a list:
<xs:simpleType name="values"> <xs:list> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="one"/> <xs:enumeration value="two"/> <xs:enumeration value="three"/> </xs:restriction> </xs:simpleType> </xs:list> </xs:simpleType>
versus:
<xs:simpleType name="values"> <xs:list itemType="my:tokenValues"/> </xs:simpleType> <xs:simpleType name="tokenValues"> <xs:restriction base="xs:token"> <xs:enumeration value="one"/> <xs:enumeration value="two"/> <xs:enumeration value="three"/> </xs:restriction> </xs:simpleType>
As the type inside a union:
<xs:simpleType name="values"> <xs:union> <xs:simpleType> <xs:restriction base="xs:int"> <xs:minInclusive value="1"/> <xs:minInclusive value="3"/> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="one"/> <xs:enumeration value="two"/> <xs:enumeration value="three"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>
#9
Occurrences - For Unions
As the type inside a union:
<xs:simpleType name="values"> <xs:union> <xs:simpleType> <xs:restriction base="xs:int"> <xs:minInclusive value="1"/> <xs:minInclusive value="3"/> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="one"/> <xs:enumeration value="two"/> <xs:enumeration value="three"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>
versus:
<xs:simpleType name="values"> <xs:union memberTypes="my:intValues my:tokenValues"/> </xs:simpleType> <xs:simpleType name="intValues"> <xs:restriction base="xs:int"> <xs:minInclusive value="1"/> <xs:minInclusive value="3"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="tokenValues"> <xs:restriction base="xs:token"> <xs:enumeration value="one"/> <xs:enumeration value="two"/> <xs:enumeration value="three"/> </xs:restriction> </xs:simpleType>
#10
Occurrences Review
As a global type.
Inside an attribute declaration.
Inside an element declaration.
Inside another simple type definition.
Inside an union simple type definition.
Inside a list simple type definition.
#11
Namespaces and Simple Types
Simple types are defined relative to a namespace.
Lexical values for simple types have no namespace associated with them.
Hence, where you define your simple types does not affect the instance.
As such, there are no special considerations to keep in mind as there are with complex types.
#12
Deriving Simple Types
You can derive simple types from your own simple types.
All the constraining facets will be applied from the whole derivation path.
Constraining facets have to be more restrictive than the base type's constraining facets.
Certain facets can be fixed along the way.
#13
Fixing Facets
You can "fix" certain facets by setting the 'fixed' attribute to 'true' on the facet declaration:
<length value="8" fixed='true'/>
The following facets can be fixed:
length, minLength, maxLength
minInclusive, maxInclusive, minExclusive, maxExclusive
totalDigits, fractionDigits
whitespace
Fixing a facet means a derived type cannot specify any other value than the fixed value.
#14
Fixing Facets - Example
A base type for million dollar figures:
<xs:simpleType base="FundingAmounts"> <xs:restriction> <xs:minInclusive value="1000000" fixed="true"/> </xs:restriction> </xs:simpleType>
Restricting a product code to 8 characters:
<xs:simpleType base="ProductCode"> <xs:restriction> <xs:length value="8" fixed="true"/> </xs:restriction> </xs:simpleType>
#15
Derving Lengths and Ranges
Ranges have to be withing the base types' range.
For example:
If minInclusive=8 on the base type, then minInclusive>=8 and minExclusive>=8.
If minLength=3 on the base type, then minLength>=3
The idea is that the derived type is a restriction of the base type and so must also be a valid value for the base type.
#16
Whitespace Derivations
You can do the following derivation order:
preserve
replace
collapse
That is:
preserve can be turned into replace or collapse.
replace can be turned into collapse.
#17
Pattern Derivations
A derived simple type must match any base type's pattern.
The effect is like an "and" of all the patterns in the derivation path.
This is the opposite of multiple patterns in one simple type--which is an or:
<xs:simpleType name="CommonAbsoluteUri"> <xs:restriction base="xs:anyURI"> <xs:pattern value="http:.*"/> <xs:pattern value="https:.*"/> <xs:pattern value="ftp:.*"/> <xs:pattern value="file:.*"/> </xs:restriction> </xs:simpleType>
#18
No Enumeration Derivations
You can't "extend" an enumeration.
That's because all simple type derivations are restrictions--not extensions.
You can use union types to extend enumerations:
<xs:simpleType name="Sizes"> <xs:restriction base="xs:token"> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="ExtendedSizes"> <xs:union memberTypes="my:Sizes"> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="extra small"/> <xs:enumeration value="extra large"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>