xsl syntax problem

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

xsl syntax problem

Post by raghavan20 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


when tried to use a xsl file with a xml file, firefox gives me this explanatory error message

Code: Select all

Error during XSLT transformation: XSLT transformation failed.
the xml file

Code: Select all

<?xml version="1.0"?>
<?xml-stylesheet href="breakfast.xsl" type="text/xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
  </food>
</breakfast_menu>

the xsl file

Code: Select all

<?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='/'>
	
		<xsl:template match='/breakfast_menu' >
			<h3>Break Fast Menu</h3>
		</xsl:template>
		
		<h4>The food items are listed below: </h4>
		
		<xsl:for-each select='/breakfast_menu/food'>	
			<xsl:template match='food'>
				<table>
					<tr>
						<th>Name</th><th>Description</th><th>Price</th><th>Calories</th>
					</tr>
					<tr>
						<td><xsl:value-of select='name' /></td>
						<td><xsl:value-of select='description' /></td>
						<td><xsl:value-of select='price' /></td>
						<td><xsl:value-of select='calories' /></td>
					</tr>
				</table>				
			</xsl:template>
		</xsl:for-each>
		
	</xsl:template>

</xsl:stylesheet>

then i got fed up with browser transformation as even IE could not tell me in which line the error is so I thought server transformation is the best way to do it. So I wrote the following script.

Code: Select all

<?php


// Load the XML source
$xml = new DOMDocument;
$xml->load('breakfast.xml');

$xsl = new DOMDocument;
$xsl->load('breakfast.xsl');

// Configure the transformer
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);

?>
but i ended up with this.

Code: Select all

Fatal error: Class 'XSLTProcessor' not found in C:\Program Files\Apache Group\Apache2\htdocs\xmlxsl1.php on line 12
fyi, i have already have this in php.ini

Code: Select all

;extension=php_sybase_ct.dll
;extension=php_tidy.dll
;extension=php_xmlrpc.dll
extension=php_xsl.dll
thanks for any help guys.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

When I run this in IE7 I get
Das Schlüsselwort xsl:template kann hier nicht verwendet werden.
"The keyword xsl:template cannot be used here"
What's
<xsl:for-each select='/breakfast_menu/food'>
<xsl:template match='food'>
supposed to do?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah you can't put templates inside loops. A template is like a function definition, it is a handler for a certain match of tag. You probably want

Code: Select all

<xsl:apply-templates whateverAttributeNameICantRememberRightNow="/breakfast_menu" />
and no for-each. Generally with templates you barely need loops, you can achieve an amazing amount with just xsl:template and xsl:apply-template and actually they are a lot more flexible than loops.
Post Reply