Connecting Tech Pros Worldwide Help | Site Map

XSD - attribute level data validation

Newbie
 
Join Date: Oct 2008
Location: Chennai, India.
Posts: 3
#1: Jan 27 '09
Hi all,

I have an XML in which i need to validate the attribute values based on another attribute value of the same tag.

For Example

Expand|Select|Wrap|Line Numbers
  1. <Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  2.     <Emp>
  3.         <Column name="EName">Albert</Column>
  4.         <Column name="Age">27</Column>
  5.         <Column name="Dept">105</Column>
  6.     </Emp>
  7. </Employees>
  8.  
Question:
In the above under Employees / Emp / Column, if the attribute "name" has the value "Dept" then the value of the tag should be 105 or 106 or 107

Can anybody throw some light on how to achieve this through XSD.
Any help is highly appreciated

I have the below XSD generated though XMLSpy
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--W3C Schema generated by XMLSpy v2008 rel. 2 (http://www.altova.com)-->
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  4.     <xs:element name="Column">
  5.         <xs:complexType>
  6.             <xs:simpleContent>
  7.                 <xs:extension base="ST_Column">
  8.                     <xs:attribute name="name" use="required">
  9.                         <xs:simpleType>
  10.                             <xs:restriction base="xs:string">
  11.                                 <xs:enumeration value="Age"/>
  12.                                 <xs:enumeration value="Dept"/>
  13.                                 <xs:enumeration value="EName"/>
  14.                             </xs:restriction>
  15.                         </xs:simpleType>
  16.                     </xs:attribute>
  17.                 </xs:extension>
  18.             </xs:simpleContent>
  19.         </xs:complexType>
  20.     </xs:element>
  21.         <xs:simpleType name="ST_Column">
  22.         <xs:restriction base="xs:string">
  23.             <xs:enumeration value="105"/>
  24.             <xs:enumeration value="27"/>
  25.             <xs:enumeration value="Albert"/>
  26.         </xs:restriction>
  27.     </xs:simpleType>
  28.     <xs:element name="Employees">
  29.         <xs:complexType>
  30.             <xs:sequence>
  31.                 <xs:element ref="Emp" maxOccurs="unbounded"/>
  32.             </xs:sequence>
  33.         </xs:complexType>
  34.     </xs:element>
  35.     <xs:element name="Emp">
  36.         <xs:complexType>
  37.             <xs:sequence>
  38.                 <xs:element ref="Column" maxOccurs="unbounded"/>
  39.             </xs:sequence>
  40.         </xs:complexType>
  41.     </xs:element>
  42. </xs:schema>
  43.  
Thanks
Naga
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Jan 27 '09

re: XSD - attribute level data validation


This is called a co-occurence constraint and is not checkable strictly through xml schema (1.0). Are you open to using extensions, such as schematron or RelaxNG? It is available in Xml Schema 1.1, but there are not as many applications which validate to that.
http://www.ibm.com/developerworks/library/x-xml11pt2/
Reply