0

I am using Analytical Information Markup Language (AnIML) is the emerging ASTM XML standard for analytical chemistry data in my project. I have an xml data which I want to validate against an XSD file.But whenever I run the program it is showing an exception. I am using jav acode. My code is-

public class XMLValidator {
public static final String XML_FILE = "test_data.xml";
public static final String SCHEMA_FILE = "test.xsd";

public static void main(String[] args) {
    XMLValidator XMLValidator = new XMLValidator();
    boolean valid = XMLValidator.validate(XML_FILE, SCHEMA_FILE);
    System.out.printf("%s validation = %b.", XML_FILE, valid);
}

private boolean validate(String xmlFile, String schemaFile) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        Schema schema = schemaFactory.newSchema(new File(schemaFile));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new File(xmlFile)));
        return true;
    } catch (SAXException | IOException e) {
        e.printStackTrace();
        return false;
    }
}

My XML file is

Modified XML

    <?xml version="1.0" encoding="UTF-8"?>
<AnIML xmlns:xsi="urn:org:astm:animl:schema:core:draft:0.90" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
 xsi:noNamespaceSchemaLocation="https://github.com/AnIML/schemas/blob/master/animl-core.xsd" version="0.90" blu-fmt-v="1">
  <ExperimentStepSet>
    ..
    </ExperimentStep>
  </ExperimentStepSet>
</AnIML>

The exception I got is

    org.xml.sax.SAXParseException; systemId: file:/D:/test/xmlvalidator/test_data.xml; lineNumber: 2; columnNumber: 229; cvc-elt.1: Declaration of element 'AnIML' can not be found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
at .....
TechoTek
  • 43
  • 12

1 Answers1

1

The XSD file you're using says that the elements must be in namespace urn:org:astm:animl:schema:core:draft:0.90

That can be seen with this attribute in its root:

targetNamespace="urn:org:astm:animl:schema:core:draft:0.90"

Your XML file doesn't use this namespace, nor any other for that matter.

Your XML file needs to comply to the XSD rules to be valid according to this XSD.

A very cheap change to your XML so that it uses the correct namespace required by the XSD, is to just declare this namespace as the default namespace in the root:

<AnIML
  xmlns="urn:org:astm:animl:schema:core:draft:0.90"
  xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
  version="0.90" blu-fmt-v="1">

And all descendant elements will inherit it.

If you still want to keep your previously failing XML Schema validation declarations, it needs to be adapted that way:

<AnIML
  xmlns="urn:org:astm:animl:schema:core:draft:0.90"
  xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="urn:org:astm:animl:schema:core:draft:0.90 https://github.com/AnIML/schemas/blob/master/animl-core.xsd"
  version="0.90" blu-fmt-v="1">
kumesana
  • 2,495
  • 1
  • 9
  • 10
  • which namespace are you talking about. where should be modified to do that. could you please explain more – TechoTek Jul 05 '18 at 11:35
  • @TechoTek I edited my answer – kumesana Jul 05 '18 at 11:42
  • if I copy your code it shows error. I have modified my xml file. please see on the modified code. but still having the same exception – TechoTek Jul 05 '18 at 11:50
  • @TechoTek that modification is not the one I told you to do. It won't work. You can't put whatever you want in xmlns:xsi, nor will it change the namespaces of your elements. It needs to be in xmlns, followed by equals sign, followed by the namespace surrounded by quotes. Well, there is another way but it would be more complicated and equivalent. – kumesana Jul 05 '18 at 11:53
  • if I write you second code, i saw redd mark in the entire XML saying that element does not have required atrribute – TechoTek Jul 05 '18 at 12:08
  • @TechoTek because your element does not have the required attribute. The point of XSD is to let you know these things. Now that you're using it correctly, it's telling you. – kumesana Jul 05 '18 at 12:26
  • so what should I to validate this xml with taht XSD. what i sthe other solution you are talking about – TechoTek Jul 05 '18 at 12:31
  • 1
    @TechoTek you should have the attributes that the XSD says you must have. And comply to all its other rules too. "what i sthe other solution" doesn't matter now, that's another solution to the namespace problem, which is solved now. You have advanced on that front. Keep advancing and address the new problem. – kumesana Jul 05 '18 at 12:35