I am not able to get passed a xsd validation on a specific element.
Before showing my code, please note the xsd I have was received by a 3rd-party, and that I am forced to work stick with, without changes.
The problem: For a given element, the xsd regex validation pattern is failing, with the message
The value '...' is invalid according to its datatype 'String' - The Pattern constraint failed.
Now, the pattern is awfully long and and complex enough for me to keep avoiding trying to understand it and resort to regex sample strings generators out there.
As examples of such, I did use the following:
1. http://uttool.com/text/regexstr/default.aspx
2. https://github.com/moodmosaic/Fare (C#)
In validating the auto generated samples with the tools mentioned above, I used http://www.regexr.com/.
I did not find one single auto generated sample where regexr.com did not confirmed it as a match.
Nevertheless, my schema validation is failing.
I created a code sample that illustrates the problem:
Xsd:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MyField">
<xsd:annotation>
<xsd:documentation>
MyField Display Text
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="([0-9]\d{3}((0[1-9]|1[012])(0[1-9]|1\d|2[0-8])|(0[13456789]|1[012])(29|30)|(0[13578]|1[02])31)|(15(8[48]|9[26])|(1[6-9]|[2-9]\d)(0[48]|[13579][26]|[2468][048])|([2468][048]|16|3579[26])00)0229)((0[0-9]|1[0-9]|2[0-3])([0-5]\d){2})" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
Xml:
<?xml version="1.0" encoding="utf-8" ?>
<MyField>
62710522201745
</MyField>
Validation code:
string xsdTestMarkup = File.ReadAllText(GetPath("TestSchema.xsd"));
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdTestMarkup)));
DirectoryInfo filesFolder = new DirectoryInfo("...myPath...");
FileInfo[] files = filesFolder.GetFiles("*.xml", SearchOption.TopDirectoryOnly);
List<XDocument> xmlDocs = new List<XDocument>();
foreach (FileInfo file in files)
{
xmlDocs.Add(XDocument.Load(file.FullName));
}
for(int i = 0; i < xmlDocs.Count; i++)
{
Console.WriteLine("Validating file [{0}]...", files[i].Name);
List<string> errors = new List<string>();
xmlDocs[i].Validate(schemas, (o, e) =>
{
errors.Add(e.Message);
});
File.WriteAllLines(GetPath("ValidationErrors" )+ Path.GetFileNameWithoutExtension(files[i].Name) + ".txt", errors);
}
What is wrong here? Is it the validation itself ? I have several other validations using "pattern" in this xsd (using different patterns), and they all pass without problems.