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

[2691 byte] By [daft_davya] at [2007-10-3 10:28:56]
# 1
I believe xml namespace declariations should be inside the DTD (i.e. the < xml > "top" element)
watertownjordana at 2007-7-15 5:51:29 > top of Java-index,Java Essentials,Java Programming...
# 2
Where is ns "blah" defined? Same directory as the document shown?
watertownjordana at 2007-7-15 5:51:29 > top of Java-index,Java Essentials,Java Programming...
# 3

To daft_davy:

1. Your XSD document is invalid: the xmlns attribute of schema documents must always have the following value: "http://www.w3.org/2001/XMLSchema"

All other values will result in the following validation error:

org.xml.sax.SAXParseException: s4s-elt-schema-ns: The namespace of element 'schema' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.

Your XSD should look like this:

<?xml version="1.0"?>

<xsd:schema targetNamespace="blah" xmlns:tns="blah" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<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>

2. Your XML document does not match the XSD schema (even if you use the one above) for the following reasons:

a) The root element of the XML document must be associated with the namespace defined by the targetNamespace attribute of the schema.

b) The "blah" namespace at the element <status> in the XML document has to be undeclared because there is no namespace declaration to this element in the schema document either. There are to ways to do this:

<host xmlns="blah">

<status xmlns="">Unknown</status>

</host>

or:

<xxx:host xmlns:xxx="blah">

<status>Unknown</status>

</xxx:host>

To watertownjordan:

The namespace URI can be virtually any string, so you don't need to specify a valid URI to define a namespace.

prgguya at 2007-7-15 5:51:29 > top of Java-index,Java Essentials,Java Programming...