This section describes fundamental and constraining facets on list datatypes: 'length', 'minLength', 'maxLength', 'pattern', 'enumeration' and 'assertions'. New simple datatype can be defined with a 'restriction' component using a list datatype as the base.
From the previous tutorial, we know that
a list datatype is defined as a datatype where the entire lexical representation is mapped to a list of items
separated by whitespaces. And its item datatype is another atomic datatype or atomic union datatype.
Now let's see what facets are supported on list datatypes.
A list construction supports 2 groups of facets:
fundamental facets and constraining facets:
A list datatype has the following settings for its fundamental facets defined:
That means we can define a new simple datatype with a "restriction" construction component using a list datatype as the base.
Some notes on how constraining facets are applied on list values:
"length", "minLength" and "maxLength" are applied on the number of items in the list,
not number of characters on any single item.
"pattern" is applied on the entire list, not on each item.
"enumeration" is provides an acceptable list of values.
For example, the following XSD document uses "restriction" components on list datatypes to define 2 new simple datatypes:
Here is a sample XML document to test the above XSD document:
<?xml version="1.1"?>
<!-- list_facet_test.xml
- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<List_Facet_Test>
<!-- Valid "Winners" list of 3 items -->
<Winners> Herong Maria Bill </Winners>
<!-- Invalid "Winners" values -->
<Winners> Lee Maria Bill </Winners>
<Winners> <![CDATA[B. Lee]]> Maria Bill </Winners>
<!-- Valid "WinningNumbers" list values -->
<WinningNumbers> 1 20 24 33 37 43 </WinningNumbers>
<!-- Invalid "WinningNumbers" values -->
<WinningNumbers> 1 20 24 33 37 43 49 </WinningNumbers>
</List_Facet_Test>
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
list_facet_test.xsd list_facet_test.xml
Error:
Line number: 11
Column number: 47
Message: cvc-pattern-valid: Value 'Lee Maria Bill' is not facet-
valid with respect to pattern '\w{6} \w{5} \w{4}' for type
'#AnonType_WinnersList_Facet_Test'.
(6 characters are needed on the first item)
Error:
Line number: 11
Column number: 47
Message: cvc-type.3.1.3: The value 'Lee Maria Bill' of element
'Winners' is not valid.
Error:
Line number: 12
Column number: 55
Message: cvc-pattern-valid: Value 'B. Lee Maria Bill' is not facet-
valid with respect to pattern '\w{6} \w{5} \w{4}' for type
'#AnonType_WinnersList_Facet_Test'.
(CDATA block is not able to "glue" togeter "B. Lee" as 1 item)
Error:
Line number: 12
Column number: 55
Message: cvc-type.3.1.3: The value 'B. Lee Maria Bill' of element
'Winners' is not valid.
Error:
Line number: 18
Column number: 63
Message: cvc-length-valid: Value '1 20 24 33 37 43 49' with length
= '7' is not facet-valid with respect to length '6' for type
'#AnonType_WinningNumbersList_Facet_Test'.
(only 6 items are allowed)
Error:
Line number: 18
Column number: 63
Message: cvc-type.3.1.3: The value '1 20 24 33 37 43 49' of element
'WinningNumbers' is not valid.
Failed with errors: 6