Search code examples
xmlvalidationxsdsaxon

How to validate XML against XSD using Saxon Home Edition (HE) 9.4


How can I use Saxon Home Edition (HE) 9.4 to validate an XML against an XSD?

Do I need the Enterprise version to do this basic thing, which is what the error message implies? Here is the code I am using.

private void btnSaxonTest3_Click(object sender, EventArgs e)
 // validate xml against xsd and show multiple errors
{
  try
  {
    SetGlobalVariables();
    Saxon.Api.Processor proc = new Processor(true);
    //this is the property to set for multiple errors
    //proc.SetProperty("net.sf.saxon.lib.FeatureKeys.VALIDATION_WARNINGS", "true");  //Enterprise function only?

    SchemaManager schemaManager = proc.SchemaManager;

    FileStream xsdFs = new FileStream(GvXSDFullPath, FileMode.Open);

    schemaManager.Compile(XmlReader.Create(xsdFs));
    SchemaValidator validator = schemaManager.NewSchemaValidator();

    FileStream xmlFs = new FileStream(GvXMLFullPath, FileMode.Open);

    validator.SetSource(XmlReader.Create(xmlFs));
    validator.ErrorList = new ArrayList();
    try
    {
      validator.Run();
    }
    catch (Exception)
    {
      AddMsg("Instance validation failed with " + validator.ErrorList.Count + " errors");
      foreach (StaticError error in validator.ErrorList)
      {
        AddMsg("At line " + error.LineNumber + ": " + error.Message);
      }
      txtResults.Text = GvResults;
      return;
    } // try catch
  }
  catch (Exception ex)
  {
    AddMsg(ex.ToString());
  }
  AddMsg("Process completed");
  txtResults.Text = GvResults;
}

Solution

  • You need the EE version. You can learn more about each version of Saxon here:

    http://www.saxonica.com/products/products.xml