Schema eksempeltest.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://arne/test"
targetNamespace="http://arne/test"
elementFormDefault="qualified">
<xsd:complexType name="recType">
<xsd:attribute name="a" type="xsd:nonNegativeInteger"/>
<xsd:attribute name="b" type="xsd:nonNegativeInteger"/>
</xsd:complexType>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="rec" type="test:recType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
test4.xml
<data xmlns="http://arne/test">
<rec>
<a>1</a>
<b>2</b>
</rec>
<rec>
<a>3</a>
<b>4</b>
</rec>
</data>
test5.xml
test6.xml
ValidateSchema.java
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class ValidateSchema {
private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
public static void testSchema(final String xmlfnm, final String xsdfnm) throws SAXException, IOException, ParserConfigurationException {
System.out.printf("Validating %s against schema %s\n", xmlfnm, xsdfnm);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File(xsdfnm));
saxParser.parse(new File(xmlfnm), new DefaultHandler() {
@Override
public void error(SAXParseException ex) {
System.out.println(ex.getMessage());
}
@Override
public void warning(SAXParseException ex) {
System.out.println(ex.getMessage());
}
});
}
public static void main(String[] args) throws Exception {
testSchema("test4.xml", "test.xsd");
testSchema("test5.xml", "test.xsd");
testSchema("test6.xml", "test.xsd");
}
}
output:
Validating test4.xml against schema test.xsd
cvc-complex-type.2.1: Element 'rec' must have no character or element information item [children], because the type's content type is empty.
cvc-complex-type.2.1: Element 'rec' must have no character or element information item [children], because the type's content type is empty.
Validating test5.xml against schema test.xsd
Validating test6.xml against schema test.xsd
cvc-datatype-valid.1.2.1: '4.5' is not a valid value for 'integer'.
cvc-attribute.3: The value '4.5' of attribute 'b' on element 'rec' is not valid with respect to its type, 'nonNegativeInteger'.
Og det er korrekt: test4.xml matcher ikke schema, test5.xml matcher schema og test6.xml matcher ikke schema selvom form er korrekt men n varedi er invalid (det kan ikke testes med DTD) !