Support #6611
openXSD Validation Throws Exception when validation fails.
0%
Description
Hi,
I am trying to perform a schema validation with xsd file. Everything works fine when file is validated successfully. But when schema can't be validated, it throws "SaxonApiException". Is there any way to let it not throw exception ? Or is this natural way to handle the validation errors ? Also, when i have to get multiple errors from validation, it's just giving only one validation error.
Error Message :
One validation error was reported: In content of element <documentInfo>: The content model does not allow element <gl-cor:uniqueID2> to appear immediately after element <entriesType>. The following elements would be valid here: gl-muc:defaultCurrency, gl-bus:targetApplication, gl-bus:sourceApplication, gl-bus:periodUnitDescription, gl-bus:periodUnit, gl-bus:periodCount, gl-cor:periodCoveredEnd, gl-cor:periodCoveredStart, gl-cor:entriesComment, gl-bus:creator, and 5 others (or nothing).
Also here is the validation code :
void SchemaValidation()
{
var validateFilePath = $@"E:\aa\xsdtest\some.xml";
var xsdFile = $@"E:\aa\xsdtest\xsd\edefter.xsd";
var xsdFileUri = new Uri(xsdFile);
var validateFileUri = new Uri(validateFilePath);
var processor = new Processor();
processor.SchemaManager.Compile(xsdFileUri);
var destination = new XdmDestination();
List<string> _errors = new List<string>();
SchemaValidationErrorHandler schemaValidationErrorHandler = new SchemaValidationErrorHandler();
var schemaValidator = processor.SchemaManager.NewSchemaValidator();
schemaValidator.UseXsiSchemaLocation = true;
schemaValidator.InvalidityListener = errors => schemaValidationErrorHandler.Test(errors);
try
{
schemaValidator.Validate(validateFileUri);
}
catch (SaxonApiException ex)
{
}
}
I can provide xml and xsd if it's needed.
Files
Updated by Michael Kay 4 days ago
The API is defined to throw an exception if the document is invalid. Whether that's a good design or not, I'm not really sure, but it's not something we can now change.
You could use the validateMultiple()
method instead; this suppresses the exception.
As for detecting more than one error, there are some errors where processing can sensibly continue; there are others where carrying on would lead to spurious errors being reported. It therefore depends on the particular condition. So we would need to look at the specific schema and invalid document to comment on this.
Updated by Nejdet Cemal Celik 4 days ago
Michael Kay wrote in #note-1:
The API is defined to throw an exception if the document is invalid. Whether that's a good design or not, I'm not really sure, but it's not something we can now change.
You could use the
validateMultiple()
method instead; this suppresses the exception.As for detecting more than one error, there are some errors where processing can sensibly continue; there are others where carrying on would lead to spurious errors being reported. It therefore depends on the particular condition. So we would need to look at the specific schema and invalid document to comment on this.
Hello.
Its okay. I just wanted to know is it possible to suppress or not :) Thank you for the clarification. Normally, I have a valid document. But I am changing the order of the elements and also added one invalid named element which doesn't belongs to that namespace. But it's still throwing the error which tells me that ordering is the issue. I can provide schema and xml file if you want. But is there any way to share it in private with you ?
Updated by Michael Kay 3 days ago
I believe that you can post attachments here and mark them as private, but I'm not sure exactly what the rules are. If that doesn't work for you, use the support at saxonica.com email address.
Updated by Nejdet Cemal Celik 3 days ago
I have sent the files to the support email. Can you check on them when you are available ? It could be rejected because of the attachments.
Updated by Michael Kay 3 days ago
Thanks for sending your sample schema and instance document.
Without being familiar with this rather complex schema, it's difficult to play around with possible invalidities. But I tried making a couple of elements invalid against simple types - on line 11, changed <xbrli:instant>2024-12-04</xbrli:instant>
to an invalid date; on line 88, changed totalDebit to an invalid numeric; and it now reports three errors.
I'm running this from the command line: if you get different behaviour from the Java API, please let me know.
If an error is found matching the content of an element against a complex type, then validation of that element will generally be abandoned at that point, and validation will resume with the next element. Perhaps that is the effect you are seeing. Generally, once you've found one error in a particular element, it's not productive to look for others, because they will usually be spurious.
Updated by Nejdet Cemal Celik 3 days ago
Hi,
Thank you for the response. Actually, changing orders of the elements also works to raise an error. Also, you are right about just reporting the first error. But if they will fix first error, then it is going to raise another error afterwards and that might cause an issue. I am having this error on .NET 7 project. When I tried to do this, its just reports the first error as I said. If you have any recommendations about my source code, I will be glad to hear it.
Please register to edit this issue