NYCPHP Meetup

NYPHP.org

[nycphp-talk] Transforming XML with XSL (XsltProcessor problems)

Ben Sgro ben at projectskyline.com
Fri Mar 13 14:29:55 EDT 2009


Hey,

Thanks for the feedback and the sample code.
I'm going to look over this in more detail today.

Thanks again everyone!

- Ben

Daniel Convissor wrote:
> Hey Folks:
>
> On Thu, Mar 12, 2009 at 09:44:08PM -0400, Ajai Khattri wrote:
>   
>> I have to say, the error reporting for the XML and XSLT functions are 
>> terrible.
>>     
>
> Here's how I load XML.  Requres track_errors to be on.  Sorry about the 
> line wrapping.
>
>
> $xml_string = '';
> $schema_file = '';
>
> if (($dom = @DOMDocument::loadXML($xml_string)) == false) {
>     $error = 'Malformed XML: ' . $php_errormsg;
> } elseif ((@$dom->schemaValidate($schema_file)) == false) {
>     $error = 'XML violates schema ' . $schema_file . ': '
>             . format_schemavalidate_error($php_errormsg);
> } else {
>     if (($sxml = @simplexml_import_dom($dom)) === false) {
>         $error = 'Malformed XML (SimpleXML): ' . $php_errormsg;
>     }
> }
>
> if (!empty($error)) {
>     // process...
> }
>
>
>
> /**
>  * Boils down XML schema validation error messages into something
>  * humans can easily comprehend
>  *
>  * @param string $error  the $php_errormsg produced by DOM
>  *                       schemaValidate()
>  *
>  * @return string
>  */
> function format_schemavalidate_error($error) {
>     $error = preg_replace('/\s+/', ' ', $error);
>
>     if (preg_match("/The element: '([^']+)'/", $error, $match)) {
>         $error = $match[1] . ' has invalid value . ';
>     } elseif (preg_match("/Element '([^']+)', attribute '([^']+)'/", 
> $error, $match)) {
>         $error = $match[2] . ' attribute of ' . $match[1] . ' has invalid 
> value . ';
>     } elseif (preg_match("/Element '([^']+)': The attribute '([^']+)' is 
> required/", $error, $match)) {
>         $error = $match[2] . ' attribute of ' . $match[1] . ' is missing 
> . ';
>     } elseif (preg_match("/is not a valid value of the atomic type 
> '({.*})?([^']+)'/", $error, $match)) {
>         $error = $match[2] . ' has invalid value . ';
>     } elseif (preg_match('/Expecting: ([^.]+)/', $error, $match)) {
>         $error = $match[1] . ' is missing . ';
>     } elseif (preg_match('/(Expected is|Expected is one of) \( 
> ({.*})?([^.]+) \)/', $error, $match)) {
>         $error = $match[3] . ' is missing . ';
>     }
>
>     return $error;
> }
>
>
> Enjoy,
>
> --Dan
>
>   



More information about the talk mailing list