Php 如何以html无序列表形式而不是xml打印这些结果?

Php 如何以html无序列表形式而不是xml打印这些结果?,php,mysql,html,xml,domdocument,Php,Mysql,Html,Xml,Domdocument,我有一个php脚本,它从mysql过程中获取分层数据并将其打印为xml。我想将这些结果打印为html无序父子列表。我该怎么做呢。下面是我打印xml的php脚本: <?php header("Content-type: text/xml"); $conn = new mysqli("localhost", "user", "********", "spec", 3306); // one non-recursive db call to get th

我有一个php脚本,它从mysql过程中获取分层数据并将其打印为xml。我想将这些结果打印为html无序父子列表。我该怎么做呢。下面是我打印xml的php脚本:

  <?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();
      ?>
query(“呼叫消息(1)”;
//--$result=$conn->query(“调用消息\u hier\u all()”;
$xml=新文档;
$xpath=newdomxpath($xml);
$msgs=$xml->createElement(“消息”);
$xml->appendChild($msgs);
//循环并构建DOM
而($row=$result->fetch_assoc()){
$msg=$xml->createElement(“消息”);
foreach($col=>$val的行)$msg->setAttribute($col,$val);
如果(为空($row[“parent\u msg\u id”])){
$msgs->appendChild($msg);
}
否则{
$qry=sprintf(“/*[@msg\u id='%d']”,$row[“parent\u msg\u id”]);
$parent=$xpath->query($qry)->项(0);
如果(!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

XSLT:


消息
消息

阅读有关xsl转换的信息。例如:在
xsl:apply templates
上没有属性
@match
,我相信您想要
@select
@Ignacio Vazquez Abrams,难道没有像这里这样的方法可以使用DOM表示直接生成html表示吗?您没有任何html要保存。您需要先对其进行转换。@Ignacio Vazquez Abrams,您能告诉我如何在代码中使用这个xslt文档吗,因为我还没有使用xslt.Soryy来处理这样一个琐碎的问题。首先,您要像解析任何其他文档一样解析样式表。然后使用
XSLTProcessor
将XML文件和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>