正则表达式的 XSD 模式验证错误
当我尝试使用包含常规的 XSD 架构验证 XML 文件时 表达式,函数 DOMDocument::schemaValidate 返回验证错误。
- 模式“^[A-Za-z]{2}$”不接受值“RU”。
- 模式“^[0-9]{4}$”不接受值“6258”。
- 模式“^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$”不接受值“G9N3T5”。
但是当我比较该值和正则表达式时,应该没问题。
这是我的 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();
}
谢谢
When I try to validate XML file with a XSD schema containing regular
expression, the function DOMDocument::schemaValidate return a validation error.
- The value 'RU' is not accepted by the pattern '^[A-Za-z]{2}$'.
- The value '6258' is not accepted by the pattern '^[0-9]{4}$'.
- The value 'G9N3T5' is not accepted by the pattern '^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$'.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 XSD 正则表达式方言中,“$”是一个普通字符,与“$”符号匹配,而不是字符串结尾。您不需要任何内容来匹配字符串的末尾 - 正则表达式隐式锚定在字符串的两端。
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.