XML validation using javax.xml.validation
Hello,
I am trying to validate some xml against my xsd.
Here is my xml:
<host>
<status>Unknown</status>
</host>
Here is my xsd:
<?xml version="1.0"?>
<xsd:schema targetNamespace="blah"
xmlns:tns="blah"
xmlns:xsd="blah">
<xsd:simpleType name="Status">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Unknown"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="host">
<xsd:complexType>
<xsd:all>
<xsd:element name="status" type="tns:Status"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:schema>
My test code is:
try
{
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("test.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
StreamSource schemaFile =new StreamSource(new File("schema.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate the document
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new DOMSource(document));
}
catch(Exception e)
{
fail("XML validation failed: " + e.getMessage());
}
I get the following error:
ERROR: 'cvc-elt.1: Cannot find the declaration of element 'host'.'
If i replace "type=tns:Status' in the "status" element with just "type=string", it works fine.
Does anyone have any idea what the problem is?
Thank you,
David

