You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Ramin Raybod <r_...@isc.iranet.net> on 2002/04/29 05:52:25 UTC

An example of schema

Hi,
I'm so new to xml schema and am reading Oreilly "Learning XML" e-book from "Safari Tech Books Online". There is an example of a schema at the end of chapter 5 of the book. I tried to validate it by xerces through a sample validation program called sax.SAXCount. It has too many errors like it is not a valid schema at all. I guess it is because of version used in this example - 1999. I compared this with another schema a sample coming with xerces set - 2001 version . Wow too much difference. How? Is there really no support for such schema mentioned in this book? or I have problems in using something?
Anyway I have written the sample schema and the related xml doc here. Oh yes I had to add some additional code to XML file for linking to the schema file... I have deleted an attribute also... These are the differences I've made to the original example of the book :)
Thanks
Ramin

The xsd file :

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      Census form for the Republic of Oz
      Department of Paperwork, Emerald City
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="census" type="CensusType"/>

  <xsd:complexType name="CensusType">
    <xsd:element name="censustaker" type="xsd:decimal" minoccurs="0"/>
    <xsd:element name="address" type="Address"/>
    <xsd:element name="occupants" type="Occupants"/>
  </xsd:complexType>

  <xsd:complexType name="Address">
    <xsd:element name="number" type="xsd:decimal"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="city"   type="xsd:string"/>
    <xsd:element name="province"  type="xsd:string"/>
    <xsd:attribute name="postalcode" type="PCode"/>
  </xsd:complexType>

  <xsd:simpleType name="PCode" base="xsd:string">
    <xsd:pattern value="[A-Z]-d{3}"/>
  </xsd:simpleType>

  <xsd:complexType name="Occupants">
    <xsd:element name="occupant" minOccurs="1" maxOccurs="50">
     <xsd:complexType>
      <xsd:element name="firstname" type="xsd:string"/>
      <xsd:element name="surname" type="xsd:string"/>
      <xsd:element name="age">
       <xsd:simpleType base="xsd:positive-integer">
        <xsd:maxExclusive value="200"/>
       </xsd:simpleType>
      </xsd:element>
     </xsd:complexType>
    </xsd:element>
   </xsd:complexType>

</xsd:schema>

The xml file :

<census xmlns:xsd="http://www.w3.org/1999/XMLSchema-instance"
        xsd:noNamespaceSchemaLocation="census.xsd">
  <censustaker>738</censustaker>
  <address>
    <number>510</number>
    <street>Yellowbrick Road</street>
    <city>Munchkinville</city>
    <province>Negbo</province>
  </address>
  <occupants>
    <occupant status="adult">
      <firstname>Floyd</firstname>
      <surname>Fleegle</surname>
      <age>61</age>
    </occupant>
    <occupant>
      <firstname>Phylis</firstname>
      <surname>Fleegle</surname>
      <age>52</age>
    </occupant>
    <occupant>
      <firstname>Filbert</firstname>
      <surname>Fleegle</surname>
      <age>22</age>
  </occupants>
</census>


RE: An example of schema

Posted by Pavitra Jain <pa...@patni.com>.
Hi,

I think the problem is that XERCES supports
http://www.w3.org/2001/XMLSchema-instance and u are using 1999 version

I generated a new schema through XMLSPy tool. Just add the following line to
ur xml at census tag
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="census1.xsd"

Now your validation code should work. Baring that a occupant tag is not
closed so, if u are catching Fatalerrors in your ErrorHandler it will
generate the exception you. The new schema is pasted below:

<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XML Spy v4.3 U (http://www.xmlspy.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="addressType">
<xs:sequence>
<xs:element ref="number"/>
<xs:element ref="street"/>
<xs:element ref="city"/>
<xs:element ref="province"/>
</xs:sequence>
</xs:complexType>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:byte">
<xs:enumeration value="22"/>
<xs:enumeration value="52"/>
<xs:enumeration value="61"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="census">
<xs:complexType>
<xs:sequence>
<xs:element ref="censustaker"/>
<xs:element name="address" type="addressType"/>
<xs:element name="occupants" type="occupantsType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="censustaker" type="xs:short"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="firstname">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Filbert"/>
<xs:enumeration value="Floyd"/>
<xs:enumeration value="Phylis"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="number" type="xs:short"/>
<xs:complexType name="occupantType">
<xs:sequence>
<xs:element ref="firstname"/>
<xs:element ref="surname"/>
<xs:element ref="age"/>
</xs:sequence>
<xs:attribute name="status" type="xs:string"/>
</xs:complexType>
<xs:complexType name="occupantsType">
<xs:sequence>
<xs:element name="occupant" type="occupantType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="province" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="surname" type="xs:string"/>
</xs:schema>

Regards,
Pavitra Jain

-----Original Message-----
From: Ramin Raybod [mailto:r_raybod@isc.iranet.net]
Sent: Monday, April 29, 2002 9:22 AM
To: xerces
Subject: An example of schema


Hi,
I'm so new to xml schema and am reading Oreilly "Learning XML" e-book from
"Safari Tech Books Online". There is an example of a schema at the end of
chapter 5 of the book. I tried to validate it by xerces through a sample
validation program called sax.SAXCount. It has too many errors like it is
not a valid schema at all. I guess it is because of version used in this
example - 1999. I compared this with another schema a sample coming with
xerces set - 2001 version . Wow too much difference. How? Is there really no
support for such schema mentioned in this book? or I have problems in using
something?
Anyway I have written the sample schema and the related xml doc here. Oh yes
I had to add some additional code to XML file for linking to the schema
file... I have deleted an attribute also... These are the differences I've
made to the original example of the book :)
Thanks
Ramin


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org