To prevent spam users, you can only post on this forum after registration, which is by invitation. If you want to post on the forum, please send me a mail (h DOT m DOT w DOT verbeek AT tue DOT nl) and I'll send you an invitation in return for an account.

validating using JAXP

WiebeWiebe Posts: 13
edited March 2011 in - Development
Hi,

This is not have a ProM 6 question, but rather have a problems with JAXP. I hope I may use this forum.

I am trying to validate an .xml against a .xsd file. I use java SDK 1.6 which uses JAXP 1.4 which uses the Xerces parser.

My .xsd looks very common, like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
[...]

My .xml instance starts like this:

<?xml version="1.0" encoding="utf-8"?>
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="my_xsd_file.xsd">
[...]

I use a validating technique explained at http://jaxp.java.net/docs/api/javax/xml/validation/package-summary.html#example-1

My validating code is this:

DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// TODO remove static paths later
Document document = parser.parse(new File("D:/workfolder", filename));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File("D:/workfolder", "my_xsd_file.xsd"));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
try {
    validator.validate(new DOMSource(document));
} catch (SAXException e) {
    context.log(e.getMessage(), MessageLevel.ERROR);
}

I get the following error, which I think should not be there and have googled but have not found a descent solution for. I think the validator needs to be configured to ignore the attributes in the root xml tag.

cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'input'.

As a workaround I just could change my .xml file:

<?xml version="1.0" encoding="utf-8"?>
<input>
[...]

But then it is no longer possible to validate the .xml document in my XML editor because there is no associated schema.

Is there a better 'clean' solutions for validating in code and explicitly specifying the schema location?

Tagged:
Sign In or Register to comment.