XML and XSLT

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
sgavan
Forum Newbie
Posts: 2
Joined: Sun Nov 09, 2003 8:13 am

XML and XSLT

Post by sgavan »

Hi,

I have a PHP page and in it I would like to "include" an XML file that has a XSL stylesheet. When I use the include() function, without the <? xml-stylesheet?> in the XML file, it loads the XML fine.

But, when I add the <? xml-stylesheet ?> tag I get a parsing error.

Can someone tell me of a way around this and if this approach will work?

I am using this on a Windows platform.

Thanks,

Ste
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post by maniac9 »

If you're just including it, that won't work, since XML uses <? ?> for the header information. PHP will think the file contains some PHP, which is wrong, and then give you a nice error message. To get around this, you need to print the header and escape the qutoes...like...

Code: Select all

echo("<?xml version="1.0" encoding="ISO-8859-1"?>\n");
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or you disable short-tags in your php.ini
http://www.php.net/manual/en/configuration.directives.php wrote:short_open_tag boolean

Tells whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"'; ?>. Also if disabled, you must use the long form of the PHP open tag (<?php ?>).

Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on.
sgavan
Forum Newbie
Posts: 2
Joined: Sun Nov 09, 2003 8:13 am

Post by sgavan »

Thank you for your replies. I've added the echo() functions to the XML files and the error has disappeared.

But the XML file still does not apply the stylesheet? I have used the same approach as you mentioned for the <?xml-stylesheet?> tag, but no luck!

I am running the PHP server for Windows under XP? Could this have any effect on the outcome? If I open the XML file using IE, then it applies the stylesheet with no problems.

I'm still trying to sort this out, so your help will be much appreciated.

Thank you,

Ste
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php itself does not apply the stylesheet when a xml-file is included.
In fact, php isn't aware it is including a xml-file. It simply searches for php-blocks, anything else is just a stream of characters with no special meaning (to php)
but there are functions to perform a transformation, take a look at http://php.net/xslt
Post Reply