ZVON > Tutorials > XML Schema and Relax NG Tutorial |
Intro / Search / ZVON |
Index | >> Example 3 / 10 << | Prev | Next | |
Now, we want the element "root" to be from the range 0-100 or 300-400 (including the border values). We will make a union from two intervals.
Valid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >50</root> Valid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >320</root> Invalid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >500</root> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="root"> <xsd:simpleType> <xsd:union> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="100"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="300"/> <xsd:maxInclusive value="400"/> </xsd:restriction> </xsd:simpleType> </xsd:union> </xsd:simpleType> </xsd:element> </xsd:schema> |
As far as I know, such complex derivations are not possible in Relax NG. However, we can overcome this using the "choice" element. And this should be the same - logical OR corresponds to operation "union".
Valid document <root xmlns="">50</root> Valid document <root xmlns="">380</root> Invalid document <root xmlns="">500</root> |
Correct Relax NG schema (correctRelax_0.rng) <grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0" > <start> <element> <name ns="">root</name> <choice> <data type="integer"> <param name="minInclusive">0</param> <param name="maxInclusive">100</param> </data> <data type="integer"> <param name="minInclusive">300</param> <param name="maxInclusive">400</param> </data> </choice> </element> </start> </grammar> |