如何使用Saxon Home Edition(HE)9.4针对XSD验证XML

时间:2012-03-13 15:21:56

标签: xml validation xsd saxon

如何使用Saxon Home Edition(HE)9.4来针对XSD验证XML?

我是否需要企业版才能执行此基本操作,这是错误消息所暗示的内容?这是我正在使用的代码。

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;
}

1 个答案:

答案 0 :(得分:2)

您需要EE版本。您可以在此处了解有关Saxon每个版本的更多信息:

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