2010-12-02 1 views
0

나는 mysql 프로 시저에서 계층 데이터를 가져 와서 xml로 인쇄하는 PHP 스크립트를 가지고있다. 이 결과를 html 순서가 지정되어 있지 않은 부모 자식 목록으로 인쇄하려고합니다. 어떻게해야합니까?이러한 결과를 xml 대신 html 순서가 지정되지 않은 목록 양식으로 인쇄하는 방법은 무엇입니까?

<?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

읽기. 예를 들면 다음과 같습니다. http://devzone.zend.com/article/1302 – rMX

답변

1

XSLT : 여기에 XML 인쇄 내 PHP 스크립트는 XSL-변환에 대한

<?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> 
+0

'xsl : apply-templates'에'@ match' 속성이 없으므로'@ select'를 원했습니다. –

+0

@Per : 물론 이죠. –

+0

@Ignacio Vazquez-Abrams, saveXML이 여기에서하는 것처럼 DOM 표현을 사용하여 HTML 표현을 직접 생성하는 메소드가 있습니까? – XCeptable

관련 문제