aspose file tools*
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
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML valiadation using schema" Watch "XML valiadation using schema" New topic
Author

XML valiadation using schema

Mohammed Ajmal
Greenhorn

Joined: Sep 20, 2004
Posts: 29
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.
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: http://aspose.com/file-tools
 
subject: XML valiadation using schema