| ZVON > Tutorials > XML Schema and Relax NG Tutorial |
| Intro / Search / ZVON |
| Index | >> Example 2 / 8 << | Prev | Next | |
The element "A" must contain a string which is exactly three characters long. We will define our custom type for the string named "myString" and will require the element "A" to be of that type.
|
Valid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >abc</A> Invalid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >ab</A> Invalid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >abcd</A> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="A" type="myString"/> <xsd:simpleType name="myString"> <xsd:restriction base="xsd:string"> <xsd:length value="3"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
The element "A" must contain a string which is exactly three characters long. We will define our custom pattern - a string exactly three characters long - and the element "A" must contain this pattern.
|
Valid document <A xmlns="">abc</A> Invalid document <A xmlns="">ab</A> Invalid document <A xmlns="">abcd</A> |
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="">A</name> <ref name="myString"/> </element> </start> <define name="myString"> <data type="string"> <param name="length">3</param> </data> </define> </grammar> |