Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation against xsd

Status
Not open for further replies.

Naug

Technical User
Sep 24, 2004
85
RU
I need to find a way to validate a document against xsd which is not referenced from xml. I have taken sample files from w3schools:
xml:
Code:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
xsd:
Code:
<?xml version="1.0"?>
<xs:schema xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL]
targetNamespace="[URL unfurl="true"]http://www.w3schools.com"[/URL]
xmlns="[URL unfurl="true"]http://www.w3schools.com"[/URL]
elementFormDefault="qualified">
<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

I try to validate it usinf xerces like so
Code:
 public static void dom4j(){
         SAXReader reader = new SAXReader();
        try {
            reader.setValidation(true);

            // specify the schema to use
            reader.setProperty("[URL unfurl="true"]http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","schema.xsd");[/URL]
//            reader.setProperty("[URL unfurl="true"]http://apache.org/xml/properties/schema/external-schemaLocation","schema.xsd");[/URL]
            org.dom4j.Document validationDoc = reader.read("file.xml");
            org.dom4j.Element root = validationDoc.getRootElement();
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SAXException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


However I get errors in case I set "external-noNamespaceSchemaLocation" property to my xsd I get
Code:
org.xml.sax.SAXParseException: TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace.

If I set external-schemaLocation to xsd I get
Code:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'note'.

Im new to this whole xsd thing and so am confused as to how it works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top