trying to send header on xsl page with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

trying to send header on xsl page with php

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try Content-type instead of Content-Type.. same for the encoding..
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post by valanbrown »

didn't seem to have any effect
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post 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
Last edited by valanbrown on Fri Sep 02, 2005 4:37 pm, edited 1 time in total.
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post by valanbrown »

one problem with server side is I'm working with php 4, and it's not likely to be upgraded easily
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;)
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post by valanbrown »

HOLY CRAP I changed the & and ? to the ascii codes, and it worked, thank you so much
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Somehow.. i wanted to type '& # 3 8 ;' (remove the spaces between the quotes ;)
valanbrown
Forum Newbie
Posts: 7
Joined: Fri Sep 02, 2005 3:38 pm

Post 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
Post Reply