XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

What Is Facet?

This section describes facets as simple datatype restrictions. A facet refers a property on a datatype. Some facets of a facet are allowed to be modified to be more restrictive to construct new user-defined datatypes.

What Is Facet? In general, the word "facet" means any of the definable aspects that make up an object. In XSD specification, a "facet" on a datatype refers to a property of the datatype. For example, facet "bounded=true" on a datatype refers to the property that the entire value space of the datatype is bounded by 2 values.

Some facets on a datatype are allowed to be modified to become more "restrictive". The result of such modifications is a new user-defined datatype. When modifying datatype's facets, we need to remember some rules:

>
  • Every simple datatype has its own list of pre-defined facets.
  • Some pre-defined facets are allowed to be modified. Some are not allowed to be modified.
  • Facet modifications should be applied in "restriction" components.
  • New settings provided in facet modifications should be equal to or more restrictive than original settings.

Let's see an example of how to modify facet settings using the "restriction" component with the following assumptions:

  • Simple datatype "unsignedByte" has a facet called "maxInclusive".
  • The original setting of "minInclusive" facet on "unsignedByte" datatype is 0.
  • The original setting of "maxInclusive" facet on "unsignedByte" datatype is 255.
  • To construct a new datatype "month" that supports only values from 1 to 12, we can modify "minInclusive" facet to be more restrictive as 1, and "maxInclusive" facet to be more restrictive as 12,

Here is how the XSD definition should look like:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- general_facet_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:simpleType name="month">
  <xs:restriction base="xs:unsignedByte">
    <xs:maxInclusive value="12"/>
    <xs:minInclusive value="1"/>
  </xs:restriction>
</xs:simpleType>
</xs:schema>

When validating this XSD document with my XsdSchemaChecker.java program presented earlier in the book, I get no errors:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaChecker 
   general_facet_test.xsd

Schema File: general_facet_test.xsd
Parser Class: com.sun.org.apache.xerces.internal.jaxp.validation
   .SimpleXMLSchema

Passed.

The following XSD example is invalid, because the original value of "maxInclusive" is 255. The new value, 366, is less restrictive than 255.

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- general_facet_test_invalid_1.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:simpleType name="day">
  <xs:restriction base="xs:unsignedByte">
    <xs:maxInclusive value="366"/>
    <xs:minInclusive value="1"/>
  </xs:restriction>
</xs:simpleType>
</xs:schema>

My XsdSchemaChecker.java program will give this error on the above XSD document:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaChecker 
   general_facet_test_invalid_1.xsd

   Error:
   Line number: 7
   Column number: 42
   Message: cvc-maxInclusive-valid: Value '366' is not facet-valid 
   with respect to maxInclusive '255' for type 'unsignedByte'.

Failed with 1 errors.

The following XSD example is also invalid, because facet "length" is supported by "unsignedByte":

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- general_facet_test_invalid_2.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:simpleType name="month">
  <xs:restriction base="xs:unsignedByte">
    <xs:maxInclusive value="12"/>
    <xs:minInclusive value="1"/>
    <xs:length value="2"/>
  </xs:restriction>
</xs:simpleType>
</xs:schema>

My XsdSchemaChecker.java program will give this error on the above XSD document:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaChecker 
   general_facet_test_invalid_2.xsd

Error:
   Line number: 7
   Column number: 42
   Message: cos-applicable-facets: Facet 'length' is not allowed by 
   type month.

Failed with 1 errors.

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

Facets, Constraining Facets and Restriction Datatypes

What Is Facet?

 List of Facets

 Constructing New Datatypes with Restriction Facets

 Facets Supported in "string"

 Facets Supported in "dateTime"

 Facets Supported in "decimal"

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

What Is Facet? - Updated in 2014, by Dr. Herong Yang