<?php

    
Class SchemaValidateException Extends Exception{
    }


    function 
libxml_display_error($error){
        return 
"Line $error->line: " trim($error->message);
    }

    function 
libxml_display_errors() {
        
$errors libxml_get_errors();
        
$result = array();
        foreach (
$errors as $error) {
            
$result[] = "Line $error->line: " trim($error->message); //libxml_display_error($error);
        
}
        
libxml_clear_errors();
        return 
$result;
    }

    function 
validateXMLAgainstSchema($schema$xml){
    
        if(
strlen(trim($xml)) == 0){
            throw new 
Exception('XML empty');
        }
        
// Enable user error handling
        
libxml_use_internal_errors(true);
            
        
$doc DOMDocument::loadXML($xml);

        if(!(
$doc instanceof DOMDocument)){
            throw new 
Exception(implode("\n"libxml_display_errors()));
        }

        if(!@
$doc->schemaValidate($schema)){
            throw new 
SchemaValidateException(implode("\n"libxml_display_errors()));
        }
    
        return 
true;
    }

    
$error NULL;
    
$success false;

    
    if(isset(
$_POST['submit'])){
        try{
            

            
/*Array
            (
                [xml] => <xml/>
                [submit] => Validate
            )
            Array
            (
                [schema] => Array
                    (
                        [name] => payload-schema.xsd
                        [type] => application/octet-stream
                        [tmp_name] => /Applications/MAMP/tmp/php/phprYiq0N
                        [error] => 0
                        [size] => 3551
                    )

            )*/
            
            
$xml $_POST['xml'];
            
$xsd $_FILES['schema']['tmp_name'];
            
            
validateXMLAgainstSchema($xsd$xml);
            
            
$success true;
            
        }
        catch(
SchemaValidateException $e){
            
$type 'Schema Errors';
            
$error $e->getMessage();
        }
        catch(
Exception $e){
            
$type 'XML Errors';
            
$error $e->getMessage();        
        }
    }
    
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Validate XML Against Schema</title>
    <link rel="stylesheet" type="text/css" media="screen" href="styles.css" />
  </head>
  <body>
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-181788-5']);
      _gaq.push(['_setDomainName', 'none']);
      _gaq.push(['_setAllowLinker', true]);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    
    </script>
    <div id="package">
      <div id="content">
        <h2>Validate XML against custom schema</h2>
        <form action="" method="post" enctype="multipart/form-data">
          <fieldset>

<?php

        
if($success == false && $error != NULL){
?>
        <p class="error message"><?php print "<strong>{$type}</strong><br />{$error}"?></p>
<?php            
        
}
        
        elseif(
$success == true){
?>
        <p class="success message">XML passed validation</p>
<?php
        
}

?>

            <p>
              <label class="file" for="schema">Schema</label>
              <input id="schema" name="schema" type="file" />
            </p>
            <p>
              <label for="xml">XML</label>
              <textarea id="xml" name="xml"><?php print $_POST['xml']; ?></textarea>
            </p>

            <div id="submission">
              <input id="submit" name="submit" type="submit" value="Validate" />
            </div>

          </fieldset>
        </form>
      </div>
      <div id="footer">
        <ul>
          <li>Written by Alistair Kearney</li>
          <li>
            <a href="index.phps">Source</a>
          </li>
          <li>
            <a href="http://twitter.com/pointybeard">Twitter</a>
          </li>
        </ul>
      </div>
    </div>
  </body>
</html>