douchen4547 2010-12-02 10:03
浏览 68
已采纳

如何以html无序列表形式而不是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"/>
            <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
  • 写回答

1条回答 默认 最新

  • dpprx26000 2010-12-02 10:54
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂