XSD Tutorials - Herong's Tutorial Examples - Version 5.11, 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:
>Let's see an example of how to modify facet settings using the "restriction" component with the following assumptions:
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
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
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
Constructing New Datatypes with Restriction Facets
Facets Supported in "dateTime"
"simpleType" - Defining Your Own Simple Datatypes
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