I'm using javax.xml.validation.Validator for validating xml against schema. I have a requirement where the input xml contains 'minOccurs' and 'maxOccurs' fields. If I validate this against schema, I'm getting org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 7; cvc-complex-type.3.2.2: Attribute 'minOccurs' is not allowed to appear in element. How to resolve this?
Validation:
URL url;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
url = classLoader.getResource(schemaLocation);
String xsd = url.toURI().getPath();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
File f = new File(xsd);
schema = factory.newSchema(f);
Validator valid = schema.newValidator();
StringReader xml = new StringReader(request);
valid.validate(new StreamSource(xml));
xml.close();
XSD:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML:
<order>
<item>
<name minoccurs="1" maxOccurs="unbounded">apple</name>
</item>
</order>