Tausif mohammad
Tausif mohammad

Reputation: 111

Dependency of one element on another using XSD

I need to check that if imagetype = I then docext should only be TIFF/tiff and if imagetype = N then docext should be PDF/pdf. Else the validation should fail. I am writing a XSD for this and do not know how to implement this constraint. Currently I am using XSD 1.0.

<xs:element name="docext" minOccurs="1" maxOccurs="1">
  <xs:simpleType>                       
    <xs:restriction base="xs:string">
      <xs:pattern value="((T|t)(I|i)(F|f)(F|f))|((P|p)(D|d)(F|f))" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>
<xs:element name="imagetype" minOccurs="1" maxOccurs="1">
  <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:enumeration value="N" />
        <xs:enumeration value="I" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Upvotes: 2

Views: 429

Answers (1)

kjhughes
kjhughes

Reputation: 111581

XSD 1.0 cannot meet your requirements as stated.

Your options include either of the following:

  1. Redesign your XML. If possible, this is the better option. Type information is typically better conveyed via element names than element values; such a design can often obviate the need for dependency constraints beyond what can be expressed via basic content model requirements.
  2. Use XSD 1.1 assertions. You can use XPath to specify constraints at the point of a common ancestor to docext and imagetype. You'll need to consider more of your XSD than shown if you have to go this route.

Upvotes: 3

Related Questions