PHP: DOMDocument->schemaValidate() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument->schemaValidateSource() DOMDocument->saveXML()

Last updated: Fri, 26 Jan 2007

view this page in

# DOMDocument->schemaValidate()

(no version information, might be only in CVS)

DOMDocument->schemaValidate() -- Validates a document based on a schema

## Описание

class DOMDocument {
bool schemaValidate ( string filename )
}

Validates a document based on the given schema file.

## Список параметров

`filename`

  The path to the schema.

## Возвращаемые значения

Возвращает `TRUE` в случае успешного завершения или `FALSE` в случае возникновения ошибки.

## Смотрите также

DOMDocument->schemaValidateSource()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->validate()

add a note User Contributed Notes
DOMDocument->schemaValidate()
nevyn at NOSPAM dot email dot it
18-Dec-2006 05:41
`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.`
justin at redwiredesign dot com
08-Nov-2006 03:32
`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.`
Mike A.
17-Feb-2006 12:03
`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`
add a note
DOMDocument->schemaValidateSource() DOMDocument->saveXML()

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST