File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XML valiadation using schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML valiadation using schema" Watch "XML valiadation using schema" New topic

XML valiadation using schema

Mohammed Ajmal

Joined: Sep 20, 2004
Posts: 29

I am using the following schema to validate the xml file using SAX parser (Xerces)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="">
<xs:element name="personnel">
<xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>

<xs:element name="person">
<xs:element ref="name"/>
<xs:element ref="email" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="url" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="link" minOccurs='0' maxOccurs='1'/>

<xs:attribute name="id" use='required'/>
<xs:attribute name="note" type="xs:string"/>
<xs:attribute name="contr" use='default' value="false">
<xs:simpleType base="xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
<xs:attribute name="salary" type="xs:integer"/>

<xs:element name="name">
<xs:element ref="fam"/>
<xs:element ref="given"/>

<xs:element name="fam" type='xs:string'/>

<xs:element name="given" type='xs:string'/>

<xs:element name="email" type='xs:string'/>

<xs:element name="url">
<xs:attribute name="href" type="xs:string" default="http://"/>

<xs:element name="link">
<xs:attribute name="manager" type="xs:IDREF"/>
<xs:attribute name="subordinates" type="xs:IDREFS"/>

<xs:notation name='gif' public='-//APP/Photoshop/4.0' system='photoshop.exe'/>


Below is sample xml file:

<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi=""
<name><fam>Worker</fam> <given>Four</given></name>
<link manager="Big.Boss"/>

<person id="five.worker">
<name><fam>Worker</fam> <given>Five</given></name>
<link manager="Big.Boss"/>


These example I got from one of the sites and I am trying to validate a xml file using the schema.

But I am getting the following errors. Please let me know where is the error. I am new to xml parsing...

E:\Imp_Exp\Testing>java TestParser personal-schema.xml
Inside main method
came inside the startDocument method
ERROR: line 5: s4s-elt-invalid-content.1: The content of '#AnonType_personnel' i
s invalid. Element 'element' is invalid, misplaced, or occurs too often.
ERROR: line 11: s4s-elt-invalid-content.1: The content of '#AnonType_person' is
invalid. Element 'element' is invalid, misplaced, or occurs too often.
came inside the startElement method

Waiting for reply in anticipation.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
The schema file itself is not valid.

Well, a handful of things -

a. you need to add xs:sequence after the complextype.
b. simpletypes don't need base = 'xs:string' (they are always strings.
c. enumeration elements should be in a restriction element.
d. ...not sure what else I changed......check the diffs.

- m

Duct Tape Design Pattern:

Architecture that creates a one-off solution using the path of least resistance.
I agree. Here's the link:
subject: XML valiadation using schema
It's not a secret anymore!