user1195101
user1195101

Reputation: 43

XSD schema validation error with regular expression

When I try to validate XML file with a XSD schema containing regular expression, the function DOMDocument::schemaValidate return a validation error.

But when I compare the value and the regular expression, it should be fine.

here is a part of my XSD :

 <xsd:complexType>
    <xsd:sequence>
       <xsd:element name="RL0201Ex" type="CODE_POSTAL" minOccurs="0" maxOccurs="1">
          <xsd:annotation>
             <xsd:documentation>
                Code postal de l'adresse postale
             </xsd:documentation>
          </xsd:annotation>
       </xsd:element>
    </xsd:sequence>
 </xsd:complexType>



<xs:simpleType name="CODE_POSTAL">
   <xs:restriction base="xs:string">
      <xs:pattern value="^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$" />
   </xs:restriction>
</xs:simpleType>

And my PHP code :

function libxml_display_error($error) {

    $return = "<br/>\n";
    $return .= "<b>Erreur ligne $error->line</b> : ";
    $return .= trim($error->message);
    $return .= "\n";

    return $return;
}

function libxml_display_errors($display_errors = true) {
    $errors = libxml_get_errors();
    $chain_errors = "";

    foreach ($errors as $error) {
        if ($error->code == "1839") {
            $chain_errors .= preg_replace('/( in\ \/(.*))/', '',     strip_tags(libxml_display_error($error)))."\n";
            if ($display_errors) {
                    echo(libxml_display_error($error));
            }
        }
    }
    libxml_clear_errors();

    return $chain_errors;
}

libxml_use_internal_errors(true);

$file   = "informations.xml";
$schema = "informations.xsd";

$dom = new DOMDocument("1.0");
$dom->load($file);

$validate = $dom->schemaValidate($schema);
if ($validate) {
   echo "<b>DOMDocument::schemaValidate() Valid schema !</b>";
} 
else {
   echo "<b>DOMDocument::schemaValidate() Generated Errors !</b><br /><br />";
   libxml_display_errors();
}

Thank you

Upvotes: 1

Views: 1720

Answers (1)

Michael Kay
Michael Kay

Reputation: 163342

In the XSD regular expression dialect, '$' is an ordinary character than matches a '$' sign, not the end of the string. You don't need anything to match the end of the string - the regex is implicitly anchored at both ends of the string.

Upvotes: 3

Related Questions