ZVON > Tutorials > XML Schema and Relax NG Tutorial |
Intro / Search / ZVON |
Index | >> Example 6 / 6 << | Prev | Next | |
Please, send all comments, bug-reports, and contributions to Jiri.Jirat@systinet.com. Thank you very much.
XML Schema keys: key, keyrefThe "keyref" lets you specify, that attribute/element refers to some node (which must be defined as "key" or "unique"). Now, the "key" element says: the elements "a" under "root" element must have unique value of "id" attribute and this attribute must be present. The "keyref" element specifies that element "b" refers to element "a" (via the attributes: b/@idref -> a/@id).
Valid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <AAA> <a id="x"/> <a id="y"/> </AAA> <BBB> <b idref="x"/> <b idref="y"/> <b idref="y"/> </BBB> </root> Invalid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <AAA> <a id="x"/> <a id="y"/> </AAA> <BBB> <b idref="x"/> <b idref="y"/> <b idref="z"/> </BBB> </root> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="root"> <xsd:complexType> <xsd:sequence minOccurs="1" maxOccurs="1"> <xsd:element name="AAA" type="myAAA"/> <xsd:element name="BBB" type="myBBB"/> </xsd:sequence> </xsd:complexType> <xsd:key name="myId"> <xsd:selector xpath="./AAA/a"/> <xsd:field xpath="@id"/> </xsd:key> <xsd:keyref name="myIdref" refer="myId"> <xsd:selector xpath="./BBB/b"/> <xsd:field xpath="@idref"/> </xsd:keyref> </xsd:element> <xsd:complexType name="myAAA"> <xsd:sequence minOccurs="1"> <xsd:element name="a" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="id" type="xsd:NCName" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="myBBB"> <xsd:sequence minOccurs="1"> <xsd:element name="b" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="idref" type="xsd:NCName" use="required"/> </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=""> <AAA> <a id="x"/> <a id="y"/> </AAA> <BBB> <b idref="x"/> <b idref="y"/> <b idref="y"/> </BBB> </root> Invalid document <root xmlns=""> <AAA> <a id="x"/> <a id="y"/> </AAA> <BBB> <b idref="x"/> <b idref="y"/> <b idref="z"/> </BBB> </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"> <ref name="myAAA"/> <ref name="myBBB"/> </element> </start> <define name="myAAA"> <element name="AAA"> <oneOrMore> <element name="a"> <attribute name="id"> <data type="NCName"/> </attribute> <empty/> </element> </oneOrMore> </element> </define> <define name="myBBB"> <element name="BBB"> <oneOrMore> <element name="b"> <attribute name="idref"> <data type="NCName"/> </attribute> <empty/> </element> </oneOrMore> </element> </define> </grammar> Correct Schematron schema <schema xmlns="http://www.ascc.net/xml/schematron" > <pattern name="Element 'a' must have unique 'id' attribute"> <rule context="a/@id"> <assert test="count(//@id[. = current()]) = 1"> Id not unique! </assert> </rule> </pattern> <pattern name="'idref' must reference existing 'id'"> <rule context="b/@idref"> <assert test="/root/AAA/a/@id[. = current()]"> Id ' <value-of select="."/>' does not exist! </assert> </rule> </pattern> </schema> |