Creating Simple Types Using Facets

Using facets lets you restrict the data a simple type can hold. For example, say that you want to create a simple type named dayOfMonth that can hold only values between 1 and 31 , inclusive. In that case, you can define it this way, using the two facets minInclusive and maxInclusive :

 <xsd:simpleType name="dayOfMonth">      <xsd:restriction base="xsd:integer">         <xsd:minInclusive value="1"/>         <xsd:maxInclusive value="31"/>     </xsd:restriction>  </xsd:simpleType> 

Now that you've created this new simple type, you can declare elements and attributes of this type.

In ch05_07.xsd, the catalogID simple type is even more powerful than this dayOfMonth simple type. The catalogID simple type uses the pattern facet to specify a regular expression (that is, a pattern set up to match text in the format you specify) that text strings values for this type must match:

 <xsd:simpleType name="catalogID">      <xsd:restriction base="xsd:string">         <xsd:pattern value="\d{3}-\d{4}-\d{3}"/>     </xsd:restriction> </xsd:simpleType> 

In this case, the text in the simpleType type must match the regular expression "\d{3}-\d{4}-\d{3}" , which matches text strings made up of three digits, a hyphen, four digits, another hyphen, and three digits.

About Regular Expressions

The regular expressions used in XML schema facets are the same as those used in the Perl programming language. You can find the complete documentation for Perl regular expressions at the Comprehensive Perl Archive Network (CPAN) Web site: www.cpan.org/doc/manual/html/pod/perlre.html. (Regular expressions are not a skill you'll need in this book.)

The catalogID type is the type of the <book> element's bookID attribute, so I can specify book ID values like this in ch05_06.xml , matching the regular expression I've used for this attribute:

  <book bookID="123-4567-890">  <bookTitle>Earthquakes for Breakfast</bookTitle>     <pubDate>2003-10-20</pubDate>     <replacementValue>15.95</replacementValue>     <maxDaysOut>14</maxDaysOut> </book> 

What facets are there, and what built-in simple types support them? You'll find the general facets, listed by the simple types that support them, in Table 5-4.

Table 5-4. Simple Types and Applicable Facets
Type length min Length max Length pattern enumeration White-space
anyURI x x x x x x
base64Binary x x x x x x
boolean       x   x
byte       x x x
date       x x x
dateTime       x x x
decimal       x x x
double       x x x
duration       x x x
ENTITIES x x x   x x
ENTITY x x x x x x
float       x x x
gDay       x x x
gMonth       x x x
gMonthDay       x x x
gYear       x x x
gYearMonth       x x x
hexBinary x x x x x x
ID x x x x x x
IDREF x x x x x x
IDREFS x x x   x x
int       x x x
integer       x x x
language x x x x x x
long       x x x
Name x x x x x x
NCName x x x x x x
negative Integer       x x x
NMTOKEN x x x x x x
NMTOKENS x x x   x x
nonNegative Integer       x x x
nonPositive Integer       x x x
normalized String x x x x x x
NOTATION x x x x x x
positive Integer       x x x
QName x x x x x x
short       x x x
string x x x x x x
time       x x x
token x x x x x x
unsignedBxte       x x x
unsignedInt       x x x
unsignedLong       x x x
unsignedShort       x x x

The numeric simple types and those simple types that can be ordered also have some additional facets, which you see in Table 5-5.

Table 5-5. Ordered Simple Types and Applicable Facets
Type max Inclusive max Exclusive min Inclusive min Exclusive Total Digits Fraction Digits
byte x x x x x x
unsignedByte x x x x x x
integer x x x x x x
positive Integer x x x x x x
negative Integer x x x x x x
nonNegative Integer x x x x x x
nonPositive Integer x x x x x x
int x x x x x x
unsignedInt x x x x x x
long x x x x x x
unsignedLong x x x x x x
short x x x x x x
unsignedShort x x x x x x
decimal x x x x x x
float x x x x    
double x x x x    
time x x x x    
dateTime x x x x    
duration x x x x    
date x x x x    
gMonth x x x x    
gYear x x x x    
gYearMonth x x x x    
gDay x x x x    
gMonthDay x x x x    

Of all the facets you see in Tables 5-4 and 5-5, my favorites are minInclusive , maxInclusive , pattern , and enumeration . We've seen the first three, but not the enumeration facet yet.

The enumeration facet lets you set up an enumeration of values, exactly as you can do in DTDs (as we saw in the previous chapter). Using an enumeration, you can restrict the possible values of a simple type to a list of values that you specify.

For example, to set up a simple type named weekday whose values can be "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , and "Saturday" , you'd define that type like this:

 <xsd:simpleType name="weekday">      <xsd:restriction base="xsd:string">         <xsd:enumeration value="Sunday"/>         <xsd:enumeration value="Monday"/>         <xsd:enumeration value="Tuesday"/>         <xsd:enumeration value="Wednesday"/>         <xsd:enumeration value="Thursday"/>         <xsd:enumeration value="Friday"/>         <xsd:enumeration value="Saturday"/>     </xsd:restriction> </xsd:simpleType> 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net