topical media & game development
server-php-xml-class-schematron-class-schematron.php / php
<?
// # # # # # # # # # # # # # # # # # # # ###
// Title : class_schematron.php
// Version : 1.0
// Author : Luis Argerich (lrargerich@yahoo.com)
// Last modification date : 05-15-2002
// Description : This class allows you to perform Schematron XML
// validations from PHP, it is based on the XSLT
// Schematron implementation so you need PHP configured
// with XSLT.
// # # # # # # # # # # # # # # # # # # # ###
// History:
// 05-15-2002 First release of this class
// 05-12-2002 Embedded the xslt file and the schematron 1.5 xslt into this class
// so you only need to include this file in order to use schematron.
// # # # # # # # # # # # # # # # # # # # ###
// To-Dos:
// # # # # # # # # # # # # # # # # # # # ###
// How to use it:
// You can have an XML file to be validated in memory or in a file
// and you need a schematron validation script in XML in memory or in a file.
// You can compile schematron_scripts to xslt files using the compile_from_mem
// or compile_from_file methods.
// Then you can validate with several flavours depeding on the XML source,
// the schematron source or if the schematron is compiled or not.
// VERY SIMPLE EXAMPLE:
//
// result=
// # # # # # # # # # # # # # # # # # # # ###
// CHECK FOR DOUBLE DEFINITION
if(defined("_class_schematron_is_included")) {
// do nothing since the class is already included
} else {
define("_class_schematron_is_included",1);
class Schematron {
var compiled;
var this->sk15='<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" xmlns:sch="http://www.ascc.net/xml/schematron" ><xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/><xsl:output method="xml" omit-xml-declaration="no" standalone="yes" indent="yes"/><xsl:param name="block"></xsl:param><xsl:param name="phase"><xsl:choose><xsl:when test="//sch:schema/@defaultPhase"><xsl:value-of select="//sch:schema/@defaultPhase"/></xsl:when><xsl:otherwise>#ALL</xsl:otherwise></xsl:choose></xsl:param><xsl:param name="hiddenKey"> key </xsl:param><xsl:template match="sch:schema | schema"><axsl:stylesheet version="1.0"> <xsl:for-each select="sch:ns | ns"> <xsl:attribute name="{concat(@prefix,'."'".':dummy-for-xmlns'."'".')}" namespace="{@uri}"/></xsl:for-each> <xsl:if test="count(sch:title/* | title/* )"> <xsl:message> <xsl:text>Warning: </xsl:text> <xsl:value-of select="name(.)"/> <xsl:text> must not contain any child elements</xsl:text> </xsl:message> </xsl:if> <xsl:call-template name="process-prolog"/><axsl:template match="*|@*" mode="schematron-get-full-path"> <axsl:apply-templates select="parent::*" mode="schematron-get-full-path"/><axsl:text>/</axsl:text><axsl:if test="count(. | ../@*) = count(../@*)">@</axsl:if> <axsl:value-of select="name()"/> <axsl:text>[</axsl:text> <axsl:value-of select="1+count(preceding-sibling::*[name()=name(current())])"/> <axsl:text>]</axsl:text></axsl:template><xsl:apply-templates mode="do-keys" select="sch:pattern/sch:rule/sch:key | pattern/rule/key | sch:key | key "/> <axsl:template match="/"> <xsl:call-template name="process-root"><xsl:with-param name="fpi" select="@fpi"/><xsl:with-param xmlns:sch="http://www.ascc.net/xml/schematron" name="title" select="./sch:title | title"/><xsl:with-param name="id" select="@id"/><xsl:with-param name="icon" select="@icon"/><xsl:with-param name="lang" select="@xml:lang"/><xsl:with-param name="version" select="@version" /><xsl:with-param name="schemaVersion" select="@schemaVersion" /><xsl:with-param name="contents"><xsl:apply-templates mode="do-all-patterns"/></xsl:with-param> </xsl:call-template> </axsl:template> <xsl:apply-templates/> <axsl:template match="text()" priority="-1"></axsl:template> </axsl:stylesheet></xsl:template> <xsl:template match="sch:active | active"><xsl:if test="not(@pattern)"><xsl:message>Markup Error: no pattern attribute in <active></xsl:message></xsl:if><xsl:if test="//sch:rule[@id= current()/@pattern]"><xsl:message>Reference Error: the pattern "<xsl:value-of select="@pattern"/>" has been activated but is not declared</xsl:message></xsl:if></xsl:template><xsl:template match="sch:assert | assert"><xsl:if test="not(@test)"><xsl:message>Markup Error: no test attribute in <assert></xsl:message></xsl:if><axsl:choose><axsl:when test="{@test}"/><axsl:otherwise><xsl:call-template name="process-assert"><xsl:with-param name="role" select="@role"/><xsl:with-param name="id" select="@id"/><xsl:with-param name="test" select="normalize-space(@test)" /><xsl:with-param name="icon" select="@icon"/><xsl:with-param name="subject" select="@subject"/><xsl:with-param name="diagnostics" select="@diagnostics"/></xsl:call-template></axsl:otherwise></axsl:choose></xsl:template><xsl:template match="sch:report | report"><xsl:if test="not(@test)"><xsl:message>Markup Error: no test attribute in <report></xsl:message></xsl:if><axsl:if test="{@test}"> <xsl:call-template name="process-report"><xsl:with-param name="role" select="@role"/><xsl:with-param name="test" select="normalize-space(@test)" /><xsl:with-param name="icon" select="@icon"/><xsl:with-param name="id" select="@id"/><xsl:with-param name="subject" select="@subject"/><xsl:with-param name="diagnostics" select="@diagnostics"/></xsl:call-template></axsl:if></xsl:template><xsl:template match="sch:diagnostic | diagnostic"><xsl:if test="not(@id)"><xsl:message>Markup Error: no id attribute in <diagnostic></xsl:message></xsl:if><xsl:call-template name="process-diagnostic"><xsl:with-param name="id" select="@id" /></xsl:call-template></xsl:template><xsl:template match="sch:diagnostics | diagnostics"/><xsl:template match="sch:dir | dir" mode="text"><xsl:call-template name="process-dir"><xsl:with-param name="value" select="@value"/></xsl:call-template></xsl:template><xsl:template match="sch:emph | emph" mode="text"><xsl:call-template name="process-emph"/></xsl:template><xsl:template match="sch:extends | extends"><xsl:if test="not(@rule)"><xsl:message>Markup Error: no rule attribute in <extends></xsl:message></xsl:if><xsl:if test="not(//sch:rule[@abstract='."'".'true'."'".'][@id= current()/@rule] ) and not(//rule[@abstract='."'".'true'."'".'][@id= current()/@rule])"><xsl:message>Reference Error: the abstract rule "<xsl:value-of select="@rule"/>" has been referenced but is not declared</xsl:message></xsl:if><xsl:call-template name="IamEmpty" /><xsl:if test="//sch:rule[@id=current()/@rule]"><xsl:apply-templates select="//sch:rule[@id=current()/@rule]" mode="extends"/></xsl:if></xsl:template><xsl:template match="sch:key | key " mode="do-keys" ><xsl:if test="not(@name)"><xsl:message>Markup Error: no name attribute in <key></xsl:message></xsl:if><xsl:if test="not(@match) and not(../sch:rule)"><xsl:message>Markup Error: no match attribute on <key> outside <rule></xsl:message></xsl:if><xsl:if test="not(@path)"><xsl:message>Markup Error: no path attribute in <key></xsl:message></xsl:if><xsl:call-template name="IamEmpty" /><xsl:choose><xsl:when test="@match"><axsl:key match="{@match}" name="{@name}" use="{@path}"/></xsl:when><xsl:otherwise><axsl:key name="{@name}" match="{parent::sch:rule/@context}" use="{@path}"/></xsl:otherwise></xsl:choose></xsl:template><xsl:template match="sch:key | key" /> <xsl:template match="sch:name | name" mode="text"><axsl:text xml:space="preserve"> </axsl:text><xsl:if test="@path" ><xsl:call-template name="process-name"><xsl:with-param name="name" select="concat('."'".'name('."'".',@path,'."'".')'."'".')"/></xsl:call-template></xsl:if><xsl:if test="not(@path)" ><xsl:call-template name="process-name"><xsl:with-param name="name" select="'."'".'name(.)'."'".'"/></xsl:call-template></xsl:if><xsl:call-template name="IamEmpty" /><axsl:text xml:space="preserve"> </axsl:text></xsl:template><xsl:template match="sch:ns | ns" mode="do-all-patterns" ><xsl:if test="not(@uri)"><xsl:message>Markup Error: no uri attribute in <ns></xsl:message></xsl:if><xsl:if test="not(@prefix)"><xsl:message>Markup Error: no prefix attribute in <ns></xsl:message></xsl:if><xsl:call-template name="IamEmpty" /><xsl:call-template name="process-ns" ><xsl:with-param name="prefix" select="@prefix"/><xsl:with-param name="uri" select="@uri"/></xsl:call-template></xsl:template><xsl:template match="sch:ns | ns" /><xsl:template match="sch:schema/sch:p | schema/p" mode="do-schema-p" ><xsl:call-template name="process-p"><xsl:with-param name="class" select="@class"/><xsl:with-param name="icon" select="@icon"/><xsl:with-param name="id" select="@id"/><xsl:with-param name="lang" select="@xml:lang"/></xsl:call-template></xsl:template><xsl:template match="sch:pattern/sch:p | pattern/p" mode="do-pattern-p" ><xsl:call-template name="process-p"><xsl:with-param name="class" select="@class"/><xsl:with-param name="icon" select="@icon"/><xsl:with-param name="id" select="@id"/><xsl:with-param name="lang" select="@xml:lang"/></xsl:call-template></xsl:template><xsl:template match="sch:phase/sch:p" /><xsl:template match="sch:p | p" /><xsl:template match="sch:pattern | pattern" mode="do-all-patterns"><xsl:if test="(phase)]/sch:active[@pattern= current()/@id]) or (../phase[@id= (phase = '."'".'#ALL'."'".') or (../sch:phase[@id= (phase)]/active[@id= current()/@id])"><xsl:apply-templates/><axsl:template match="text()" priority="-1" mode="M{count(preceding-sibling::*)}"></axsl:template></xsl:if></xsl:template><xsl:template match="sch:phase | phase" ><xsl:if test="not(@id)"><xsl:message>Markup Error: no id attribute in <phase></xsl:message></xsl:if></xsl:template><xsl:template match="sch:rule[not(@abstract='."'".'true'."'".')] | rule[not(@abstract='."'".'true'."'".')]"><xsl:if test="not(@context)"><xsl:message>Markup Error: no context attribute in <rule></xsl:message></xsl:if><axsl:template match="{@context}" priority="{4000 - count(preceding-sibling::*)}" mode="M{count(../preceding-sibling::*)}"><xsl:call-template name="process-rule"><xsl:with-param name="id" select="@id"/><xsl:with-param name="context" select="@context"/><xsl:with-param name="role" select="@role"/></xsl:call-template><xsl:apply-templates/><axsl:apply-templates mode="M{count(../preceding-sibling::*)}"/></axsl:template></xsl:template><xsl:template match="sch:rule[@abstract='."'".'true'."'".'] | rule[@abstract='."'".'true'."'".']" ><xsl:if test=" not(@id)"><xsl:message>Markup Error: no id attribute on abstract <rule></xsl:message></xsl:if> <xsl:if test="@context"><xsl:message>Markup Error: (2) context attribute on abstract <rule></xsl:message></xsl:if></xsl:template><xsl:template match="sch:rule[@abstract='."'".'true'."'".'] | rule[@abstract='."'".'true'."'".']" mode="extends" ><xsl:if test="@context"><xsl:message>Markup Error: context attribute on abstract <rule></xsl:message></xsl:if><xsl:apply-templates/></xsl:template><xsl:template match="sch:span | span" mode="text"><xsl:call-template name="process-span" ><xsl:with-param name="class" select="@class"/></xsl:call-template></xsl:template><xsl:template match="sch:title | title" /> <xsl:template match="sch:value-of | value-of" mode="text" ><xsl:if test="not(@select)"><xsl:message>Markup Error: no select attribute in <value-of></xsl:message></xsl:if><xsl:call-template name="IamEmpty" /><axsl:text xml:space="preserve"></axsl:text><xsl:choose><xsl:when test="@select" ><xsl:call-template name="process-value-of"><xsl:with-param name="select" select="@select"/></xsl:call-template></xsl:when><xsl:otherwise ><xsl:call-template name="process-value-of"><xsl:with-param name="select" select="'."'".'.'."'".'"/></xsl:call-template></xsl:otherwise></xsl:choose><axsl:text xml:space="preserve"></axsl:text></xsl:template><xsl:template match="text()" priority="-1" mode="do-keys"></xsl:template><xsl:template match="text()" priority="-1" mode="do-all-patterns"></xsl:template><xsl:template match="text()" priority="-1" mode="do-schema-p"></xsl:template><xsl:template match="text()" priority="-1" mode="do-pattern-p"></xsl:template><xsl:template match="text()" priority="-1"></xsl:template><xsl:template match="text()" mode="text"><xsl:value-of select="normalize-space(.)"/></xsl:template><xsl:template match="text()" mode="inline-text"><xsl:value-of select="."/></xsl:template><xsl:template name="IamEmpty"><xsl:if test="count( * )"><xsl:message><xsl:text>Warning: </xsl:text><xsl:value-of select="name(.)"/><xsl:text> must not contain any child elements</xsl:text></xsl:message></xsl:if></xsl:template><xsl:template name="diagnosticsSplit"><!-- Process at the current point the first of the <diagnostic> elements referred to parameter str, and then recurse --><xsl:param name="str"/><xsl:variable name="start"><xsl:choose><xsl:when test="contains(str,'."'".' '."'".')"/></xsl:when><xsl:otherwise><xsl:value-of select="str,'."'".' '."'".')"><xsl:value-of select="substring-after(start)) = 0) and not(//sch:diagnostic[@id = (start)])"><xsl:message>Reference error: A diagnostic "<xsl:value-of select="string(start)) > 0"><xsl:apply-templates select="//sch:diagnostic[@id = (start) ]"/></xsl:if><xsl:if test="not(end"/></xsl:call-template></xsl:if></xsl:template><xsl:template match="*"><xsl:message><xsl:text>Warning: unrecognized element </xsl:text><xsl:value-of select="name(.)"/></xsl:message></xsl:template><xsl:template match="*" mode="text"><xsl:message><xsl:text>Warning: unrecognized element </xsl:text><xsl:value-of select="name(.)"/></xsl:message></xsl:template><xsl:template name="process-prolog"/><xsl:template name="process-root"><xsl:param name="contents"/><xsl:copy-of select="test"/><xsl:with-param name="role" select="test"/><xsl:with-param name="role" select="name}"/></xsl:template><xsl:template name="process-ns" /><xsl:template name="process-p"/><xsl:template name="process-pattern"/><xsl:template name="process-rule"/><xsl:template name="process-span" ><xsl:apply-templates mode="inline-test"/></xsl:template><xsl:template name="process-value-of"><xsl:param name="select" /><axsl:value-of select="{
}
function compile_schematron_from_file(xml_filename) {
global xslt=new Xslt();
xml_filename);
this->sk15);
if(ret=this->compiled[ret;
} else {
trigger_error("XSLT error compiling xslt->getError(),E_USER_WARNING);
return false;
}
return xml_string) {
name=md5(xslt->setXmlString(xslt->setXslString(xslt->transform()) {
xslt->getOutput();
name]=ret;
} else {
trigger_error("XSLT error compiling xml-string".xslt->destroy;
}
function get_compiled(this->compiled[this->compiled[xml_filename,this->compiled[fp=fopen(fp) {
trigger_error("Cannot save compiled schematron to fp,xml_filename]);
fclose(xml_string,name=md5(this->compiled[this->compile_schematron_from_mem(xslt=new Xslt();
xml_string);
this->compiled[xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
xml_string,this->compiled[this->compile_schematron_from_file(xslt=new Xslt();
xml_string);
this->compiled[xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
xml_filename,name=md5(this->compiled[this->compile_schematron_from_mem(xslt=new Xslt();
xml_filename);
this->compiled[xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
xml_filename,this->compiled[this->compile_schematron_from_file(xslt=new Xslt();
xml_filename);
this->compiled[xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
xml_string,xslt=new Xslt();
xml_string);
validation_string);
if(ret=ret;
} else {
trigger_error("XSLT error running schematron".xslt->destroy();
}
function schematron_validate_mem_using_compiled_file(validation_filename) {
xslt->setXmlString(xslt->setXsl(xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
xml_filename,xslt=new Xslt();
xml_filename);
validation_string);
if(ret=ret;
} else {
trigger_error("XSLT error running schematron".xslt->destroy();
}
function schematron_validate_file_using_compiled_file(validation_filename) {
xslt->setXml(xslt->setXsl(xslt->transform()) {
xslt->getOutput();
return xslt->getError(),E_USER_WARNING);
return false;
}
// Title : class_xslt.php
// Version : 1.1
// Author : Luis Argerich (lrargerich@yahoo.com)
// Last modification date : 03-11-2002
// Description : An abstraction class for the XSLT extension
// this one uses the Sablotron processor but we
// may release classes based on other processors
// later.
// # # # # # # # # # # # # # # # # # # # ###
// History:
// 03-11-2001 Class modified to work with the new XSLT extension
// 11-11-2001 Class created
// # # # # # # # # # # # # # # # # # # # ###
// To-Dos:
// # # # # # # # # # # # # # # # # # # # ###
// How to use it:
// include_once("class_xslt.php");
// xslt=new Xslt();
// xml)
// xsl)
// if(ret=ret;
// } else {
// print("Error:".xsl, output, this->processor = xslt_create();
}
/* Destructor */
function destroy() {
xslt_free(string) {
string;
}
function getOutput() {
return xml) {
xml;
return true;
}
function setXslString(this->xsl=uri) {
if(uri)) {
doc->getString();
return true;
} else {
xml");
return false;
}
}
function setXsl(doc = new docReader(this->xsl = this->setError("Could not open arguments = array(
'/_xml' => this->xsl
);
this->processor, 'arg:/_xml', 'arg:/_xsl', NULL, ret) {
this->processor));
return false;
} else {
ret);
return true;
}
}
/* Error Handling */
function setError(this->error = this->error;
}
}
/* docReader -- read a file or URL as a string */
/* test */
/*
docUri->getString();
*/
class docReader {
var type; // private URI type: 'file','url'
var uri;
/* public constructor */
function docReader(this->setUri(this->uri=this->setType();
this->getUri(),"r");
if(this->getType() == 'file') {
this->getUri());
} else {
this->bignum;
}
fp,uri) { // returns boolean
if (strstr(uri) {
return false;
} else {
return true;
}
}
/* set and get methods */
function setUri(this->uri = this->uri;
}
function setString(this->string = this->string;
}
function setType() {
if (this->uri)) {
this->type = 'url';
}
}
function getType() {
return
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.