1

Is it possible in XSD to define an enumeration restriction based also on content of the XML document?

e.g. I want this XML to be valid:

<object>
    <command name="add">
        <param name="layer" type="int" />
        <param name="position" type="point" />
    </command>
    <struct name="point">
        <param name="x" type="float" />
        <param name="y" type="float" />
    </struct>
</object>

according to this schema:

<?xml version="1.0"?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified">

    <xsd:simpleType name="paramType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="int" />
            <xsd:enumeration value="float" />
            <xsd:enumeration value="double" />
            <xsd:enumeration value="bool" />
            <xsd:enumeration value="string" />
            <xsd:enumeration value="table" />
            <!-- XXX: include values from /object/struct/@name here??? -->
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="paramSpec">
        <xsd:attribute name="name" type="xsd:string" use="required" />
        <xsd:attribute name="type" type="paramType" use="required" />
    </xsd:complexType>

    <xsd:complexType name="objectSpec">
        <xsd:sequence>
            <xsd:choice maxOccurs="unbounded">
                <xsd:element name="command">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="param" type="paramSpec" maxOccurs="unbounded" />
                        </xsd:sequence>
                        <xsd:attribute name="name" type="xsd:string" use="required" />
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="struct">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="param" type="paramSpec" maxOccurs="unbounded" />
                        </xsd:sequence>
                        <xsd:attribute name="name" type="xsd:string" use="required" />
                    </xsd:complexType>
                </xsd:element>
            </xsd:choice>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="object" type="objectSpec" />
</xsd:schema>

because the value of the type attribute is either one of the basic types defined in paramType, or the name of a <struct> element defined in the XML itself.

It seems that <xsd:key>/<xsd:keyref> would do validation on the values from XML only. How to combine those with additional values?

fferri
  • 18,285
  • 5
  • 46
  • 95

0 Answers0