XSD Tutorials - Herong's Tutorial Examples - Version 5.11, by Dr. Herong Yang
Facets Supported in "string"
This section describes fundamental facets and constraining facets supported in 'string' built-in datatype.
Since "string" is the most commonly used simple built-in datatype, let's look at it first to see how many facets are supported in "string".
The "string" datatype has the following settings for its fundamental facets defined:
ordered = false bounded = false cardinality = countably infinite numeric = false
The "string" datatype supports the following constraining facets:
whiteSpace = preserve length = undefined minLength = undefined maxLength = undefined pattern = undefined enumeration = undefined assertions = undefined
Here is a sample schema, string_datatype_facet.xsd, that uses "string" datatype with different restriction facets:
<?xml version="1.1"?> <!-- string_datatype_facet.xsd - Copyright (c) 2013, HerongYang.com, All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="String_Datatype_Facet"> <xs:complexType> <xs:sequence> <xs:element name="canadaAddress" maxOccurs="unbounded"> <xs:complexType> <!-- Using "minLength" to restrict the string --> <xs:attribute name="street"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> </xs:restriction> </xs:simpleType> </xs:attribute> <!-- Using "whiteSpace" to restrict the string --> <xs:attribute name="city"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType> </xs:attribute> <!-- Using "length" to restrict the string --> <xs:attribute name="provinceCode"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="2"/> </xs:restriction> </xs:simpleType> </xs:attribute> <!-- Using "pattern" to restrict the string --> <xs:attribute name="zip"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document, string_datatype_facet.xml, to test the above XSD document:
<?xml version="1.1"?> <!-- string_datatype_facet.xml - Copyright (c) 2013, HerongYang.com, All Rights Reserved. --> <String_Datatype_Facet> <!-- Valid attribute values --> <canadaAddress street="770 Don Mills" city="Toronto" provinceCode="ON" zip="M3C 1T3" /> <!-- Inalid attribute values --> <canadaAddress street="7 M." city=" Toronto " provinceCode="Ontario" zip="M3C LT3" /> </String_Datatype_Facet>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:
c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator string_datatype_facet.xsd string_datatype_facet.xml Error: Line number: 21 Column number: 6 Message: cvc-minLength-valid: Value '7 M.' with length = '4' is not facet-valid with respect to minLength '5' for type '#AnonType_streetcanadaAddressString_Datatype_Facet'. Error: Line number: 21 Column number: 6 Message: cvc-attribute.3: The value '7 M.' of attribute 'street' on element 'canadaAddress' is not valid with respect to its type, 'null'. Error: Line number: 21 Column number: 6 Message: cvc-length-valid: Value 'Ontario' with length = '7' is not facet-valid with respect to length '2' for type '#AnonType_provinceCodecanadaAddressString_Datatype_Facet'. Error: Line number: 21 Column number: 6 Message: cvc-attribute.3: The value 'Ontario' of attribute 'provinceCode' on element 'canadaAddress' is not valid with respect to its type, 'null'. Error: Line number: 21 Column number: 6 Message: cvc-pattern-valid: Value 'M3C LT3' is not facet-valid with respect to pattern '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]' for type '#AnonType_zipcanadaAddressString_Datatype_Facet'. Error: Line number: 21 Column number: 6 Message: cvc-attribute.3: The value 'M3C LT3' of attribute 'zip' on element 'canadaAddress' is not valid with respect to its type, 'null'. Failed with errors: 6
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