XML 在 PHP 中使用 XML-Reader 进行验证
XML Validation with XML-Reader in PHP
我在验证生成的 XML 字符串时遇到错误。我用 XML-Reader 加载了 XML-String 并分配了 XSD-File 进行验证。
有对象 ID 和 URL 可以根据允许的字符模式进行验证。我认为 ID 和网址是正确的。但是为什么验证过程会产生错误?
我收到这样的错误消息:
Element 'objectID': [facet 'pattern'] The value 'ffc89' is not accepted by the pattern '^[a-z]{1,1}[a-z0-9.-]{3,14}$'.
Element 'objectID': 'ffc89' is not a valid value of the local atomic type.
Element 'originUrl': [facet 'pattern'] The value 'http://domain.com/images/89/f972c66982290125.jpg' is not accepted by the pattern '^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+'.
Element 'originUrl': 'http://domain.com/images/89/f972c66982290125.jpg' is not a valid value of the local atomic type.
这是代码片段:
$reader = new XMLReader();
// we enable user error handling
libxml_use_internal_errors(true);
// load xml sructure for testing against xsd
$reader->xml($xml_str_tocheck);
$reader->setSchema($xsd_file_name);
// read xml structure
while( $reader->read() ) ;
// close xml
$reader->close();
// get found xml errors
$errors = libxml_get_errors();
// we disable user error handling
// (Disabling will also clear any existing libxml errors.)
libxml_use_internal_errors(false);
// check if xml is not valid
if( count($errors) )
{
foreach ($errors as $error)
{
echo $error->message;
}
}
这是用于验证的 XML- 字符串:
<?xml version="1.0" encoding="UTF-8"?>
<oimages startFetchDate="2015-06-10T12:48:20+00:00">
<object>
<objectID>ffc89</objectID>
<images>
<image>
<originUrl>http://domain.com/images/89/f972c66982290125.jpg</originUrl>
</image>
</images>
</object>
</oimages>
这是XSD-文件:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="images">
<xs:complexType>
<xs:sequence>
<xs:element name="object" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="objectID" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:maxLength value="15"/>
<xs:pattern value="^[a-z]{1,1}[a-z0-9.-]{3,14}$"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="images" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="image" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="url" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
<xs:pattern value="^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
您的 XML 对于您的 XSD 无效。
对您的 XSD 进行以下更改:
- 删除第 31 行
xs:element
的额外结束标记。
- 将根元素名称从
images
更改为 oimages
。
- 将
startFetchDate
属性添加到 oimages
。
- 从中删除前导
^
和尾随 $
^[a-z]{1,1}[a-z0-9.-]{3,14}$
因为 XSD 中的正则表达式
已经暗示在开始和结束时开始和结束
字符串。
- 从中删除前导
^
^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+
.
对 XSD 进行上述更改后,XML 将根据 XSD.
成功验证
我在验证生成的 XML 字符串时遇到错误。我用 XML-Reader 加载了 XML-String 并分配了 XSD-File 进行验证。
有对象 ID 和 URL 可以根据允许的字符模式进行验证。我认为 ID 和网址是正确的。但是为什么验证过程会产生错误?
我收到这样的错误消息:
Element 'objectID': [facet 'pattern'] The value 'ffc89' is not accepted by the pattern '^[a-z]{1,1}[a-z0-9.-]{3,14}$'.
Element 'objectID': 'ffc89' is not a valid value of the local atomic type.
Element 'originUrl': [facet 'pattern'] The value 'http://domain.com/images/89/f972c66982290125.jpg' is not accepted by the pattern '^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+'.
Element 'originUrl': 'http://domain.com/images/89/f972c66982290125.jpg' is not a valid value of the local atomic type.
这是代码片段:
$reader = new XMLReader();
// we enable user error handling
libxml_use_internal_errors(true);
// load xml sructure for testing against xsd
$reader->xml($xml_str_tocheck);
$reader->setSchema($xsd_file_name);
// read xml structure
while( $reader->read() ) ;
// close xml
$reader->close();
// get found xml errors
$errors = libxml_get_errors();
// we disable user error handling
// (Disabling will also clear any existing libxml errors.)
libxml_use_internal_errors(false);
// check if xml is not valid
if( count($errors) )
{
foreach ($errors as $error)
{
echo $error->message;
}
}
这是用于验证的 XML- 字符串:
<?xml version="1.0" encoding="UTF-8"?>
<oimages startFetchDate="2015-06-10T12:48:20+00:00">
<object>
<objectID>ffc89</objectID>
<images>
<image>
<originUrl>http://domain.com/images/89/f972c66982290125.jpg</originUrl>
</image>
</images>
</object>
</oimages>
这是XSD-文件:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="images">
<xs:complexType>
<xs:sequence>
<xs:element name="object" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="objectID" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:maxLength value="15"/>
<xs:pattern value="^[a-z]{1,1}[a-z0-9.-]{3,14}$"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="images" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="image" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="url" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
<xs:pattern value="^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
您的 XML 对于您的 XSD 无效。
对您的 XSD 进行以下更改:
- 删除第 31 行
xs:element
的额外结束标记。 - 将根元素名称从
images
更改为oimages
。 - 将
startFetchDate
属性添加到oimages
。 - 从中删除前导
^
和尾随$
^[a-z]{1,1}[a-z0-9.-]{3,14}$
因为 XSD 中的正则表达式 已经暗示在开始和结束时开始和结束 字符串。 - 从中删除前导
^
^(http|https){1}(://){1}[a-zA-Z0-9\-\./#?&_]+
.
对 XSD 进行上述更改后,XML 将根据 XSD.
成功验证