2012-02-07 96 views
1

當我嘗試使用包含常規 表達式的XSD模式驗證XML文件時,函數DOMDocument :: schemaValidate返回驗證錯誤。正則表達式的XSD模式驗證錯誤

  • 值'RU'不被模式'^ [A-Za-z] {2} $'接受。
  • 模式'^ [0-9] {4} $'不接受值'6258'。
  • 值'G9N3T5'不被模式'^ [A-Za-z] \ d [A-Za-z] \ d [A-Za-z] \ d $'接受。

但是,當我比較值和正則表達式時,應該沒問題。

這裏是我的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> 

我的PHP代碼:

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(); 
} 

謝謝

回答

3

在XSD正則表達式的話, '$' 是一個普通的字符比匹配'$'符號,而不是字符串的末尾。你不需要任何東西來匹配字符串的末尾 - 正則表達式隱式錨定在字符串的兩端。