0
votes

I have following Xml schema.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="type">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Type 1" />
      <xs:enumeration value="Type 2" />
      <xs:enumeration value="Type 3" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Element_1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="TypeName" type="type" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Element_2">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="TypeName" type="type" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I am validating following xml against the above schema.

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Element_1>
        <TypeName>Type 4</TypeName>
    </Element_1>
    <Element_3>
        <TypeName>Type 2</TypeName>
    </Element_3>
</Root>

I am expecting a validation error and I get one: The 'TypeName' element is invalid - The value 'Type 4' is invalid according to its datatype 'type' - The Enumeration constraint failed. The element 'Root' has invalid child element 'Element_3'. List of possible elements expected: 'Element_2'.

The Error message clearly shows the invalid elements (Element_3) and also displays list of possible valid elements (Element_2). Is it possible to display the valid list of 'TypeName'.

Now, I want error message similar to the one below: The 'TypeName' element is invalid - The value 'Type 4' is invalid according to its datatype 'type'. List of possible values expected: 'Type 1, Type 2' - The Enumeration constraint failed. The element 'Root' has invalid child element 'Element_3'. List of possible elements expected: 'Element_2'.

Is it possible to get above error message (or similar). Does any other restrictions other than xs:enumeration show all the valid values?

1
What XML/XSD tool chain are you using? (There are no standards that list how validation errors are reported; each tool has its own way.) – Richard
If I understood correctly, you are asking which tool am I using. I am using Visual Studio 2012. – lerner1225
IIRC VS has XML editing and XSLT debugging capabilities, but is not itself an XSD validator. ISTR it uses the .NET XSD support, which gives you no control over the output of error messages. You could make use of the types in the System.Xml.Schema namespace, but I'm not sure they're detailed enough to be able to get the possible values given a target element. I would be interested to see how you get on… – Richard

1 Answers

0
votes

@Richard: Thanks for the info.

Couldn't find a solution, but build a workaround.

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
    if (e.Exception != null && e.Exception.InnerException != null)
    {
        if (e.Exception.InnerException.Message == "The Enumeration constraint failed.")
        {
            if (((XElement)sender).Name == "TypeName")
            {
                Console.WriteLine("Error, valid Enums are Type 1, Type 2, Type 3...");
            }
        }
    }
    else
    {
        Console.WriteLine("Error.");
    }
}