Transforming XML data to HTML using XSL and Php: only printing xml tags without any formatting -


i have transform xml output html. following tutorial @ link text code outputting xml tags in single line without formatting text value.i want output html unordered list following hierarchical structure of parent child in xml file output.

here page output:

message msg 1message msg 1-1message msg 1-2message msg 1-2-1message msg 1-2-2message  msg 1-2-2-1message msg 1-2-2-1-1message msg 1-2-2-1-2  

& here page source:

<html:ul xmlns:html="http://www.w3.org/1999/xhtml"><html:li>message msg 1</html:li> <html:ul><html:li>message msg 1-1</html:li><html:li>message msg 1-2</html:li><html:ul> <html:li>message msg 1-2-1</html:li><html:li>message msg 1-2-2</html:li><html:ul> <html:li>message msg 1-2-2-1</html:li><html:ul><html:li>message msg 1-2-2-1-1</html:li> <html:li>message msg 1-2-2-1-2</html:li></html:ul></html:ul></html:ul></html:ul></html:ul> 

here code

php file:  <?php # load xml file $xml = new domdocument(); $xml->load('messages.xml');  # start xslt $xslt = new xsltprocessor(); $xsl = new domdocument(); $xsl->load('msg.xsl'); $xslt->importstylesheet( $xsl ); print $xslt->transformtoxml( $xml ); ?> 

msg.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> 

messages.xml

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="msg.xsl"?><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> </messages> 

your stylesheet not output html rather xhtml fragment, , in way (with qualified names) need serve application/xml browser (like mozilla, opera, safari, ie 9, not ie 6-8) understands content type.

so make sure like

  header('content-type: application/xml'); 

before sending content browser. or drop xhtml namespace , prefixes result elements, xslt stylesheet outputs html fragment many more browsers can parse , understand text/html , render want.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -