全部博文(315)
分类:
2007-11-09 13:19:37
// include required files
include 'XML/Query2XML.php';
include 'MDB2.php';
try {
// initalize Query2XML object
$q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:pass@localhost/world'));
// generate SQL query
// get results as XML
$sql = "SELECT * FROM Country";
$xml = $q2x->getFlatXML($sql);
// read XSL stylesheet data
$xsl = new DOMDocument;
$xsl->load('country.xsl');
// initialize XSLT engine
$xslp = new XSLTProcessor;
// attach XSL stylesheet object
$xslp->importStyleSheet($xsl);
// perform transformation
header('Content-Type: text/html');
echo $xslp->transformToXML($xml);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
对了示例数据库下面提供下载:
|
以及xsl文件:
3、上面只是简单的提供数据库查询结果转xml并有xsl格式化输出,下面来点特别的:
// include required files
include 'XML/Query2XML.php';
include 'MDB2.php';
try {
// initalize Query2XML object
$q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:pass@localhost/world'));
// generate SQL query
// get results as XML
$sql_1 = "SELECT * FROM Country";
$sql_2 = "SELECT * FROM City WHERE CountryCode = ? ORDER BY Population DESC LIMIT 5";
$xml = $q2x->getXML($sql_1, array(
'idColumn' => 'code',
'rootTag' => 'countries',
'rowTag' => 'country',
'attributes' => array('code', 'name', 'continent'),
'elements' => array('cities' => array(
'sql' => array('data' => array('code'), 'query' => $sql_2),
'idColumn' => 'id',
'rootTag' => 'cities',
'rowTag' => 'city',
'attributes' => array('name','district','population'))
)
)
);
// read XSL stylesheet data
$xsl = new DOMDocument;
$xsl->load('countries.xsl');
// initialize XSLT engine
$xslp = new XSLTProcessor;
// attach XSL stylesheet object
$xslp->importStyleSheet($xsl);
// perform transformation
header('Content-Type: text/html');
echo $xslp->transformToXML($xml);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
注意粗红字体部分,一句话概括的说就是在序列化xml的时候,不只可以用一个sql,还可以把一个sql的查询结果作为第二个sql查询的依据从而生成xml
counties.xsl的代码如下