Transforming XML with XSL

Soldato
Joined
31 Oct 2005
Posts
8,846
Location
Leeds
Hi there again, should be a quick simple answer to this

I have a PHP page that takes my SQL database and outputs it as an XML file

I then want to transform the XML using XSL, I already have the XSL file

this is my PHP file

<?php

header("Content-type: text/xml");

$host = "mysql4.hehe.com";
$user = "not seeing my pass";
$pass = "not ever";
$database = "nah";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM a5168769_cust";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<entries>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<username>\n";
$xml_output .= "\t\t<username>" . $row['username'] . "</username>\n";
// Escaping illegal characters
$row['text'] = str_replace("&", "&", $row['text']);
$row['text'] = str_replace("<", "<", $row['text']);
$row['text'] = str_replace(">", "&gt;", $row['text']);
$row['text'] = str_replace("\"", "&quot;", $row['text']);
$xml_output .= "\t\t<password>" . $row['password'] . "</password>\n";
$xml_output .= "\t</username>\n";
}

$xml_output .= "</entries>";

echo $xml_output;

?>

I have the line

<?xml-stylesheet type="text/xsl" href="inn.xsl"?>

I need to include that somewhere in the above PHP script, whereabouts would I place it? Many thanks
 
echo it just after the header :)

cheers, what am I looking at here

I've tried

echo <?xml-stylesheet type="text/xsl" href="inn.xsl"?> and that doesn't work, I'm assuming because I've used the echo command I need to look at my line of code following it but I don't know how to correctly present it
 
Can't use XLST on these server's sadly, thats why I'm going down this path...


cheers, when I post that into dreamweaver some of the colours dissapear as if the code is no longer regonised, anyway I saved it and ran it and now I'm getting

Scrap the above

You left a PHP tag in that was a note rather than an actual command, deleted it and got the colours back

Now I'm getting

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://www.micholden.site50.net/outputocuk.php'. Line...

<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="output.xsl"?>\n<br><tabl...
 
Last edited:
The XML output looks like this when using the orginial code before we started the changes

http://www.micholden.site50.net/outputxml3.php

This is the XSL file

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<html>
<body>
<h2>Latest News</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">id</th>
<th align="left">username</th><br />
<th align="left">password</th>
</tr>
<xsl:for-each select="entries/username">
<tr>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="username"/></td>
<td><xsl:value-of select="password"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template></xsl:stylesheet>

Firefox gives this error when running the new code
XML Parsing Error: not well-formed
Location: http://www.micholden.site50.net/outputocuk.php
Line Number 1, Column 39:<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="output.xsl"?>\n<entries>
--------------------------------------^

Even if we don't get this sorted I really owe you thanks for taking time out to help:D
 
After running the new code

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Only one top level element is allowed in an XML document. Error processing resource 'http://www.micholden.site50.net/output...

<script type="text/javascript">
-^

Firefox

XML Parsing Error: junk after document element
Location: http://micholden.site50.net/outputocuk3.php
Line Number 32, Column 1:<script type="text/javascript">
^
 
Back
Top Bottom