You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Bishr Tabbaa <b....@pentasafe.com> on 2001/01/19 20:33:18 UTC

validation by schema

environment:
win2k
sun jdk1.3
xerces 1.2.3

problem: having flipped validation features on, i can't get garbage out for
garbage in.  well-formed, INVALID document instances such as foo2.xml are
not being reported as such.  is it scroll blindness?  folks, sorry for this
being so long.

regards,
bishr

===== test.xsd =========

<?xml version="1.0" ?>

<schema xmlns="http://www.w3.org/1999/XMLSchema">

<annotation>
	<documentation>
		XML Schema for the FooBar Markup Language
	</documentation>
</annotation>

<element name="Foo" type="FooType"/>

<complexType name="FooType" >
	<sequence>
		<element name="bar1" type="string" minOccurs="1"
maxOccurs="1"/>
		<element name="bar2" type="integer" minOccurs="1"
maxOccurs="1"/>
	</sequence>
</complexType>


</schema>

====== foo1.xml ====

<?xml version="1.0" encoding="UTF-8"?>

<Foo	xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation='..\lib\test.xsd'>

	<bar1> Hello </bar1>
	<bar2> 12345 </bar2>
</Foo>

===== foo2.xml ====

<?xml version="1.0" encoding="UTF-8"?>

<Foo	xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation='..\lib\test.xsd'>

</Foo>

===== ValidationRules.java ======

import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class ValidateRules {
	public static void main(String[] args) {
		DOMParser parser = new DOMParser();
		try {
			parser.setErrorHandler( new DefaultHandler() );
	
parser.setFeature("http://xml.org/sax/features/validation", true);
	
parser.setFeature("http://xml.org/sax/features/namespaces", true);
	
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
	
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error
", true);
	
parser.setFeature("http://apache.org/xml/features/validation/dynamic",
true);
	
//parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion
", true);
			parser.parse(new InputSource(new
FileInputStream(args[0])));
		}
		catch (SAXException e) {
			if (e.getException() != null)
	
e.getException().printStackTrace(System.err);
			else e.printStackTrace(System.err);
		}
		catch (Exception e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		Document document = parser.getDocument();
		
	}
}