I have a schema, shown below, for the XML file that is parsed by a form-generating PHP script. It takes the XML file, which has items like this:
And translates them into HTML fields. The real file is more complicated, but this should do to get my point across.
I would like to indicate in the schema that certain attributes are only permissible if other attributes have a certain value. Thus, if the value of "type" is "text", only the "size" attribute is allowed. If the type is "textarea", only the "rows" and "cols" values are allowed. Finally, if the type is "select", allow only the "size" attribute and additionally require at least one "option" element (and don't allow "options" for any other type).
Is it possible to include such complex requirements in an XML schema, or am I better off leaving it as is where things are generally optional and it's up the the user to conform?
Thanks!
Code:
<field type="text" size="30" />
<field type="textarea" rows="5" cols="40" />
<field type="select" size="20">
<option>red</option>
<option>blue</option>
</field>
And translates them into HTML fields. The real file is more complicated, but this should do to get my point across.
I would like to indicate in the schema that certain attributes are only permissible if other attributes have a certain value. Thus, if the value of "type" is "text", only the "size" attribute is allowed. If the type is "textarea", only the "rows" and "cols" values are allowed. Finally, if the type is "select", allow only the "size" attribute and additionally require at least one "option" element (and don't allow "options" for any other type).
Is it possible to include such complex requirements in an XML schema, or am I better off leaving it as is where things are generally optional and it's up the the user to conform?
Code:
<xs:element name="field">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="option" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="text" />
<xs:enumeration value="textarea" />
<xs:enumeration value="select" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="size" type="xs:integer" use="optional" />
<xs:attribute name="rows" type="xs:integer" use="optional" />
<xs:attribute name="cols" type="xs:integer" use="optional" />
</xs:complexType>
</xs:element>
Thanks!