从 php 生成 xml 和 xsl
我正在尝试从 mysql 数据库生成 xml。应用程序不需要知道数据库中存在的表。
该功能是这样的,您输入表名称,它会为其生成 xml。
该函数工作完美,但我需要使用 xsl 来设置 xml 的样式。由于应用程序不知道要处理哪个表,因此编写预定义的 xsl 无法正常工作。
有人可以建议我如何以编程方式编写 xsl 以及生成的 xml。
该函数粘贴在下面。
PHP 生成 XML (genXMl.php):
<?
header("content-type:text/xml");
if(isset($_GET['tbl']))
{
$myServer = "localhost";
$myUser = "user";
$myPass = "pwd";
$myDB = "test";
$table = $_GET['tbl'];
function getXML($sql="Default Query")
{
$conn=mysql_connect("localhost","user","pwd");
$db=mysql_select_db("test");
$result = mysql_query($sql,$conn);
$columns="";
echo "<records>";
while($row=mysql_fetch_assoc($result))
{
$columns.="<record>";
foreach($row as $key => $value)
{
$columns.="<$key>$value</$key>";
}
$columns.="</record>";
}
echo $columns;
echo "</records>";
}
getXML("SELECT * FROM $table");
}
XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My header</h2>
<table border="1">
<xsl:for-each select="records/record">
<tr>
<td><xsl:value-of select="<?php echo $key; ?>" /></td> //trying to style the $KEY
</tr>
<tr>
<td><xsl:value-of select="<?php echo $value;?>"/></td> //trying to style the $VALUE </tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
i am trying to generate xml from mysql database. The application does not need to know the tables that exist in the database.
the function is such that you enter the table name and it generates the xml for it.
The function works perfect but i need to style the xml with xsl. since the application does not know which table it is going to handle, writing a predefined xsl has fialed to work.
can someone suggest how i might programmatically write an xsl along with the generated xml.
the function is pasted below.
PHP to generate XML (genXMl.php):
<?
header("content-type:text/xml");
if(isset($_GET['tbl']))
{
$myServer = "localhost";
$myUser = "user";
$myPass = "pwd";
$myDB = "test";
$table = $_GET['tbl'];
function getXML($sql="Default Query")
{
$conn=mysql_connect("localhost","user","pwd");
$db=mysql_select_db("test");
$result = mysql_query($sql,$conn);
$columns="";
echo "<records>";
while($row=mysql_fetch_assoc($result))
{
$columns.="<record>";
foreach($row as $key => $value)
{
$columns.="<$key>$value</$key>";
}
$columns.="</record>";
}
echo $columns;
echo "</records>";
}
getXML("SELECT * FROM $table");
}
THE XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My header</h2>
<table border="1">
<xsl:for-each select="records/record">
<tr>
<td><xsl:value-of select="<?php echo $key; ?>" /></td> //trying to style the $KEY
</tr>
<tr>
<td><xsl:value-of select="<?php echo $value;?>"/></td> //trying to style the $VALUE </tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应将 xml 标签定义为数据库中的键,而应为 xml 定义一个可以使用 DTD 或 XSD 进行验证的唯一结构。
那么,您的 XSL 将非常简单。
例如,您可以这样做:
您可以这样做:
然后,您的 xsl 将很容易制作:)
正如 @Tomalak 的评论中提到的,您应该使用 DOMDocument 而不是连接字符串。
You shouldn't define xml tags as key from your database, but defining an unique structure for your xml that you can validate using a DTD or XSD.
Then, your XSL will be really simple.
For example, instead of that :
You could do something like:
And then, your xsl would be simple to make :)
And as mentionned in the comments by @Tomalak, you should use DOMDocument instead of concatenating strings.