Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schema: require attributes based on the value of other attributes

Status
Not open for further replies.

courtarro

Programmer
Oct 26, 2005
16
US
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:

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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top