aspose file tools*
The moose likes XML and Related Technologies and the fly likes Restriction based on another element value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Restriction based on another element value" Watch "Restriction based on another element value" New topic
Author

Restriction based on another element value

chris silvia
Greenhorn

Joined: Feb 01, 2013
Posts: 1
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

Joined: Jan 18, 2011
Posts: 464
    
    2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restriction based on another element value
 
Similar Threads
XSD element validation
converting xsd to html
XML valiadation using schema
Axis 1.4 throwing org.xml.sax.SAXException: Invalid element
schema generation