Home > Software design >  Validating XML with XSD when XSD has escape characters in a pattern
Validating XML with XSD when XSD has escape characters in a pattern

Time:10-07

I am validating an XML with XSD provided, and one of the elements say last name, needs to allow the apostrophe or single-quote character ('), but when I run the validation using the below code, it does not validate as the XSD pattern has the escaped ' format for the ('). Is there an option to allow validation with the ' value in the pattern to match for '.

            XmlSchemaSet schema = new XmlSchemaSet();
            schema.Add(namespaceXsd, schemaPath);

            string validationMessage = string.Empty;
            docTOValidate.Validate(schema, (s, e) =>
            {
                validationMessage = e.Message;
            });

Below is the pattern applied in the XSD for the last name element, and the regex has the escaped '. So here is I pass Last' as value it fails validation.

 <xsd:element name="lastName">
    <xsd:simpleType>
       <xsd:restriction base="xsd:string">
           <xsd:pattern value="[A-Z]{1}[A-Z\p{Zs};&apos;\-]{1,29}" />
       </xsd:restriction>
    </xsd:simpleType>
 </xsd:element>

EDIT Below is the sample xml and the error message, could not copy all the filed names, but below shows the filed that has the exception

<Root>
    <Key>
        <ID>
            <S>000</S>
            <UD>111<UD>
        </ID>
        ...
        ...
        ...
    <Key>
    <Data>
        <firstIn>N</firstIn>
        <lastName>NAME'</lastName>
        ...
        ...
        ...
        ...
    </Data>
</Root>

Below is the exception

The 'http://www.ui-icon.org/ns/xxxx:lastName' element is invalid - The value 'NAME`' is invalid according to its datatype 'String' - The Pattern constraint failed.

Even if I pass the Name with escaped value, I still get the validation error, though the regex would validate fine for O'NAME

The 'http://www.ui-icon.org/ns/namespace:lastName' element is invalid - The value 'O&apos;NAME' is invalid according to its datatype 'String' - The Pattern constraint failed.

CodePudding user response:

The pattern you have written is supplied by the XML parser to the XSD schema processor as

[A-Z]{1}[A-Z\p{Zs};'\-]{1,29}

and you could equally well have written it that way directly, because escaping an apostrophe is unnecessary in this context.

Whatever your problem is, it has nothing to do with the distinction between &apos; and ', which the schema processor never even sees.

  • Related