ZVON > Tutorials > XML Schema and Relax NG Tutorial |
Intro / Search / ZVON |
Index | >> Example 2 / 6 << | Prev | Next | |
Please, send all comments, bug-reports, and contributions to Jiri.Jirat@systinet.com. Thank you very much.
XML Schema keys: keyWe will use "key" element to define, that the elements "a" under "root" element must have unique value of "id" attribute and it must be present.
Valid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <a id="x"/> <a id="y"/> <a id="z"/> </root> Invalid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <a/> <a id="y"/> <a id="z"/> </root> Invalid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <a id="x"/> <a id="y"/> <a id="y"/> </root> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="root" type="myList"> <xsd:key name="myId"> <xsd:selector xpath="./a"/> <xsd:field xpath="@id"/> </xsd:key> </xsd:element> <xsd:complexType name="myList"> <xsd:sequence minOccurs="1"> <xsd:element name="a" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="id" type="xsd:NCName"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:schema> |
There is no such mechanism in Relax NG, but we can combine Relax NG and Schematron schemas.
Valid document <root xmlns=""> <a id="x"/> <a id="y"/> <a id="z"/> </root> Invalid document <root xmlns=""> <a/> <a id="y"/> <a id="z"/> </root> Invalid document <root xmlns=""> <a id="x"/> <a id="y"/> <a id="y"/> </root> |
Correct Relax NG schema (correctRelax_0.rng) <grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" ns="" xmlns="http://relaxng.org/ns/structure/1.0" > <start> <element name="root"> <oneOrMore> <element name="a"> <attribute name="id"> <data type="NCName"/> </attribute> <empty/> </element> </oneOrMore> </element> </start> </grammar> Correct Schematron schema |