Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restriction based on another element value

 
chris silvia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to XML schema and I am trying to figure out how to set up restrictions based on another value.

For example:

<PersonalInfo>
<State>CT</State>
<Car>
<Manufacturer>MINI</Manufacturer>
<AntiLockBrakes>Y</AntiLockBrakes>
</Car>
</PersonalInfo>

<PersonalInfo>
<State>NC</State>
<Car>
<Manufacturer>MINI</Manufacturer>
<AntiLockBrakes>Y</AntiLockBrakes>
</Car>
</PersonalInfo>

<PersonalInfo>
<State>RI</State>
<Car>
<Manufacturer>MINI</Manufacturer>
<AntiLockBrakes>ABS</AntiLockBrakes>
</Car>
</PersonalInfo>


<State>RI</State> valid value is ABS only
<State>NC</State> and <State>CT</State> valid value is Y only

The following allows ABS and Y regardless of the state

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="State">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="RI"/>
<xs:enumeration value="CT"/>
<xs:enumeration value="NC"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PersonalInfo">
<xs:complexType>
<xs:sequence>
<xs:element ref="State"/>
<xs:element ref="Car"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Manufacturer">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="MINI"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Car">
<xs:complexType>
<xs:sequence>
<xs:element ref="Manufacturer"/>
<xs:element ref="AntiLockBrakes"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="AntiLockBrakes">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ABS"/>
<xs:enumeration value="Y"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you've in mind w3c xml schema. In the broadly supported v1.0, this kind of constraints cannot be expressed by and from within the schema document. Possible solutions would be either to relegate the checking (assertion) to a small device at application level or use schematron (basically an xslt of specific construction) in tandam with w3c xml schema or use an entirely different schema like relaxng. In v1.1 which has now reached as a w3c recommendation, additional elements are added, following to a big degree the approach used by schematron, to resolve these co-constraint issues. However the support of v1.1 is much less wide-spread and it takes time to get things done properly naturally. So, in that regard, it is your call.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic