0

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.

Veverke
  • 9,208
  • 4
  • 51
  • 95
  • Looks like it should work. why are you using sometimes [0-9] and sometimes \d? e.g. at the beginning [0-9]\d{3} is the same as [0-9]{4}... maybe it works if you replace the \d by [0-9] – maraca Jul 28 '15 at 09:56
  • I wrote at the very beginning of the post "please note..." meaning - please do not ask me why am I using this awful regex :). I have no idea why they did it this way - it's purpose is ultimately to validate date fields !!! Something that can be done with way simpler regexes... I am trying to request a change from the 3rd party body... but one is not supposed to rely on that. – Veverke Jul 28 '15 at 10:06
  • @maraca: by the way, "looks like it should work" means you run the sample and do not get an error in the generated .txt output ? – Veverke Jul 28 '15 at 10:07
  • Ok sorry, you are right, somehow forgot that if you can't change the xsd you can't change that either of course. The other thing just meant I saw no mistake. And \ is often causing trouble, e.g. in regex created from strings in most languages they have to be doubled. The other thing I had in mind was too many groups, but doesn't make sense either. – maraca Jul 28 '15 at 11:24
  • @maraca: hmm... well, whatever... if I am granted permission to change the pattern then I should be ok. Thanks anyway. – Veverke Jul 28 '15 at 11:27
  • You're welcome, cough, sorry one last stupid remark: could it be that you have spaces around the number? 62710522201745 does it change anything. – maraca Jul 28 '15 at 11:36

1 Answers1

1

Try it without the whitespace. Seems to work fine.

Also note that the regular expression rules used in XSD's differ slightly from the standard regex rules, I don't think it makes any difference in this case, but worth knowing.

<?xml version="1.0" encoding="utf-8" ?>
<!-- Created with Liquid XML 2015 Designer Edition (Trial) 13.1.0.5909 (http://www.liquid-technologies.com) -->
<MyField>62710522201745</MyField>
Sprotty
  • 5,676
  • 3
  • 33
  • 52