0

I would like to validate XSD (XML schema) against the W3C XSD specification in Java. I appreciate any example.

Thank you for your answer in advance.

Bests, Rómeó Nagy

Rómeó Nagy
  • 95
  • 1
  • 8

2 Answers2

1

When you create a new schema instance with SchemaFactory it throws an SAXException if the input schema document was invalid. Suppose your xsd is present in file:

    File schemaFile = new File("pathToXsd");
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
      Schema schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
      System.out.println("Schema was invalid");
    }
    System.out.println("Schema was valid.");
Balázs Nemes
  • 699
  • 4
  • 9
0

Your XSD file that you want to validate is just an XML document that has an associated XSD file that defines what constitutes a valid XML document.

For details on various ways to parse an XML document with validation, see this question.

The XML file is your XSD file. The schema file you use to validate your XML file is http://www.w3.org/2001/XMLSchema.

Community
  • 1
  • 1
Rob
  • 6,247
  • 2
  • 25
  • 33