Приглашаем посетить
Русская библиотека (biblioteka-rus.ru)

Validating XML

Previous
Table of Contents
Next

Validating XML

$dom->relaxNGValidate('quotes.rng')


PHP 5 can validate XML against three types of files: Document Type Definitions (DTDs), Schemas (.xsd), and relaxNG. For the latter two, the following four methods of the DOM object exist:

  • schemaValidate('file.xsd') Validates against a Schema file

  • schemaValidateSource('...') Validates against a Schema string

  • relaxNGValidate('file.rng') Validates against a relaxNG file

  • relaxNGValidateSource('...') Validates against a relaxNG string

The preceding code uses relaxNGValidate() to validate a (well-formed) XML file against a nonmatching relaxNG file. If you change <element name="person"> to <element name="author"> in the file quotes.rng, the validation succeeds.

Validating XML Against relaxNG (validate-rng.php)
<?php
  $dom = new DOMDocument;
  $dom->load('quotes.xml');
  echo 'Validation ' .
    (($dom->relaxNGValidate('quotes.rng')) ?
'succeeded.' : 'failed.');
?>

TIP

Creating a relaxNG file can be quite difficult; the Java tool Trang, available at http://thaiopensource.com/relaxng/trang.html, can read in an XML file and create a relaxNG, Schema, or DTD file out of it.


Validating a Schema is similar and shown in the file validate-xsd.php in the download repository. When it comes to validating DTDs, you have to patch the XML a bit. The DTD file must be included in the file or referenced like this:

<!DOCTYPE note SYSTEM "quotes.dtd">

Then, just load the XML document into a DOM object and call validate(). The following contains the appropriate code; the file referenced in the code repository contains an intentional error in the DTD (month instead of year).

Validating XML Against a DTD (validate-dtd.php)
<?php
  $dom = new DOMDocument();
  $dom->load('quotes-dtd.xml');
  echo 'Validation ' .
    (($dom->validate()) ? 'succeeded.' : 'failed.');
?>

What Does PEAR Offer?

As of this writing, the XML section of PEAR contains 28 packages, too many to mention. Here are some of them:

  • XML_Beautifier formats XML documents so that they are prettier

  • XML_DTD allows parsing of DTDs, even with PHP 4

  • XML_Serializer converts XML files into data structures and vice versa

  • XML_Util contains a wealth of helper functions for working with XML



Previous
Table of Contents
Next