XML Validation with XML-Reader in PHP -
i've got errors during validation of generated xml string. loaded xml-string xml-reader , assigned xsd-file validation.
there object ids , urls validate against pattern of allowed characters. think ids , urls correct. why validation process generates errors?
i've got error messages this:
element 'objectid': [facet 'pattern'] value 'ffc89' not accepted pattern '^[a-z]{1,1}[a-z0-9.-]{3,14}$'. element 'objectid': 'ffc89' not valid value of local atomic type. element 'originurl': [facet 'pattern'] value 'http://domain.com/images/89/f972c66982290125.jpg' not accepted pattern '^(http|https){1}(://){1}[a-za-z0-9\-\./#?&_]+'. element 'originurl': 'http://domain.com/images/89/f972c66982290125.jpg' not valid value of local atomic type.
here code snippet:
$reader = new xmlreader(); // enable user error handling libxml_use_internal_errors(true); // load xml sructure testing against xsd $reader->xml($xml_str_tocheck); $reader->setschema($xsd_file_name); // read xml structure while( $reader->read() ) ; // close xml $reader->close(); // found xml errors $errors = libxml_get_errors(); // disable user error handling // (disabling clear existing libxml errors.) libxml_use_internal_errors(false); // check if xml not valid if( count($errors) ) { foreach ($errors $error) { echo $error->message; } }
this xml-string validation:
<?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>
this xsd-file:
<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>
your xml not valid respect xsd.
make following changes xsd:
- delete closing tag
xs:element
on line 31. - change root element name
images
oimages
. - add
startfetchdate
attributeoimages
. - remove leading
^
, trailing$
^[a-z]{1,1}[a-z0-9.-]{3,14}$
because regular expressions in xsd implied begin , end @ beginning , ending of string. - remove leading
^
^(http|https){1}(://){1}[a-za-z0-9\-\./#?&_]+
.
after making above changes xsd, xml validate against xsd.
Comments
Post a Comment