Question

Je suis en train de tester validation XSD en utilisant saxonne. Quand je reçois à la validation réelle, seule la première erreur est prise parce que validator.Run () renvoie une exception quand il arrive à la première erreur, et ne continue pas après. Ceci est évidemment pas ce que vous voulez, quand vous avez un fichier xml avec beaucoup d'erreurs. Est-il possible de poursuivre la validation après l'exception est levée ou est-il une autre méthode de validation à l'aide saxonne?

Ce code est basé sur l'exemple de validation saxonne dans le dossier il de la documentation des échantillons de ce qui est la section qui exécute la validation.

SchemaValidator validator = manager.NewSchemaValidator();
using (Stream xmlFile = File.OpenRead(fileName))
{
    using (XmlReader xmlValidatingReader = XmlReader.Create(xmlFile))
    {
        validator.SetSource(xmlValidatingReader);
        validator.ErrorList = new ArrayList();
        try
        {
            validator.Run();
        }
        catch (Exception)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
            foreach (StaticError error in validator.ErrorList)
            {
                sb.AppendLine("At line " + error.LineNumber + ": " + error.Message);
                tbXsdOutput.Text = sb.ToString();
            }
            return;
        }
    }
}
Était-ce utile?

La solution

Here's how I configured Saxonica to return more than one error:

proc.SetProperty(net.sf.saxon.lib.FeatureKeys.VALIDATION_WARNINGS,"true");

Working code below:

static void Main(string[] args)
{
    try
    {
        errors = new ArrayList();
        Saxon.Api.Processor proc = new Processor(true);
        proc.SetProperty(net.sf.saxon.lib.FeatureKeys.VALIDATION_WARNINGS,"true");
        //this is the property to set!

        SchemaManager schemaManager = proc.SchemaManager;

        FileStream xsdFs = new FileStream(@"C:\path\to.xsd", FileMode.Open);

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

        FileStream xmlFs = new FileStream(@"C:\path\to.xml", FileMode.Open);

        schemaValidator.SetSource(XmlReader.Create(xmlFs));
        schemaValidator.ErrorList = errors;
        schemaValidator.Run();
    }
    catch(net.sf.saxon.type.ValidationException e)
    {
        foreach(StaticError error in errors)
        {
            Console.WriteLine(error.ToString());
        }
        Console.ReadKey(true);
        Environment.Exit(0);

    }

    foreach (StaticError error in errors)
    {
        Console.WriteLine(error.ToString());
    }
    Console.ReadKey(true);
}

You can see more about the VALIDATION_WARNINGS option here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top