/**
* 將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()
*/
//我把它加工成一個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) ;
//程序結束
?>