PHP question.
May 26, 2004 6:03 AM
Subscribe
Ok, another PHP question... this one less stupid than my last one (hopefully). XML/XSLT.... [mi]
I have some generated XML (from a php script that builds from a directory and contents)... I used and modified some code to do a transform:
< ?phpbr>
/**
*
* A class to transform XML through XSLT using PHP4 Sablotron extension
*
*/
#######################################################################
## file extended by Daniel Unterberger (d.u@mail.com)
## changes: some php:eval's added
class xsltTransform
{
var $xsl_file;
var $xml_file;
var $fileName;
/**
* Constructor to the xsl_transform ticket
*
* @param $xsl_file The XSLT file containing the transformation info
* @param $xml_file The XML file containing the actual data
* @see readFile()
*/
function xsltTransform($xsl_file = '', $xml_file = '')
{
if($xsl_file{0} == "/") # this is a relative path, so use the file
{
$this->xsl_string = "file://".$_SERVER['DOCUMENT_ROOT'].$xsl_file;
}
else
{
$this->xsl_string = $this->readFile($xsl_file,'php:eval');
}
if($xml_file{0} == "/") # this is a relative path, so use the file
{
$this->xml_string = "file://".$_SERVER['DOCUMENT_ROOT'].$xml_file;
}
else
{
$this->xml_string = $this->readFile($xml_file);
}
}
/**
* Function to read through the file
*
* @param $fileName Which file to read?
*
*/
function readFile($fileName,$php_eval="")
{
// get contents of a file into a string
$fd = fopen( $fileName, "r" );
while (!feof ($fd)) {
$content .= fgets($fd, 4096); #?rtrim
}
fclose( $fd );
##################################################
## extension by Daniel Unterberger (d.u@mail.com)
while ( $php_eval and ( $pos_start=strpos($content,'') ))
{
$pos_end=strpos($content,'');
$content=substr($content,0,$pos_start).
eval( substr($content,$pos_start+10,$pos_end-$pos_start-10) ).
substr($content,$pos_end+11);
}
if ($GLOBALS["debug"]=="ON") print "$content";
## see xslt-file for debuggin if you call page.php?debug=ON (remove on life-server)
###################################################
## end extension
return $content;
}
/**
* Function to apply the actual transformation
*
*/
function applyTransformation()
{
$xh = xslt_create();
# $this->result = '';
$this->result = xslt_process($xh, $this->xml_string, $this->xsl_string);
if(!$this->result)
{
print ("Transformation failed; Error String:".xslt_error($xh)."; Err #:".xslt_errno($xh)."
");
print ("XSL:-".$this->xsl_string."-
");
print ("XML:-".$this->xml_string."-
");
}
xslt_free($xh);
return $this->result;
}
// End of class, do not remove
}
?>
>
now the if I generate the XML and save it to the file system, then give the function xsltTransform the actual file path the transform work fine. However, if I read it from the webserver using http:// (where it was generated from) I can print it to the screen.
However, but the call to $this->result = xslt_process($xh, $this->xml_string, $this->xsl_string); in applyTransformation fails indicating that the XML is badly formed (error 4). Any advice from our PHP gurus?
posted by pissfactory to computers & internet (1 comment total)
What's your input to each function? Can you make reproduce it with a simpler example?
posted by holloway at 3:05 PM on May 26, 2004