Skip to content

xml validation code is wrong #85

Description

@staabm

the code sample - provided by me in fa985ed - doesnt work :/.

I tried a lot of different ways but wasnt able to find one which works using the xml-extension.
As a workaround I localy work with a DOMDocument based approach:

    static private function validateXml($xmlString, $schema) {

        $libxml_display_error = function ($error)
        {
            $return = "";
            switch ($error->level) {
                case LIBXML_ERR_WARNING:
                    $return .= "Warning $error->code: ";
                    break;
                case LIBXML_ERR_ERROR:
                    $return .= "Error $error->code: ";
                    break;
                case LIBXML_ERR_FATAL:
                    $return .= "Fatal Error $error->code: ";
                    break;
            }
            $return .= trim($error->message);
            if ($error->file) {
                $return .=    " in $error->file";
            }
            $return .= " on line $error->line";

            return $return;
        };

        libxml_use_internal_errors(TRUE);

        $xml = new \DOMDocument();
        $xml->loadXml($xmlString);

        if (!$xml->schemaValidate($schema)) {
            echo '> DOMDocument::schemaValidate() Generated Errors!'."\n";
            echo 'Schema:'. $schema;

            var_dump($xmlString);

            $errors = libxml_get_errors();
            foreach ($errors as $error) {
                debug($libxml_display_error($error));
            }
            exit();
        }
    }

just to let you know...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions