Page 1 of 1

trying to send header on xsl page with php

Posted: Fri Sep 02, 2005 3:52 pm
by valanbrown
I have an xml with xsl file.

I modified the .htaccess file to process xml and xsl, because I need some php functionality, just some simple variables to make a search work using the xsl file and xpath.

This is what the start of my xsl file looks like:

Code: Select all

<?php

header('Content-Type: text/xml');
header('Content-Encoding: UTF-8');

...

?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	<!-- Main Template Begins-->
	<xsl:template match="guide">
		<html>
...
And, the start of my xml file:

Code: Select all

<?php

header('Content-Type: text/xml');
header('Content-Encoding: UTF-8');

...

?>

<?php echo('<?xml-stylesheet type="text/xsl"  href="style.xsl?selector2='); echo($selector); echo('&value2='); echo($value); echo('"?>'); ?>

<guide>
<item>
  <restaurant_name>
...
it worked normally when not set to process for php, but when I enabled php, it didn't seem to recognize as an xsl file, so I tried sending a header to make it recognize as xsl, and it worked perfectly for firefox, but doesn't seem to work in other browsers like opera or IE. I'm guessing I'm not sending the correct header, how do I do it correctly?

Posted: Fri Sep 02, 2005 4:03 pm
by feyd
try Content-type instead of Content-Type.. same for the encoding..

Posted: Fri Sep 02, 2005 4:08 pm
by valanbrown
didn't seem to have any effect

Posted: Fri Sep 02, 2005 4:30 pm
by timvw
Which browser/user-agent are you using? (If your browser doesn't support it, you could consider server-side xslt)

http://www.w3schools.com/xml/xml_browsers.asp

Posted: Fri Sep 02, 2005 4:34 pm
by valanbrown
right now I've tried ie, opera id'd as ie, and firefox. firefox is the only one that works.

my main goal is to get it to work in ie

Posted: Fri Sep 02, 2005 4:35 pm
by valanbrown
one problem with server side is I'm working with php 4, and it's not likely to be upgraded easily

Posted: Fri Sep 02, 2005 6:52 pm
by timvw
php4 can do xslt too ;)

It's pretty easy with the xslt extension (i don't consider the php implementations a real option because they are slooooow)

Posted: Fri Sep 02, 2005 6:58 pm
by valanbrown
I'm not sure if I'd be able to install an extension, but i'll keep that in mind as a solution. I'd really like a way to just get ie to recognize the xsl file, I spent a fair amount of time getting it to work the way it does.

if I do use the xml php functions, I would be able to take the xml and xsl files, and use php out output an (x)html file right? would I be able to use php in the xml and xsl files themselves?

Posted: Fri Sep 02, 2005 8:21 pm
by timvw
You're not required to use "files". You can also pass "strings" to the xslt_process function.

Thus, if you use php to generate a xml-string or xsl-string, you can write your dynamic stuff ;)


An example script could look like:

Code: Select all

require_once('mysql.php');
require_once('rstoxml.php');
require_once('transform.php');
require_once('output.php');

$query = "SELECT * FROM message";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
$xml = rstoxml($result);
$xsl = file_get_contents('xsl/index.xsl');
$xhtml = transform($xml, $xsl);
output($xhtml);

(I presume you write cleaner verions.. Mines were simply hacked together as a proof of concept)
http://timvw.madoka.be/programming/php/rstoxml.php.txt
http://timvw.madoka.be/programming/php/ ... rm.php.txt
http://timvw.madoka.be/programming/php/output.php.txt

Posted: Fri Sep 02, 2005 8:41 pm
by timvw
Apparently IE has an issue with the & in the HREF value to the XSL value...

You would have to use & (unicode for ampersand) instead of & ;)


The following does seem to work: <?xml-stylesheet type="text/xsl" href="style.xsl?selector=foo&value2=bar"?>. Meaby that a call to htmlentities, before echoing the string is the easiest solution ;)

Posted: Fri Sep 02, 2005 8:46 pm
by valanbrown
HOLY CRAP I changed the & and ? to the ascii codes, and it worked, thank you so much

Posted: Fri Sep 02, 2005 8:48 pm
by timvw
Somehow.. i wanted to type '& # 3 8 ;' (remove the spaces between the quotes ;)

Posted: Fri Sep 02, 2005 8:49 pm
by valanbrown
I've even had this problem before, just linking to php files and I totally forgot about it

You just saved me a lot of time, thanks