This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

XML valiadation using schema

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic