validate with schema

S

sk

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().newDocumentBuilder();
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.W3C_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 xmlns:xsi="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 protected]</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 protected]</email>
<link manager="Big.Boss"/>
</person>

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

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

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

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

</personnel>

personal.xsd===============================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs='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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top