Embed XML in HTML so that XSLT can transform XML

OK, I have been puzzling on this for several hours and I can’t seem to get it right, so I thought there are probably some helpful Sitepointers around :slight_smile:

Recently, I’ve been playing around with an XSLT-powered templating system for a forum. I’ve gotten to the point that I know how to transform an XML file using XSLT. This all works fine. However, now I want to have XML, which is inside HTML, to be styled by XSLT, as well. All of a sudden, the XSLT does nothing anymore! Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Not important</title>

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="forumlist.xsl"?>

<name>Another test forum</name>

<name>Test forum</name>



The forumlist.xsl file works fine… when I make a seperate file with only the XML things are transformed fine.

Does anyone know what my problem is? My guess it that “inline” XML is fine in HTML, but the XSLT is just “not getting grip” on the XML.

  • Peter

I have seen how to use xsl to output xml as html, like in this article:
It uses java with lotus and xerces, but you could parse with php or other parsers too.

It seems you did not understand my question. I know how to use XSLT to transform XML into HTML. This works fine.

The thing is: I have XML inside HTML and I want that XML transformed using XSLT. See the example code I posted.

Thank you for your reply, however.

Could you just use CSS to style inline XML???

Im no expert at all, just a suggestion?

No, I could not. CSS does not support things as foreach, as well.

First off, your method of embedding XML in an XHTML document won’t work. For one, you can’t really mix XML namespaces in XHTML 1.0; you have to go to at least XHTML 1.1 for that. Second, it looks like you did copy/paste from an XML file into an empty HTML file. That won’t work since you have to properly namespace everything.

I still really don’t understand why you’re putting XML in an HTML file like that. You could just as easily do an XSLT transformation on the server to produce your HTML and send that to the client’s browser. Browsers suck at XML as a general rule and HTML is going to be much easier for them to do anything with.

vgarcia, thanks for your response. The thing is that I wish to use XSLT for a templating system. However, the page is not finished with “just” the transformed XML. I want extra HTML next to it. Let me explain this in more detail.

I am creating a forum. What I want is to have it so that users can style their forum using XSLT. Therefore, the forum list will have to be outputted in XML. This works fine, I just generate the XML with PHP, which pulls it from a database.

Now, I can transform the XML without any problems. However, what I end up with is a file with only the transformed XML. Therefore, it is not really a page. It has no HTML elements besides the ones that I put in the XSLT template. I don’t want to put the rest of the page design in this XSLT template!

This is the template file:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

<xsl:template match="/">
    <h2>Board index</h2>
    <tr bgcolor="#9acd32">
      <th align="left">Name</th>
    <xsl:for-each select="forumlisting/forum">
      <td><xsl:value-of select="name"/></td>

So, my question really is this:

How can I transform XML into HTML using XSLT, while the XML is inside an HTML document?

In that case, may I suggest something like this:

//let's assume you have an XML transforming function in another file.
...other stuff...
//put your XML in a variable, either by reading it from
//the filesystem or using an XML library
$xml = whatever;
//do the same for the XSL
$xsl = whatever;

//now we transform and print to browser
echo transformFunction($xml, $xsl);


Don’t bother putting the XML in your HTML page at all, just show the output on the page after the transformation.

Yes… that’s exactly what I want! However, how can I transform XML to HTML on the server? Isn’t XSLT a client-side technology, like CSS?

Not at all, as a matter of fact I almost exclusively use XSLT server-side because many browsers don’t support XSLT client-side (like Opera). There are a few XML/XSLT parsing libraries available in any language; you mentioned you were doing this in PHP, so make sure your web host has the proper libraries installed for you to be able to run XSLT on the server; trust me, it’s a lot easier than trying to mix namespaces and depending on client-side transformation.

OK, I will parse the XSLT on the server as you suggested. Thanks for your help! :slight_smile:

OK, so I have another problem. The XSLT functions in PHP are disabled by default. Is there any PHP class or something of the like that I can just include with my script for distribution, that you know of? I don’t think users will appreciate having to ask their host to enable XSLT support for them.

PHP5 supports XSLT natively I believe.

PS: Welcome to the wonderful world of PHP4 and XML. As far as I know you’re going to have to get the hosts to install the required libraries, there’s no easy script you can include in your distribution that automatically turns this stuff on. However, if you’re releasing an app that works with XML and XSLT to a great extent, then having someone’s host install a library shouldn’t be that much of an issue. If it is, you might want to try another method for templating in your app.

Yes, PHP5 supports XSLT natively, but it is disabled by default (which means the host will have to enable it). I’m not worrying about PHP4, as my application is PHP5 only, anyway. Thanks for your advice, I will just tell people to have their hosts enable XSLT support for them.

  • Peter