PHP 5.6.0beta1 released

DOMDocument::schemaValidate

(PHP 5)

DOMDocument::schemaValidate Valida un documento basado en un esquema

Descripción

public bool DOMDocument::schemaValidate ( string $filename [, int $flags ] )

Valida un documento basado en el fichero de esquema dado.

Parámetros

filename

La ruto al esquema.

flags

Una máscara de bits de banderas de validación de esquemas de Libxml. Actualmente el único valor admitido es LIBXML_SCHEMA_CREATE. Disponible desde PHP 5.5.2 y Libxml 2.6.14.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Historial de cambios

Versión Descripción
5.5.2 Se añadió el parámetro flags

Ver también

add a note add a note

User Contributed Notes 7 notes

up
2
NetPanther
5 years ago
Initial situation:
- Debian Lenny
- Apache 2 with PHP 5.2.6
- libxml 2.6.32

Problem: While trying to validate a manually created (!) DOMDocument against an existing XML Schema, I ran into a warning like the one below. Validation fails, even though the document IS valid and the command line tool xmllint confirms this (even with libxml 2.6.32, so this must be a problem with DOM). The validation works fine with libxml 2.7.3.

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element 'YourRootElementName': No matching global declaration available for the validation root. in /var/www/YourFile.php on line X

Solution: As libxml 2.7.3 is not provided for Debian Lenny yet and this problem seems to be caused by DOM (s.o.), I currently use the following workaround on my machines. DOM obviously has some namespace problems with documents, that have been created manually (i.e. they were not loaded from a file).

So my workaround is saving the DOMDocument temporarily, re-loading it and then validating the temporary DOMDocument. Strangely enough the validation of the same document (= same content) now works. Sure, creating a temporary file is not a nice solution, but unless this bug is fixed, this workaround should do just fine.

<?php 

// Works with libxml 2.7.3 and higher.
public function isValid()
{
  return
$this->dom->schemaValidate('schema.xsd');
}

// Workaround for prior libxml versions, e.g. 2.6.32.
public function isValid()
{
 
// Create temporary file and save manually created DOMDocument.
 
$tempFile = time() . '-' . rand() . '-document.tmp';
 
$this->dom->save($tempFile);
 
 
// Create temporary DOMDocument and re-load content from file.
 
$tempDom = new DOMDocument();
 
$tempDom->load($tempFile);
 
 
// Delete temporary file.
 
if (is_file($tempFile))
  {
   
unlink($tempFile);
  }
 
 
// Validate temporary DOMDocument.
 
return $tempDom->schemaValidate('schema.xsd');
}

?>
up
1
wkoehler at ce-gmbh dot com
3 years ago
In older versions of PHP5 this function might cause error messages when dealing with namespaces. I had problems with PHP 5.2.14 which comes with libXML V2.6.16. After switching to PHP 5.3.5 with libXML V2.7.7 I no longer had problems. Took me only about 30 hrs to figure that one out.
up
1
Mike A.
8 years ago
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself.  See http://php.net/manual/en/ref.libxml.php for more info.

example.xml
<?xml version="1.0"?>
<example>
    <child_string>This is an example.</child_string>
    <child_integer>Error condition.</child_integer>
</example>

example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
    <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="child_string" type="xs:string"/>
                <xs:element name="child_integer" type="xs:integer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<?php

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

    return
$return;
}

function
libxml_display_errors() {
   
$errors = libxml_get_errors();
    foreach (
$errors as $error) {
        print
libxml_display_error($error);
    }
   
libxml_clear_errors();
}

// Enable user error handling
libxml_use_internal_errors(true);

$xml = new DOMDocument();
$xml->load('example.xml');

if (!
$xml->schemaValidate('example.xsd')) {
    print
'<b>DOMDocument::schemaValidate() Generated Errors!</b>';
   
libxml_display_errors();
}

?>

Old error message:
Warning: DOMDocument::schemaValidate() [function.schemaValidate]: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.php on line 40

New error message:
DOMDocument::schemaValidate() Generated Errors!
Error 1824: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.xml on line 4
up
1
poletto at jeuxvideo dot com
6 years ago
I had a tricky issue using this method, i thought it was bug but i realized i misundestood something about namespaces.
When you want to use a schema to describe an xml document, you basically put the default namespace to a personnal namespace (and you refer to this namespace in the targetNamespace attribute of your schema).

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://my.uri.net">
  <elt>
    <x>blabla</x>
    <y>blabla</y>
  </elt>
</root>

That xmlns attribute specifies a "default namespace" and means that the root element and its children are in this namespace.
What i misunderstood is that, there's no way with the DOM api to specify a "default namespace" for each children of the root element.
Thus you may need to use createElementNS() and createAttributeNS() methods for each created element or attribute in the document specifying the URI of your namespace each time ("http://my.uri.net").

This only applies if you want to validate a document built with the API, not with a document loaded from an xml file or stream.
up
0
mati
8 months ago
If write any error detector from xml file using schema and libxml function watch out on some unreseloved bug

https://bugs.php.net/bug.php?id=54138
up
0
nevyn at NOSPAM dot email dot it
7 years ago
I've seen that trying to validate an XML with a schema containing keys, they were ignored, and the XML was considered still valid altough it contained duplicate keys.
up
0
justin at redwiredesign dot com
7 years ago
In his previous comment, Mike A wrote about validating documents using an XSD. However, you can validate without one. In my case, I needed to ensure that the content entered was just valid XML or not, and I couldn't find an XSD to support that. So I wrote this:

public static function validate($xml)
{
    libxml_use_internal_errors(true);

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->loadXML($xml);

    $errors = libxml_get_errors();
    if (empty($errors))
    {
        return true;
    }

    $error = $errors[0];
    if ($error->level < 3)
    {
        return true;
    }

    $lines = explode("\r", $xml);
    $line = $lines[($error->line)-1];

    $message = $error->message.' at line '.$error->line.':<br />'.htmlentities($line);

    return $message;
}

The catch here is that the function only checks for the first error is LIBXML_ERR_FATAL, which would break XSL/XML compilation.

In my experience, the errors are returned by libxml_get_errors in descending severity, so this may be an OK thing to do.
To Top