ZVON > Tutorials > XML Schema and Relax NG Tutorial |
Intro / Search / ZVON |
Index | >> Example 8 / 8 << | Prev | Next | |
The schema below defines an abstract complexType named "myAbstractType" (attribute "abstract" is set to "true"). The type "AAA" is an extension of this type - we add the requirement that attribute "bbb" must be present. Element declaration cannot use abstract type.
Valid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" bbb="1" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <XXX>sdkhlg nkd</XXX> </root> Invalid document <root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <XXX>sdkhlg nkd</XXX> </root> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="root" type="AAA"/> <xsd:complexType name="myAbstractType" abstract="true"> <xsd:sequence minOccurs="1"> <xsd:element name="XXX" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="AAA"> <xsd:complexContent> <xsd:extension base="myAbstractType"> <xsd:attribute name="bbb" type="xsd:string" use="required"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:schema> |
Although Relax NG does not contain such features, we can achieve the same effect with the Relax NG schema below.
Valid document <root bbb="1" xmlns=""> <XXX>sdkhlg nkd</XXX> </root> Invalid document <root xmlns=""> <XXX>sdkhlg nkd</XXX> </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="AAA"/> </element> </start> <define name="myAbstractType"> <oneOrMore> <element name="XXX"> <text/> </element> </oneOrMore> </define> <define name="AAA"> <attribute name="bbb"> <text/> </attribute> <ref name="myAbstractType"/> </define> </grammar> |