July 24, 2004

Validating XML with PHP using catalogs (on win32)

Problem: I had some XHTML-strict files which needed to be validated using PHP:

sample.html:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>XHTML validatation test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>

Now, in PHP we can use DOMXML as follows:


<?php
$domxml = domxml_open_file('sample.html');
if(is_object($domxml))
{
$error = array();
if($domxml->validate($error))
{
echo 'this file is valid!';
}
}
?>

But! Aaargh... PHP is now gonna download the DTD from http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd !!! Takes ages!
No, i want to validate against a local dtd!.

Luckily came across this interesting piece. It describes the usage of XML-Catalogs with PHP. Such a catalog redirects for example 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' to a DTD stored locally. PHP's DOMXML is build using GNome's libxml so we can use a catalog.

Steps for using XML Catalog with PHP on win32/IIS:
1] download the necessary XHTML 1.0 DTD's here
2] extract the zip into root of IIS (ie: C:\Inetpub\wwwroot)
3] create 'catalog.xml' in for example 'C:\Inetpub\wwwroot':

<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" 
	"http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<public publicId="-//W3C//DTD XHTML 1.0 Strict//EN" uri="xhtml1/xhtml1-20020801/DTD/xhtml1-strict.dtd" />
</catalog>

Finally:
4] set up a system environment variable 'XML_CATALOG_FILES' with value '<path-to-catalog.xml>

That's it! Now you can validate your xml agains locally stored DTD's!


Tim Knip's Log | © Copyright 2002-2003 by Suite75
Suite75 blogs
ARCHIVES


Send me an Mail

My Groove V-card

Subscribe to Tim Knip's Log

Powered by Movable Type