schematron_validate_file_using_file("sample1.xml","validation_sample1.xml"); // ################################################################################## // 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 $sk15; function Schematron() { $this->sk15='#ALL key Warning: must not contain any child elements /@ [ ] Markup Error: no pattern attribute in <active>Reference Error: the pattern "" has been activated but is not declaredMarkup Error: no test attribute in <assert>Markup Error: no test attribute in <report> Markup Error: no id attribute in <diagnostic>Markup Error: no rule attribute in <extends>Reference Error: the abstract rule "" has been referenced but is not declaredMarkup Error: no name attribute in <key>Markup Error: no match attribute on <key> outside <rule>Markup Error: no path attribute in <key> Markup Error: no uri attribute in <ns>Markup Error: no prefix attribute in <ns>Markup Error: no id attribute in <phase>Markup Error: no context attribute in <rule>Markup Error: no id attribute on abstract <rule> Markup Error: (2) context attribute on abstract <rule>Markup Error: context attribute on abstract <rule> Markup Error: no select attribute in <value-of>Warning: must not contain any child elementsReference error: A diagnostic "" has been referenced but is not declaredWarning: unrecognized element Warning: unrecognized element '; } function compile_schematron_from_file($xml_filename) { global $xslt_schematron; $xslt=new Xslt(); $xslt->setXml($xml_filename); $xslt->setXslString($this->sk15); if($xslt->transform()) { $ret=$xslt->getOutput(); $this->compiled[$xml_filename]=$ret; } else { trigger_error("XSLT error compiling $xml_filename".$xslt->getError(),E_USER_WARNING); return false; } return $ret; } function compile_schematron_from_mem($xml_string) { $xslt=new Xslt(); $name=md5($xml_string); $xslt->setXmlString($xml_string); $xslt->setXslString($this->sk15); if($xslt->transform()) { $ret=$xslt->getOutput(); $this->compiled[$name]=$ret; return $ret; } else { trigger_error("XSLT error compiling xml-string".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy; } function get_compiled($xml_filename) { if(isset($this->compiled[$xml_filename])) { return $this->compiled[$xml_filename]; } else { return false; } } function save_compiled($xml_filename,$filename) { if(isset($this->compiled[$xml_filename])) { $fp=fopen($filename,"w"); if(!$fp) { trigger_error("Cannot save compiled schematron to $filename may be a permissions problem",E_USER_WARNING); } fwrite($fp,$this->compiled[$xml_filename]); fclose($fp); return true; } else { trigger_error("trying to save a schematron that was not compiled first",E_USER_WARNING); return false; } } /* API FOR UNCOMPILED SCRIPTS */ function schematron_validate_mem_using_mem($xml_string,$validation_string) { $name=md5($validation_string); if(!isset($this->compiled[$name])) { $this->compile_schematron_from_mem($validation_string); } $xslt=new Xslt(); $xslt->setXmlString($xml_string); $xslt->setXslString($this->compiled[$name]); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_mem_using_file($xml_string,$validation_filename) { if(!isset($this->compiled[$validation_filename])) { $this->compile_schematron_from_file($validation_filename); } $xslt=new Xslt(); $xslt->setXmlString($xml_string); $xslt->setXslString($this->compiled[$validation_filename]); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_file_using_mem($xml_filename,$validation_string) { $name=md5($validation_string); if(!isset($this->compiled[$nameing])) { $this->compile_schematron_from_mem($validation_string); } $xslt=new Xslt(); $xslt->setXml($xml_filename); $xslt->setXslString($this->compiled[$name]); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_file_using_file($xml_filename,$validation_filename) { if(!isset($this->compiled[$validation_filename])) { $this->compile_schematron_from_file($validation_filename); } $xslt=new Xslt(); $xslt->setXml($xml_filename); $xslt->setXslString($this->compiled[$validation_filename]); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } /* API FOR COMPILED VALIDATION SCRIPTS */ function schematron_validate_mem_using_compiled_mem($xml_string,$validation_string) { $xslt=new Xslt(); $xslt->setXmlString($xml_string); $xslt->setXslString($validation_string); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_mem_using_compiled_file($xml_string,$validation_filename) { $xslt=new Xslt(); $xslt->setXmlString($xml_string); $xslt->setXsl($validation_filename); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_file_using_compiled_mem($xml_filename,$validation_string) { $xslt=new Xslt(); $xslt->setXml($xml_filename); $xslt->setXslString($validation_string); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } function schematron_validate_file_using_compiled_file($xml_filename,$validation_filename) { $xslt=new Xslt(); $xslt->setXml($xml_filename); $xslt->setXsl($validation_filename); if($xslt->transform()) { $ret=$xslt->getOutput(); return $ret; } else { trigger_error("XSLT error running schematron".$xslt->getError(),E_USER_WARNING); return false; } $xslt->destroy(); } } } ?> setXml("applications.xml"); // or setXmlString($xml) // $xslt->setXsl("tr1.xsl"); // or setXslString($xsl) // if($xslt->transform()) { // $ret=$xslt->getOutput(); // echo $ret; // } else { // print("Error:".$xslt->getError()); // } // ################################################################################## // CHECK FOR DOUBLE DEFINITION HERE if(defined("_class_xslt_is_included")) { // do nothing since the class is already included } else { define("_class_xslt_is_included",1); class Xslt { var $xsl, $xml, $output, $error ; /* Constructor */ function xslt() { $this->processor = xslt_create(); } /* Destructor */ function destroy() { xslt_free($this->processor); } /* output methods */ function setOutput($string) { $this->output = $string; } function getOutput() { return $this->output; } /* set methods */ function setXmlString($xml) { $this->xml=$xml; return true; } function setXslString($xsl) { $this->xsl=$xsl; return true; } function setXml($uri) { if($doc = new docReader($uri)) { $this->xml = $doc->getString(); return true; } else { $this->setError("Could not open $xml"); return false; } } function setXsl($uri) { if($doc = new docReader($uri)) { $this->xsl = $doc->getString(); return true; } else { $this->setError("Could not open $uri"); return false; } } /* transform method */ function transform() { $arguments = array( '/_xml' => $this->xml, '/_xsl' => $this->xsl ); $ret = xslt_process($this->processor, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments); if(!$ret) { $this->setError(xslt_error($this->processor)); return false; } else { $this->setOutput($ret); return true; } } /* Error Handling */ function setError($string) { $this->error = $string; } function getError() { return $this->error; } } /* docReader -- read a file or URL as a string */ /* test */ /* $docUri = new docReader('http://www.someurl.com/doc.html'); echo $docUri->getString(); */ class docReader { var $string; // public string representation of file var $type; // private URI type: 'file','url' var $bignum = 1000000; var $uri; /* public constructor */ function docReader($uri) { // returns integer $this->setUri($uri); $this->uri=$uri; $this->setType(); $fp = fopen($this->getUri(),"r"); if($fp) { // get length if ($this->getType() == 'file') { $length = filesize($this->getUri()); } else { $length = $this->bignum; } $this->setString(fread($fp,$length)); return 1; } else { return 0; } } /* determine if a URI is a filename or URL */ function isFile($uri) { // returns boolean if (strstr($uri,'http://') == $uri) { return false; } else { return true; } } /* set and get methods */ function setUri($string) { $this->uri = $string; } function getUri() { return $this->uri; } function setString($string) { $this->string = $string; } function getString() { return $this->string; } function setType() { if ($this->isFile($this->uri)) { $this->type = 'file'; } else { $this->type = 'url'; } } function getType() { return $this->type; } } }