I’m trying to validate my XML files from given XSD file with the following code,
Source xmlFile = new StreamSource(fXmlFile);
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("presentation.xsd"));
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
And I uploaded my XSD file to here to show you: http://orhancanceylan.com/stack/presentation.xsd
But when I run my code, I get this error:
org.xml.sax.SAXParseException: s4s-att-not-allowed: Attribute 'maxOccurs' cannot appear in element 'element'.
What’s the problem, how should I solve it ?
You have an error in your schema, global presentation element cannot have constraints ( maxOccurs=”1″ minOccurs=”1″), you’d need to remove them (it does not make sense to have min/max for global elements, those constraints makes sense in context of other elements).
More on this in XSD Primer: http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#Globals