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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

XML valiadation using schema

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

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="http://www.w3.org/2001/XMLSchema">
<xs:element name="personnel">
<xs:complexType>
<xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>
</xs:complexType>
</xs:element>

<xs:element name="person">
<xs:complexType>
<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:simpleType>
</xs:attribute>
<xs:attribute name="salary" type="xs:integer"/>
</xs:complexType>
</xs:element>

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

<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:complexType>
<xs:attribute name="href" type="xs:string" default="http://"/>
</xs:complexType>
</xs:element>

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

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

</xs:schema>

Below is sample xml file:

<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema"
noNamespaceSchemaLocation='personal.xsd'>
<person>
<name><fam>Worker</fam> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>

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

</personnel>

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic