As of the 1st of February 2015 this wiki has been deprecated.
This archive is now a read only site.
There is also a copy of the history that has been converted into a git repository at: https://github.com/IATI/IATI-Wiki-Archive.
Please do not rely on any of the information found here.

XML Validation

xmllint

xmllint is a command line tool that can be used to check xml files for validation against the IATI Standard xml schemas.

Examples

Test an xml file against a schema

xmllint --noout --schema  http://iatistandard.org/downloads/iati-activities-schema.xsd some_data_file.xml
xmllint --noout --schema  http://iatistandard.org/downloads/iati-organisations-schema.xsd some_data_file.xml

To send the errors to a file

xmllint --noout --schema  http://iatistandard.org/downloads/iati-activities-schema.xsd some_data_file.xml 2> errors.txt

To make an XML file more readable in your text editor:

xmllint --format --recover input_file.xml > output_file.xml

To test a whole directory of files

xmllint --noout --schema <chose your schema!>.xsd *

Validate all files in a directory using PHP

The following code will supply you with 2 arrays of files that validate or fail validation Using the DOM loads the full file into memory, so large files might take time (but not that much!)

$dir = "";//The directory with your files in. A relative path should work 
$good_files =  $invalid_files = array();

if ($handle = opendir($dir)) {
          /* This is the correct way to loop over the directory. */
          while (false !== ($file = readdir($handle))) {
              if ($file != "." && $file != "..") { //ignore these system files
                  
                  //Load the XML into the DOM
                  $xml = new DOMDocument();
                  $xml->load($dir . $file);
                  
                  //Check to see if it is an organisation or activity file ans select the appropriate schema                  
                  if ($xml->getElementsByTagName("iati-organisation")->length == 0) {
                    $xsd = "http://iatistandard.org/downloads/iati-activities-schema.xsd";
                    if ($myinputs['org'] == "1") { //sanitized $_GET['orgs']
                      continue;
                    }
                  } else {
                    $xsd = "http://iatistandard.org/downloads/iati-organisations-schema.xsd";
                  }

                  //This is the validation bit
                  if ($xml->schemaValidate($xsd)) {
                      $good_files[] = $file; 
                      //print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
                      //if (isset($_GET['errors']) && $_GET['errors'] == "all") {
                      //  libxml_display_all_errors();
                      //} else {
                      //  libxml_display_just_files();
                      //}
                  } else {
                      $invalid_files[] = $file;
                  }
                  
                  
              }// end if file is not a system file
          } //end while
          closedir($handle);
    }


QR Code
QR Code XML Validation (generated for current page)