我有一个php脚本,它从mysql过程中获取分层数据并将其打印为xml。我想将这些结果打印为html无序父子列表。我怎么做。这是我的php脚本打印xml:
<?php
header("Content-type: text/xml");
$conn = new mysqli("localhost", "user", "********", "spec", 3306);
// one non-recursive db call to get the message tree !
$result = $conn->query("call message_hier(1)");
//--$result = $conn->query("call message_hier_all()");
$xml = new DomDocument;
$xpath = new DOMXpath($xml);
$msgs = $xml->createElement("messages");
$xml->appendChild($msgs);
// loop and build the DOM
while($row = $result->fetch_assoc()){
$msg = $xml->createElement("message");
foreach($row as $col => $val) $msg->setAttribute($col, $val);
if(is_null($row["parent_msg_id"])){
$msgs->appendChild($msg);
}
else{
$qry = sprintf("//*[@msg_id = '%d']", $row["parent_msg_id"]);
$parent = $xpath->query($qry)->item(0);
if(!is_null($parent)) $parent->appendChild($msg);
}
}
$result->close();
$conn->close();
echo $xml->saveXML();
?>
这是它打印的xml
<messages>
<message msg_id="1" emp_msg="msg 1" parent_msg_id="" parent_msg="" depth="0">
<message msg_id="2" emp_msg="msg 1-1" parent_msg_id="1" parent_msg="msg 1" depth="1"/>
<message msg_id="3" emp_msg="msg 1-2" parent_msg_id="1" parent_msg="msg 1" depth="1">
<message msg_id="4" emp_msg="msg 1-2-1" parent_msg_id="3" parent_msg="msg 1-2" depth="2"/>
<message msg_id="5" emp_msg="msg 1-2-2" parent_msg_id="3" parent_msg="msg 1-2" depth="2">
<message msg_id="6" emp_msg="msg 1-2-2-1" parent_msg_id="5" parent_msg="msg 1-2-2" depth="3">
<message msg_id="7" emp_msg="msg 1-2-2-1-1" parent_msg_id="6" parent_msg="msg 1-2-2-1" depth="4"/>
<message msg_id="8" emp_msg="msg 1-2-2-1-2" parent_msg_id="6" parent_msg="msg 1-2-2-1" depth="4"/>
</message>
</message>
</message>
</message>
</message
答案 0 :(得分:1)
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="messages">
<html:ul>
<xsl:apply-templates select="message" />
</html:ul>
</xsl:template>
<xsl:template match="message[message]">
<html:li>message <xsl:value-of select="@msg_id" /></html:li>
<html:ul>
<xsl:apply-templates select="message" />
</html:ul>
</xsl:template>
<xsl:template match="message">
<html:li>message <xsl:value-of select="@msg_id" /></html:li>
<xsl:apply-templates select="message" />
</xsl:template>
</xsl:stylesheet>