XSLT
<?php require_once "DB.php"; //PEAR中的数据库处理类 $dataType = "mysql" ; //数据库类型 $user = "root"; //用户名 $pass = "abcd" ; //密码 $host="202.96.215.200"; //Mysql数据库服务器地址 $db_name = "test"; //数据库名 $dsn="$dataType://$user:$pass@$host/$db_name"; //连接数据库的DNS配制 $db = DB::connect($dsn); //连接数据库 if (DB::isError($db)) { die ($db->getMessage()); //连接失败,输出出错信息 } //下面二个是公共的函数 /** * 读取xsl文档 * * @param String $filename - xsl文件的名称 * @return string */ function readXsl($filename) { if(false==file_exists($filename)) { echo "要读取的文件<font color='red'>$filename</font>不存在</br />"; return false ; } return implode('', file($filename)); } //end function readXsl
/** * 将xml文件或数组变量根据xsl文件转换成HTML内容 * http://knowsky.com * @param array $arydata - 数组变量 * @param String $xslstring - xsl文档数据 * @param String $xmlstring - xml文档数据 */ function getHtml($arydata = false, $xslstring = false, $xmlstring = false) { global $db ; //使用刚才的$db对象 include_once("XML/sql2xml.php"); //把sql2xml包含进来 $sql2xmlclass = new xml_sql2xml($db); //将sql2xml实例化 $sql2xmlclass->setEncoding("GB2312"); //设置数据的转码类型 if (false == $xmlstring) { // 如果用户传入数组数据,则应用该数组数据到xsl //设置生成XML文档数据的节点名称 $options = array ( tagNameRow => "row" , tagNameResult => "result" ); $sql2xmlclass->SetOptions($options); //添加要生成XML文档的数据 $sql2xmlclass->add($arydata); } //得到xml文档 $xmlstring = $sql2xmlclass->getxml(); //print $xmlstring; //下面开始将XML数据文档用XSLT转换成HTML文档 $arguments = array('/_xml' => $xmlstring, '/_xsl' => $xslstring ); $xh = xslt_create();
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', null, $arguments);
if ($result) { return $result; xslt_free($xh); } else { return "转换xml数据到xsl时出错"; xslt_free($xh); } } //end function getHtml()
//从用户信息表中查询数据的SQL语句 $sql = "select nsrnm, #代码 qymc, #企业名称 qydh #电话 from yhxx #用户信息表"; // 执行SQL语句 $res = $db->query($sql); if ($db->isError($res)) { echo "执行SQL语句时出错"; } while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { $data[] = $row; //将数据放到一个数组中 } //print_r($data); //大家可以看到数据已经放到了一个多维的数组中了 //至此,我们的程序已经基本上完成了,再接下去,我们要定义显示数据的页面 //打开你的DW 或 FrontPage XP,制作显示的页面,我做了一个,并提供给大家下载 //我们制作的数据显示页面文件为:browesData.html /* 这是我们平时要显示的数据列表界面 <html> <head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>数据浏览</title> </head> <body> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> <tr> <td width="21%" align="center" bgcolor="#C0C0C0">代码</td> <td width="50%" align="center" bgcolor="#C0C0C0">企业名称</td> <td width="29%" align="center" bgcolor="#C0C0C0">电话</td> </tr> <tr> <td width="21%"> </td> <td width="50%"> </td> <td width="29%"> </td> </tr> </table> </body> </html>
*/ //我把它加工成一个XSLT格式的HTML文档 /* <?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="GB2312" indent="yes" /> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>数据浏览</title> </head> <body> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> <tbody> <tr> <td width="21%" align="center" bgcolor="#C0C0C0">代码</td> <td width="50%" align="center" bgcolor="#C0C0C0">企业名称</td> <td width="29%" align="center" bgcolor="#C0C0C0">电话</td> </tr> <xsl:for-each select="root/result/row"> <tr> <td width="21%"> <xsl:value-of select="nsrnm" /></td> <td width="50%"> <xsl:value-of select="qymc" /></td> <td width="29%"> <xsl:value-of select="qydh" /></td> </tr> </xsl:for-each> </tbody> </table> </body> </html> </xsl:template> </xsl:stylesheet> */ $htmlFile="browesData.html" ; $htmlStr = readXsl($htmlFile); //将xslt格式的HTML文档读取到变量中 echo getHtml($data, $htmlStr) ; //程序结束 ?>
|