Sorry all - wasn't too sure if anyone was actually listening that's all. I will proberly get butchered for this, but here is a development version of what I have so far.
First, the xslt file. Understand my knowledge of xslt is limited - I'm still learning this one:
PHP Code:
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method='html' cdata-section-elements="cdata" version='1.0' encoding='utf-8' indent='yes' />
<!-- string formatting -->
<xsl:template match='br'>
<br />
<xsl:apply-templates />
</xsl:template>
<xsl:template match='bold'>
<b>
<xsl:value-of select='.' /></b>
</xsl:template>
<xsl:template match='italic'>
<i>
<xsl:value-of select='.' /></i>
</xsl:template>
<xsl:template match='underline'>
<u>
<xsl:value-of select='.' /></u>
</xsl:template>
<xsl:template match='para'>
<p>
<xsl:value-of select='.' /></p>
</xsl:template>
<!-- FORMs -->
<xsl:template match='form'>
<form>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='method'><xsl:value-of select='@method' /></xsl:attribute>
<xsl:attribute name='action'><xsl:value-of select='@action' /></xsl:attribute>
<xsl:apply-templates />
</form>
</xsl:template>
<xsl:template match='text'>
<input type='text'>
<xsl:attribute name='value'>
<xsl:value-of select='.' />
</xsl:attribute>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='size'><xsl:value-of select='@size' /></xsl:attribute>
<xsl:attribute name='maxlength'><xsl:value-of select='@maxlength' /></xsl:attribute>
</input>
</xsl:template>
<xsl:template match='password'>
<input type='password'>
<xsl:attribute name='value'>
<xsl:value-of select='.' /></xsl:attribute>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='size'><xsl:value-of select='@size' /></xsl:attribute>
<xsl:attribute name='maxlength'><xsl:value-of select='@maxlength' /></xsl:attribute>
</input>
</xsl:template>
<xsl:template match='hidden'>
<input type='hidden'>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='value'><xsl:value-of select='@value' /></xsl:attribute>
</input>
</xsl:template>
<xsl:template match='submit'>
<input type='submit'>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='value'><xsl:value-of select='@value' /></xsl:attribute>
</input>
</xsl:template>
<xsl:template match='select'>
<select>
<xsl:attribute name='size'><xsl:value-of select='@size' /></xsl:attribute>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:apply-templates /></select>
</xsl:template>
<xsl:template match='option'>
<option>
<xsl:attribute name='value'><xsl:value-of select='@value' /></xsl:attribute>
<xsl:apply-templates /></option>
</xsl:template>
<xsl:template match='textarea'>
<textarea>
<xsl:attribute name='name'><xsl:value-of select='@name' /></xsl:attribute>
<xsl:attribute name='cols'><xsl:value-of select='@cols' /></xsl:attribute>
<xsl:attribute name='rows'><xsl:value-of select='@rows' /></xsl:attribute>
<xsl:apply-templates /></textarea>
</xsl:template>
<!-- ANCHORs -->
<xsl:template match = "link">
<a>
<xsl:attribute name="href">
<xsl:value-of select = "@href" />
</xsl:attribute>
<xsl:value-of select="." />
</a>
</xsl:template>
<!-- BUTTONs -->
<xsl:template match='button-row'>
<tr>
<td width='100%' align='center' height='48' valign='center'>
<xsl:apply-templates />
</td></tr>
</xsl:template>
<xsl:template match='cancel-button'>
<button onClick='window.location="index.php";'>
<xsl:attribute name='onclick'><xsl:value-of select='@onclick' /></xsl:attribute>
<xsl:value-of select='.' /></button>
</xsl:template>
<xsl:template match='go-button'>
<button>
<xsl:attribute name='onClick'><xsl:value-of select='@onClick' /></xsl:attribute>
<xsl:value-of select='.' /></button>
</xsl:template>
<!-- TABLE cell outputs -->
<xsl:template match='table'>
<tr>
<td width='100%' valign='top'>
<table border='0' align='center' cellspacing='1' cellpadding='0'>
<xsl:attribute name='width'><xsl:value-of select='@width' /></xsl:attribute>
<xsl:attribute name='bgcolor'><xsl:value-of select='@bgcolor' /></xsl:attribute>
<tbody>
<xsl:apply-templates />
</tbody></table>
</td></tr>
</xsl:template>
<xsl:template match='alt-table'>
<tr>
<td width='100%' valign='top'>
<table border='0' style='border:solid 1px black;' width='512' align='center' cellspacing='1' cellpadding='0'>
<xsl:attribute name='width'><xsl:value-of select='@width' /></xsl:attribute>
<xsl:attribute name='bgcolor'><xsl:value-of select='@bgcolor' /></xsl:attribute>
<tbody>
<xsl:apply-templates />
</tbody></table>
</td></tr>
</xsl:template>
<xsl:template match='row'>
<tr>
<xsl:for-each select='cell'>
<td>
<xsl:attribute name='align'><xsl:value-of select='@align' /></xsl:attribute>
<xsl:attribute name='valign'><xsl:value-of select='@valign' /></xsl:attribute>
<xsl:attribute name='width'><xsl:value-of select='@width' /></xsl:attribute>
<xsl:attribute name='height'><xsl:value-of select='@height' /></xsl:attribute>
<xsl:attribute name='bgcolor'><xsl:value-of select='@bgcolor' /></xsl:attribute>
<xsl:apply-templates />
</td>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match='title'>
<tr>
<td width='100%' align='center' height='24' valign='top'>
<xsl:apply-templates /></td></tr>
</xsl:template>
<xsl:template match='message'>
<tr>
<td width='100%' align='center' height='16' valign='center'>
<xsl:attribute name='colspan'><xsl:value-of select='@colspan' /></xsl:attribute>
<xsl:apply-templates /></td></tr>
</xsl:template>
<xsl:template match='message-block'>
<tr>
<td width='100%' align='center' height='32' valign='center'>
<xsl:apply-templates /></td></tr>
</xsl:template>
<xsl:template match='spacer'>
<tr>
<td width='100%' align='center' height='16' valign='center'>
<xsl:attribute name='colspan'><xsl:value-of select='@colspan' /></xsl:attribute>
</td></tr>
</xsl:template>
<xsl:template match='content'>
<tr>
<td width='100%' align='center' height='16' valign='center'>
<xsl:apply-templates /></td></tr>
</xsl:template>
</xsl:stylesheet>
And now some example xml files I use:
PHP Code:
<root>
<title>
<bold>Manage Employee's</bold>
</title>
<message>You may use the Quick Search facility below to manage an employee.</message>
<spacer />
<content>
[ <link href='index.php?Do=UserSearch&chr=A'>A</link> |
<link href='index.php?Do=UserSearch&chr=B'>B</link> |
<link href='index.php?Do=UserSearch&chr=C'>C</link> |
<link href='index.php?Do=UserSearch&chr=D'>D</link> |
<link href='index.php?Do=UserSearch&chr=E'>E</link> |
<link href='index.php?Do=UserSearch&chr=F'>F</link> |
<link href='index.php?Do=UserSearch&chr=G'>G</link> |
<link href='index.php?Do=UserSearch&chr=H'>H</link> |
<link href='index.php?Do=UserSearch&chr=I'>I</link> |
<link href='index.php?Do=UserSearch&chr=J'>J</link> |
<link href='index.php?Do=UserSearch&chr=K'>K</link> |
<link href='index.php?Do=UserSearch&chr=L'>L</link> |
<link href='index.php?Do=UserSearch&chr=M'>M</link> |
<link href='index.php?Do=UserSearch&chr=N'>N</link> |
<link href='index.php?Do=UserSearch&chr=O'>O</link> |
<link href='index.php?Do=UserSearch&chr=P'>P</link> |
<link href='index.php?Do=UserSearch&chr=Q'>Q</link> |
<link href='index.php?Do=UserSearch&chr=R'>R</link> |
<link href='index.php?Do=UserSearch&chr=S'>S</link> |
<link href='index.php?Do=UserSearch&chr=T'>T</link> |
<link href='index.php?Do=UserSearch&chr=U'>U</link> |
<link href='index.php?Do=UserSearch&chr=V'>V</link> |
<link href='index.php?Do=UserSearch&chr=W'>W</link> |
<link href='index.php?Do=UserSearch&chr=X'>X</link> |
<link href='index.php?Do=UserSearch&chr=Y'>Y</link> |
<link href='index.php?Do=UserSearch&chr=Z'>Z</link> ]
</content>
<button-row>
<go-button onClick='window.location="index.php?Do=AddUser";'>New Employee</go-button>
</button-row>
<table width='448' bgcolor='skyblue'>
<import />
</table>
<spacer />
</root>
And now for the classes:
PHP Code:
class db_connect extends file_handle
{
/*
class properties
*/
var $hostname;
var $username;
var $password;
var $srce;
var $numb;
var $q;
/*
class constructor
*/
function db_connect($srce = "", $hostname = "", $username = "", $password = "")
{
$this -> host = $hostname; /* database server hostname */
$this -> user = $username; /* database server username */
$this -> pass = $password; /* database server password */
$this -> srce = $srce; /* (database) query source */
}
/*
class function
query database table passed to class function
*/
function query_user_db($query)
{
$conn = @ mysql_connect($this -> host, $this -> user, $this -> pass);
$stmt = @ mysql_select_db($this -> srce, $conn);
if($conn && $stmt)
{
/* no given errors from database connection */
$q = @ mysql_query($query, $conn);
$this -> numb = @ mysql_num_rows($q);
/** added 04 01 03 **/
$this -> fields = @ mysql_num_fields($q);
$this -> q = $q;
return $q;
}
else
{
/* something went wrong with database connection */
if(LOG_DB_ERRORS)
{
?>
<div class="smaller">** Database Error **</div>
<?
$str = date("Y-m-d H:i:s")." - [ Database Error on file: ".$_SERVER["REDIRECT_URL"]." ]\r\n";
$this -> append_txt2_file("error-log.txt", $str);
exit();
}
}
}
/**
* class member function
* public
*
* set database source
*/
function set_db_source($source)
{
$this -> srce = $source;
}
/*
class function
return number of records found from last known database query
*/
function total_rows_db()
{
return $this -> numb;
}
/*
class functon added 04 01 03
return number of fields given by query string
*/
function total_fields_db()
{
return $this -> fields;
}
/*
class function added 04 01 03
return an 2 dimensional array of db query
array[rec][field]
array[rec][field]...
*/
function dump_user_db($query = "")
{
/**
* can also carry out the database query from here
* if needs be
*/
if(isset($query) && $query != (string) "")
{
$this -> query_user_db($query);
}
$count = 0;
while($r = mysql_fetch_row($this -> q))
{
$limit = $this -> fields;
for($a = 0;$a < $limit;$a++)
{
$array[$count][$a] = $r[$a];
}
$count++;
}
return $array;
}
} /* end of class db_connect */
/**
* php class number 04
*
* maintain file read, write, delete
*
*
* example of use:
*
*/
class file_handle
{
/*
class properties
*/
var $fp;
var $filename;
/*
class function
append a character string to specified text file
*/
function append_txt2_file($filename, $string)
{
if($fp = fopen($filename, "a+"))
{
fputs($fp, $string);
fclose($fp);
}
}
} /* end of class file_handle */
class DocReader
{
var $string;
var $type;
var $bignum = 1000000;
var $uri;
function DocReader($uri)
{
$this -> SetUri($uri);
$this -> uri = $uri;
$this -> SetType();
$fp = fopen($this -> GetUri(), "r");
if($fp)
{
if($this -> GetType() == 'file')
{
$length = filesize($this -> GetUri());
}
else
{
$length = $this -> bignum;
}
$this -> SetString(fread($fp, $length));
return 1;
}
else
{
return 0;
}
}
function IsFile($uri)
{
if(strstr($uri, 'http://') == $uri)
{
return false;
}
else
{
return true;
}
}
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;
}
}
class Xslt
{
var $xsl;
var $xml;
var $error;
var $output;
function Xslt()
{
$this -> processor = xslt_create();
}
function Destroy()
{
xslt_free($this -> processor);
}
function SetOutput($string)
{
$this -> output = $string;
}
function GetOutput()
{
return $this -> output;
}
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;
}
}
/**
* additional class method made by les quinn 11 feb 2003
*/
function SetXmlString($uri)
{
if(is_string($uri))
{
$this -> xml = $uri;
return true;
}
else
{
/**
* unknown type
*/
$this -> SetError("Unknown data string in $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;
}
}
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;
}
}
function SetError($string)
{
$this -> error = $string;
}
function GetError()
{
return $this -> error;
}
} /* end of class Xslt */
/**
* php class number 07
*/
class transform_xml /** added 18.02.03 by les quinn version 0.35 **/
{
/**
* class Xslt (and DocReader) from an [url]www.webreference.com[/url] article
* unknown author(s) or source
*/
/**
* class properties
*/
var $temp;
var $xslt_;
var $xml_dir;
var $xml_file;
var $xsl_file;
/**
* class constructor
*/
function transform_xml()
{
$this -> xslt_ = new Xslt;
}
/**
* class method
* set directory path for xml/xsl files
*/
function set_xml_dir($path)
{
$this -> xml_dir = (string) $path;
}
/**
* get directory path for xml/xsl files
*/
function get_xml_dir()
{
return $this -> xml_dir;
}
/**
* class method
* set either xml file or xml string
*/
function set_xml_file($file)
{
$this -> xml_file = (string) $file;
}
/**
* class method
* set xsl file
*/
function set_xsl_file($file)
{
$this -> xsl_file = (string) $file;
}
/**
* class method
* get xml file or xml string
*/
function get_xml_file()
{
return $this -> xml_file;
}
/**
* class method
* clear string buffer
*/
function set_xml_buffer()
{
$this -> temp = '';
}
/**
* class method
* return string buffer
*/
function get_xml_buffer()
{
return $this -> temp;
}
/**
* class method
* add an xml tag and content to buffer w/out attributes
*/
function add_xmltag2_buffer($tag, $fragment, $attr = '')
{
/**
* begin to build xml tag
*/
$tmp = '';
/**
* check first to see if any attributes available for this tag
*/
if(is_array($attr))
{
/**
* we have one or more attributes to add to tag
*/
foreach($attr as $key => $value)
{
/**
* for every attribute found...
*/
$tmp .= " $key='$value'";
}
}
$tmp = "<$tag". $tmp .">$fragment</$tag>";
$this -> temp .= $tmp;
}
/**
* class method
* enclose an xml string within a pair of specified xml well formed tags
*/
function xml_enclose_tags($tags, $fragment)
{
$str = '<'. $tags .'>'. $fragment;
$str .= '</'. $tags .'>';
/**
* would need to reference this string from object reference, i.e.
*
* $obj -> append_text2_xml_string('<tab-import-1 />', $obj -> xml_enclose_tags('tab-row', $xmlFragment));
*
* where $xmlFragment is the dynamic content embedded within xml tags to use
*/
return $str;
}
/**
* class method
* transform a well formed xml file
*/
function make_xml_document()
{
$this -> xslt_ -> SetXml($this -> xml_dir . $this -> xml_file);
$this -> xslt_ -> SetXsl($this -> xml_dir . $this -> xsl_file);
if($this -> xslt_ -> Transform())
{
/**
* echo transformed xml to browser
*/
echo($this -> xslt_ -> GetOutput());
}
else
{
/**
* there has been a problem transforming xml file/string
*/
echo("Error:". $this -> xslt_ -> GetError());
}
}
/**
* class method
* transform a well formed xml string
*/
function make_xml_string()
{
$this -> xslt_ -> SetXmlString($this -> xml_file);
$this -> xslt_ -> SetXsl($this -> xml_dir . $this -> xsl_file);
if($this -> xslt_ -> Transform())
{
/**
* echo transformed xml to browser
*/
echo($this -> xslt_ -> GetOutput());
}
else
{
/**
* there has been a problem transforming xml file/string
*/
echo("Error:". $this -> xslt_ -> GetError());
}
}
/**
* class method
* replace a specified tag with dynamic content from php function
*/
function append_function2_xml_file($tag2parse, $function2execute)
{
$fp = file($this -> xml_dir . $this -> xml_file, 'r');
if($fp)
{
$xml_txt = '';
while(list($num, $line) = each($fp))
{
trim($line);
/**
* replace $tag with dynamic content generated by function
*/
$xml_txt .= ereg_replace($tag2parse, $function2execute($this) /* pass on object ref. */, $line);
}
}
/**
* now xml file is retained as an xml string
*/
$this -> xml_file = $xml_txt;
}
/**
* class method
* continue to replace a specific tag with dynamic content from php string
*/
function append_function2_xml_string($tag2parse, $function2execute)
{
$this -> xml_file = ereg_replace($tag2parse, $function2execute(), $this -> xml_file);
}
/**
* class method
* replace a specified tag with dynamic content from a string
*/
function append_text2_xml_file($tag2parse, $text)
{
$fp = file($this -> xml_dir . $this -> xml_file, 'r');
if($fp)
{
$xml_txt = '';
while(list($num, $line) = each($fp))
{
trim($line);
/**
* replace $tag with content from $text
*/
$xml_txt .= ereg_replace($tag2parse, $text, $line);
}
}
/**
* now xml file is retained as an xml string
*/
$this -> xml_file = $xml_txt;
}
/**
* class method
* continue to replace a specific tag with dynamic content from a string
*/
function append_text2_xml_string($tag2parse, $text)
{
$this -> xml_file = ereg_replace($tag2parse, $text, $this -> xml_file);
}
} /* end of class transform_xml */
Pheww... still with me ?
Now some PHP to transform that xml file:
PHP Code:
function DoExecuteApp($menu, $command, $executable)
{
/**
* create a new instance of transformation class
*/
$trans = new transform_xml;
$XML_DIR = '../apps/'. $_SESSION["OffManager"]["MenuState"] .'/templates/';
/**
* set xml directory and xsl stylesheet for this application
*/
$trans -> set_xml_dir($XML_DIR);
$trans -> set_xsl_file('admin-.xsl');
/**
* begin TABLE structure
*/
BeginTable('604', 'top', 'center');
if(strtolower($command) == (string) "home" || empty($command))
{
if(strtolower($executable) == (string) "usersearch") { UserSearch($trans); }
else if(strtolower($executable) == (string) "adduser") { AddUser($trans); }
else if(strtolower($executable) == (string) "doadduser") { DoAddUser($trans); }
else
{
$trans -> set_xml_file('admin-home.xml');
$trans -> append_function2_xml_file('<import />', 'ShowNumberMessages');
$trans -> make_xml_string();
}
}
/**
* close TABLE structure
*/
EndTable();
}
function ShowNumberMessages($obj = '')
{
/**
* functionality for search number of private messages this user has
*/
return $obj -> xml_enclose_tags('message-block', 'You have <bold>0</bold> new messages today.');
}
Enjoy -
Bookmarks