Link to home
Start Free TrialLog in
Avatar of newaira
newaira

asked on

XML + XML Schema validation using PHP?

How can I validate an XML file described by XML Schema using PHP? I've searched for a while now and I cannot find anything about it. I think there is something on XML file + DTD validation on PEAR, but I'm looking for XML + XML Schema validation...

Thank you.
Avatar of hernst42
hernst42
Flag of Germany image

With php5 it should be possible, but not with php4
see
http://www.mail-archive.com/php-general@lists.php.net/msg149242.html
or
http://de3.php.net/manual/en/function.dom-domdocument-schemavalidate.php (The function which should do the schema-validation in php5)
Avatar of newaira
newaira

ASKER

That is no good because I'm working in php 4.3.5
I don't think that there are php-classes or functions written by other that would do that in PHP as that is a very complex thing to do (rewrite a lot of parts of the libxml2 in PHP). One hard way to go is to write your own php-extension for PHP4 that does call the validate via libxml2 C-functions
Avatar of newaira

ASKER

Do you know a good DTD vallidator? I am trying to use the one at http://pear.php.net/package/XML_DTD/ but it's all messed up...
No I don't know of a good DTD-Validator written in PHP or usable in PHP. Maybe there is a shell-command like xmllint from www.w3c.org and then do a exec('xmllint --option filename')
Avatar of newaira

ASKER

Well I found a good XML + XML Scheme validator!

1. Go to http://www.w3.org/2001/03/webdata/xsv
2. Enter the web path to the xml file to be validated against an XML schema.
3. Choose the 'xml, but labelled text/plain' radio button.
4. Get results. -> An XML file is displayed with the error messages (if any).

To use this in PHP I just used file_get_contents of the URL given after clicking 'Get results' and checked for any errors. Something like this:

$validate_xml = file_get_contents("http://www.w3.org/2001/03/webdata/xsv?docAddrs=http%3A%2F%2Fuwssa.org%2Fmodules%2FMembers%2Fmembers.xml&style=text");

if (substr_count(strtolower($validate_xml), "<fsm>") >= 1)
  echo "members.xml is invalid.";
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial