validate with schema

Discussion in 'Java' started by sk, Jul 4, 2006.

  1. sk

    sk Guest

    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>
    <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 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>
     
    sk, Jul 4, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Antonio_Ruiz_Mart=EDnez?=

    validate a document or element against a schema

    =?ISO-8859-1?Q?Antonio_Ruiz_Mart=EDnez?=, Jan 13, 2005, in forum: Java
    Replies:
    0
    Views:
    318
    =?ISO-8859-1?Q?Antonio_Ruiz_Mart=EDnez?=
    Jan 13, 2005
  2. fhuddles
    Replies:
    1
    Views:
    965
    fhuddles
    Feb 15, 2005
  3. Replies:
    2
    Views:
    576
  4. Markus
    Replies:
    1
    Views:
    1,541
    Markus
    Nov 23, 2005
  5. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,265
    Stanimir Stamenkov
    Apr 25, 2005
Loading...

Share This Page