How to print these results in html unordered list form in stead of xml?
I have a php script that takes hierarchical data from a mysql procedure and prints it as xml. I want to these results to be printed as html unordered parent child list. How do I do that. Here is my php script that prints 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();
?>
this is xml it prints
<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"/>
开发者_C百科<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
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>
精彩评论