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

I have to transform my xml output to html. I am following a tutorial at link text My code is outputting xml tags in a single line without any formatting with text value.I want the output as an HTML unordered list following hierarchical structure of parent child in xml file output.

Here is 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 is 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 is my 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>

Answers


Your stylesheet does not output HTML but rather an XHTML fragment, and that in a way (with qualified names) that you need to serve it as application/xml to a browser (like Mozilla, Opera, Safari, IE 9, but not IE 6-8) that understands that content type.

So make sure you do something like

  header('Content-Type: application/xml');

before sending the content to the browser. Or drop any XHTML namespace and any prefixes from the result elements, then the XSLT stylesheet outputs an HTML fragment many more browsers can parse and understand as text/html and render it as you want.


Need Your Help

How to change background of UITableViewController in GroupedStyle

c# colors monotouch uitableview

I'm trying to change the color of a UITableViewController when using a split view. Then the style is set to grouped, the background seems to light gray no matter what.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.