logo PHP XML Classes
A collection of classes and resources to process XML using PHP

Description: Schematron is a validation language for XML. This is a class to process Schematron validations from PHP. XML documents can be validated from files, URLs or PHP strings and the Schematron script can be processed from a file, script or string too. Schematron scripts can be "compiled" into XSLT stylesheets. Methods to validate files, documents or URLs using the compiled scripts are provided as well.

Schematron (class_schematron.php)
PHP XML Classes
Schematron (class_schematron.php)

Description: This class implements the Schematron 1.5 language for XML validation. Schematron scripts can be used directly from XML or pre-compiled into an XSLT stylesheet for later usage. XML files to be validated can be read from disk files or php strings.

NEWS:
  • 07-04-02 Documentation updated and package rebuilt.
  • 06-20-02 BugFixes.
  • 05-15-02 First version of this class released.
This class code as well as the documentation are hosted at SourceForge please visit our SourceForge page for releases, documentation, bug-tracking, support forums and mailing lists.

Resources Requirements
  • PHP 4.0.5+
  • XSLT extension (Sablotron)

Features To-dos
  • XML files to be validated can be read from PHP strings or files
  • Schematron scripts can be read from files or PHP strings
  • Schematron scripts can be used compiled or uncompiled
  • The class caches schematron compilations in order to prevent a schematron script from being compiled twice.
  • The Schematron 1.5 XSLT implementation is "inlined" in the class, no aditional files are needed.

Contact:

Detailed description and usage:
Using the class:

Initialization:
include_once("class_schematron.php");
$s = new Schematron();

Compiling a Schematron script (optional):
$s->compile_schematron_from_file("validation_sample1.xml");
// You can get the compiled schematron as a string
$compiled=$s->get_compiled("validation_sample1.xml");
// Or you can save it as a file
$s->save_compiled("validation_sample1.xml","validation1.xsl");

Running an uncompiled schematron script from a file to validate an XML file:
$ret=$s->schematron_validate_file_using_file("sample1.xml","validation_sample1.xml");

Running an uncompiled schematron script from mem to validate an XML file:
$ret=$s->schematron_validate_file_using_mem("sample1.xml",$schematron);

Running an uncompiled schematron from a file to validate an XML document in memory:
$ret=$s->schematron_validate_mem_using_file($xml,"validation_file.xml");

Running an uncompiled schematron from mem to validate an XML document in memory:
$ret=$s->schematron_validate_mem_using_mem($xml,$schematron);

Running an compiled schematron script from a file to validate an XML file:
$ret=$s->schematron_validate_file_using_compiled_file("sample1.xml","validation_sample1.xml");

Running an compiled schematron script from mem to validate an XML file:
$ret=$s->schematron_validate_file_using_compiled_mem("sample1.xml",$schematron);

Running an compiled schematron from a file to validate an XML document in memory:
$ret=$s->schematron_validate_mem_using_compiled_file($xml,"validation_file.xml");

Running an compiled schematron from mem to validate an XML document in memory:
$ret=$s->schematron_validate_mem_using_compiled_mem($xml,$schematron);


Documentation

Classes

class_schematron

Extends: None
Description: This class implements the Schematron 1.5 language allowing to validate XML files from PHP using Schematron.

Method Summary
 void Schematron()
          Constructor
 string compile_schematron_from_file(string $filename)
          Compiles a schematron file to XSLT
 string compile_schematron_from_mem(string $xml)
          Compiles a Schematron script from a PHP string to an XSLT stylesheet
 string get_compiled(string $xml_filename)
          Returns the compiled XSLT stylesheet for a Schematron script
 boolean save_compiled(string $xml_filename, string $filename)
          Saves a previously compiled Schematron script to a file
 string validate_mem_using_mem(string $xml_string, string $validation_string)
          Validates an XML string using a Schematron script from a string.
 string validate_mem_using_file(string $xml_string, string $validation_filename)
          Validates an XML document in a string using a Schematron script from a file
 string validate_file_using_mem(string $xml_filename, string $validation_string)
          Validates an XML document in a file using a Schematron script from a PHP string
 string validate_file_using_file(string $xml_filename, string $validation_filename)
          Validates an XML document in a file using a Schematron script from a file
 string validate_mem_using_compiled_mem(string $xml_string, string $validation_string)
          Validates an XML document held in a PHP string using a compiled Schematron scrip
 string validate_mem_using_compiled_file(string $xml_string, string $validation_filename)
          Validates an XML document held in memory using a compiled Schematron script from
 string validate_file_using_compiled_mem(string $xml_filename, string $validation_string)
          Validates an XML documen in a file using a compiled Schematron script from a PHP
 string validate_file_using_compiled_file(string $xml_filename, string $validation_filename)
          Validates an XML document in a file using a compiled Schematron script in a file
 

Method Detail

Schematron

void Schematron(string $xml_filename, string $validation_filename)
This is the class constructor
 
Parameters:
Returns:
Nothing
Throws:
None

compile_schematron_from_file

string compile_schematron_from_file(string $filename)
Compiles a schematron 1.5 script generating an XSLT stylesheet.
 
Parameters:
$filename - Name of the file containing the Schematron script.
Returns:
The compiled Schematron script as an XSLT stylesheet
Throws:
XSLT errors if it cannot compile the Schematron file.

compile_schematron_from_mem

string compile_schematron_from_mem(string $xml)
This compiles a schematron 1.5 script held in memory in a PHP string returning an XSLT stylesheet that can be used to validate XML documents following the rules defined in the script.
 
Parameters:
$xml - String containing the Schematron 1.5 script
Returns:
The XSLT stylesheet corresponding to the validation. (The compiled schematron script)
Throws:
Can raise an error if the Schematron cannot be compiled (XSLT errors)

get_compiled

string get_compiled(string $xml_filename)
When a schematron script is used or compiled from a file the schematron class stores a compiled representation of the script as an XSLT stylesheet, you can obtain this stylesheet and use it in methods receiving a compiled Schematron script.
 
Parameters:
$xml_filename - This is the name of the file that originally held the Schematron script, note that the file doesn't need to exist now. The name should match the name used in previously invoked validation method or compilation method.
Returns:
False if the script was never used or compiled. or The Schematron compiled XSLT stylesheet corresponding to the filename provided as a parameter (the name of the file that originally held the Schematron script).
Throws:
None

save_compiled

boolean save_compiled(string $xml_filename, string $filename)
This method is used to save a previously compiled or used Schematron script as an XSLT stylesheet to a file.
 
Parameters:
$xml_filename - This is the name that was used in a compilation or validation method using a Schematron script from a filename, one used or compiled the class stores a compiled representation as an XSLT stylesheet that can now be saved to a file to be used later as a compiled Schematron script.
$filename - This is the name of the file where the method will write the compiled (XSLT) schematron script.
Returns:
true if the compiled scripts exists in the class or false if not.
Throws:
Can throw an error if the file cannot be saved to disk.

validate_mem_using_mem

string validate_mem_using_mem(string $xml_string, string $validation_string)
Validates an XML string using a Schematron script from a string.
 
Parameters:
$xml_string - string containing the XML document to be validated
$validation_string - String containing the Schematron script to be used
Returns:
The result of the validation.
Throws:
An XSLT error if the schematron script is invalid

validate_mem_using_file

string validate_mem_using_file(string $xml_string, string $validation_filename)
Validates an XML document in a string using a Schematron script from a file. The compiled Schematron script will be cached and can be saved.
 
Parameters:
$xml_string - String containing the XML document to be validated
$validation_filename - Filename of the Schematron script to be used
Returns:
The result of the validation
Throws:
Can throw an XSLT error if the Schematron script is invalid.

validate_file_using_mem

string validate_file_using_mem(string $xml_filename, string $validation_string)
Validates an XML document in a file using a Schematron script from a PHP string
 
Parameters:
$xml_filename - Name of the file contaning the XML document to be validated
$validation_string - A PHP string containing the Schematron script to be used
Returns:
The result of the validation
Throws:
Can throw an error (XSLT) if the Schematron script is invalid.

validate_file_using_file

string validate_file_using_file(string $xml_filename, string $validation_filename)
Validates an XML document in a file using a Schematron script from a file
 
Parameters:
$xml_filename - Name of the file containing the XML document to be validated
$validation_filename - Name of the filename containing the Schematron script to be used
Returns:
The result of the validation
Throws:
Can throw an XSLT error if the Schematron script is invalid

validate_mem_using_compiled_mem

string validate_mem_using_compiled_mem(string $xml_string, string $validation_string)
Validates an XML document held in a PHP string using a compiled Schematron script contained in a PHP string as an XSLT stylesheet.
 
Parameters:
$xml_string - A PHP string containing the XML document to be validated
$validation_string - A PHP string containing a compiled Schematron script (as XSLT)
Returns:
The result of the validation.
Throws:
Can throw an XSLT error

validate_mem_using_compiled_file

string validate_mem_using_compiled_file(string $xml_string, string $validation_filename)
Validates an XML document held in memory using a compiled Schematron script from a file. The compiled script should be an XSLT stylesheet representing the Schematron transformation to be done.
 
Parameters:
$xml_string - A PHP string containing the XML document to be validated
$validation_filename - Name of the file containing the compiled version of the Schematron script
Returns:
The result of the validation.
Throws:
Can throw an XSLT error if the compiled script has errors

validate_file_using_compiled_mem

string validate_file_using_compiled_mem(string $xml_filename, string $validation_string)
Validates an XML documen in a file using a compiled Schematron script from a PHP string
 
Parameters:
$xml_filename - Name of the file containing the XML document to be validated
$validation_string - PHP String containing the compiled version of a Schematron script
Returns:
The result of the validation
Throws:
Can throw an XSLT error if the compiled Schematron script has errors

validate_file_using_compiled_file

string validate_file_using_compiled_file(string $xml_filename, string $validation_filename)
Validates an XML document in a file using a compiled Schematron script in a file
 
Parameters:
$xml_filename - Name of the file containing the XML document to be validated
$validation_filename - Name of the file containing the compiled representation of the Schematron script to be used
Returns:
The result of the validation
Throws:
Can throw an XSLT error if the compiled script has errors


Contribute!: If you want to contribute a class to this project or help with new versions of existing classes please let me know it by emaill Hosted at:
SourceForge.net Logo
Contact & credits
Luis Argerich
Rogerio

OSI Certified Open Source Software
OSI
RSS FEED XHTML