I hope, XML Schema community might find few of the things here interesting.
This post can be considered the PART 4 of the XML Schema 1.1 design series that I started couple of weeks ago. The previous parts of this series are available here:
1) PART 1
2) PART 2
3) PART 3
I'm using latest XML Schema 1.1 code-base from Xerces-J SVN repos.
Use-case: (A)
The examples in this post illustrate, how we can constrain the cardinality of XML Schema 1.1 xs:list instance members, and optionally constraining (just to verify myself how XSD 1.1 assertions behave in various combinations) few aspects of list members (like for example that, list items need to be even integers).
Here's an XML instance document (this describes a simple enough list of integers encapsulated in an XML element "X"), which I'll use for illustrations in this post:
[XML 1] (named temp.xml)
<X>2 4 6 5 10 3</X>
Below are few XML Schema 1.1 examples (with Schema 1.1 instructions highlighted with different color), and explanations from my point of view thereafter:
[XML Schema 1]
<?xml version='1.0'?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="X"> <xs:complexType> <xs:simpleContent> <xs:restriction base="INT_LIST"> <xs:assertion test="count($value) le 5" /> </xs:restriction> </xs:simpleContent> </xs:complexType> </xs:element> <xs:complexType name="INT_LIST"> <xs:simpleContent> <xs:restriction base="xs:anyType"> <xs:simpleType> <xs:list itemType="xs:integer" /> </xs:simpleType> <xs:assert test="every $x in $value satisfies ($x mod 2 = 0)" /> </xs:restriction> </xs:simpleContent> </xs:complexType> </xs:schema>
[XML Schema 2]
<?xml version='1.0'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:simpleType>
<xs:restriction base="INT_LIST">
<xs:assertion test="$value mod 2 = 0" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:simpleType name="INT_LIST">
<xs:list itemType="xs:integer" />
</xs:simpleType>
</xs:schema>
[XML Schema 3]
<?xml version='1.0'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="INT_LIST">
<xs:assert test="count($value) le 5" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="INT_LIST">
<xs:list itemType="xs:integer" />
</xs:simpleType>
</xs:schema>
Here are results of XML instance (of document [XML 1]) validation, with the specified schema's:
1. When XML document ([XML 1]) is validated by the schema [XML Schema 1], we get following validation outcomes with Xerces:
[Error] temp.xml:1:20: cvc-assertion.3.13.4.1: Assertion evaluation ('every $x in $value satisfies ($x mod 2 = 0)') for element 'X' with type 'INT_LIST' did not succeed.
[Error] temp.xml:1:20: cvc-assertion.3.13.4.1: Assertion evaluation ('count($value) le 5') for element 'X' with type '#anonymous' did not succeed.
2. When XML document ([XML 1]) is validated by the schema [XML Schema 2], we get following validation outcomes (with Xerces):
[Error] temp.xml:1:20: cvc-assertion.3.13.4.1: Assertion evaluation ('$value mod 2 = 0') for element 'X' with type '#anonymous' did not succeed. Assertion failed for an xs:list member value '5'.
[Error] temp.xml:1:20: cvc-assertion.3.13.4.1: Assertion evaluation ('$value mod 2 = 0') for element 'X' with type '#anonymous' did not succeed. Assertion failed for an xs:list member value '3'.
3. When XML document ([XML 1]) is validated by the schema [XML Schema 3], we get following validation outcomes (with Xerces):
[Error] temp.xml:1:20: cvc-assertion.3.13.4.1: Assertion evaluation ('count($value) le 5') for element 'X' with type '#anonymous' did not succeed.
Here's some quick analysis from my point of view, with regards to what I wanted to achieve with these use-cases (A):
The XML Schema 1.1 assertions XPath 2.0 context variable "$value" has a type annotation xs:anyAtomicType*.
1. The first validation result (1. above) illustrates that every item of xs:list needs to be an even integer, and number of list items are constrained to be maximum "5" (this is a sample "max" limit on number of list items).
2. I intended to use validation results 2. and 3. in combination doing an boolean "AND" of them, essentially to have same XML instance validation objective as case 1. The boolean "AND" of two schema validations can be achieved with for example, Java JAXP validation API. I wrote XML Schema document, [XML Schema 2] to have the XML Schema validator return each individual list item, which do not pass test of mathematical evenness (this was not entirely achieved with schema document [XML Schema 1] -- where the schema detected an evenness failure for whole list instance, but didn't report every individual list item which didn't pass evenness test).
I hope the intent of the use-case described here, and the solutions offered are explained clear enough for XML Schema audience.
Thanks for reading, and as usual I hope that this blog post was interesting!
No comments:
Post a Comment