Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > validate with schema

Reply
Thread Tools

validate with schema

 
 
sk
Guest
Posts: n/a
 
      07-04-2006
I use the sample program to validate customized xml document which they are
also from
sun's web site. However, when validating I always gets the followign error

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element 'personnel'.

If anyone had the same experience and have solved the problem, please give
me some idea.
what I am missing.

valiadte with java============

/*
* SchemaTest.java
*
* Created on June 30, 2006, 7:00 PM
*/

package examples;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.validation.*;
/**
*
* @author Administrator
*/
public class SchemaTest {

/** Creates a new instance of SchemaTest */
public SchemaTest() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {

// Parse an XML document into a DOM tree.
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBu ilder();
Document document = parser.parse(new
File("c:\\personal-schema.xml"));

// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory =
SchemaFactory.newInstance(javax.xml.XMLConstants.W 3C_XML_SCHEMA_NS_URI);

// Load a WXS schema, represented by a Schema instance.
javax.xml.transform.Source schemaFile = new
javax.xml.transform.stream.StreamSource(new File("c:\\personal.xsd"));
Schema schema = factory.newSchema(schemaFile);

// Create a Validator object, which can be used to validate
// an instance document.
Validator validator = schema.newValidator();

// Validate the DOM tree.
validator.validate(new
javax.xml.transform.dom.DOMSource(document));

} catch (ParserConfigurationException e) {
// exception handling
} catch (org.xml.sax.SAXException e) {
// exception handling - document not valid!
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// exception handling
}

}

}

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element 'personnel'.

personal-schema.xml=============================
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='personal.xsd'>

<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email></email>
<link subordinates="one.worker two.worker three.worker four.worker
five.worker"/>
</person>

<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email></email>
<link manager="Big.Boss"/>
</person>

<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email></email>
<link manager="Big.Boss"/>
</person>

<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email></email>
<link manager="Big.Boss"/>
</person>

<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email></email>
<link manager="Big.Boss"/>
</person>

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

</personnel>

personal.xsd===============================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlnss='http://www.w3.org/2001/XMLSchema'>
<xs:element name="personnel">
<xs:complexType>
<xs:sequence>
<xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>
</xs:sequence>
</xs:complexType>

<xs:unique name="unique1">
<xs:selector xpath="person"/>
<xs:field xpath="name/given"/>
<xs:field xpath="name/family"/>
</xs:unique>
<xs:key name='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="keyref1" refer='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="link/@manager"/>
</xs:keyref>
</xs:element>

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<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:sequence>
<xs:attribute name="id" type="xs:ID" use='required'/>
<xs:attribute name="note" type="xs:string"/>
<xs:attribute name="contr" default="false">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</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="family"/>
<xs:element ref="given"/>
</xs:all>
</xs:complexType>
</xs:element>

<xs:element name="family" 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't validate W3C XML meta-schema Ian Pilcher Java 2 08-23-2005 01:23 PM
Validate Document object against Schema without reparsing the xml veerleverbr@hotmail.com Java 2 06-28-2005 07:42 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Unable to validate XML Schema with SAX Parser: fhuddles Java 1 02-15-2005 02:58 PM
validate a document or element against a schema =?ISO-8859-1?Q?Antonio_Ruiz_Mart=EDnez?= Java 0 01-13-2005 11:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57