Jump to content

Php Xml And Xsl ("master Page" & "details Page")


lgodinhoramos
 Share

Recommended Posts

Good morning everyone.I have an XML file name: specials.xml

<?xml version="1.0" encoding="UTF-8"?><specials>	<menu_item id="1" course="app" status="live">		<name>Summer Salad</name>		<link>salad.html</link>		<description>organic butter lettuce with apples, blood oranges, gorgonzola, and raspberry vinaigrette</description>		<price>7</price>	</menu_item>	<menu_item id="2" course="app" status="live">		<name>Thai Noodle Salad</name>		<link>noodles.html</link>		<description>lightly sauteed in sesame oil with baby bok choi, portobello mushrooms, and scallions</description>		<price>6</price>	</menu_item></specials>

The testmaster.php and testmaster.xsl pages (below) lists all products with status "live" and provides a link to the page testdetails.php that will show only the details of the item clicked.testmaster.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body>   <?php $xml = new DOMDocument();$xml->load( 'specials.xml' );$xsl = new DOMDocument();$xsl->load( 'testmaster.xsl' );$xslt = new XSLTProcessor();$xslt->importStylesheet( $xsl );echo $xslt->transformToXML( $xml );?></body></html>

testmaster.xsl

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">		<xsl:output				indent="yes"				method="xml"				omit-xml-declaration="yes" />						<xsl:template match="/">						 <xsl:for-each select="specials/menu_item">				<ul>	   					<li>					<xsl:if test="@status = 'live'">						<a>							 <xsl:attribute name="href">							   <xsl:text>testdetails.php?id=</xsl:text>							   <xsl:value-of select="./@id" />							 </xsl:attribute>							 								 <xsl:value-of select="name"/>						</a>					  </xsl:if>						</li>	 				</ul>			</xsl:for-each>		</xsl:template></xsl:stylesheet>

Below are the pages (I'm trying to build) responsible for showing the details of the clicked item, testdetails.php and testdetails.xsl.testdetails.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title><body><?php   $id = $_GET['id'];echo $id; ?>     <?php $xml = new DOMDocument();	$xml->load( 'specials.xml' );$xsl = new DOMDocument();$xsl->load( 'testdetails.xsl' );$xslt = new XSLTProcessor();$xslt->importStylesheet( $xsl );echo $xslt->transformToXML( $xml );?></body></html>

and testdetails.xsl

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">		<xsl:output				indent="yes"				method="xml"				omit-xml-declaration="yes" />						<xsl:template match="/">			 				<xsl:if test="@id = '$id'">							  				Categoria:<xsl:value-of select="specials/menu_item/@course"/>				<br />				<br />				Codigo: <xsl:value-of select="specials/menu_item/@id"/><br />				<br />				Nome: <xsl:value-of select="specials/menu_item/name"/><br />				<br />				Descricao: <xsl:value-of select="specials/menu_item/description"/><br />				<br />				Price: £<xsl:value-of select="specials/menu_item/price"/>		 		 </xsl:if>									  		</xsl:template></xsl:stylesheet>

I'm trying to build these details pages, but I'm not getting the desired result.Could someone help me?Thanks. :-)Luis

Edited by Luis G. Ramos
Link to comment
Share on other sites

XSLT doesn't know the ID of the special you want to print the details for.You must either load into it only the special itself OR pass a parameter to XSLT, telling it which one to look for.For the sake of efficiency, I reccomend the former. The best (though arguably not the easiest) way to do it is with XMLReader, like so (in testdetails.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title><body><?php   $id = $_GET['id'];echo $id;?>     <?php$xml = new DOMDocument;$reader = new XMLReader();$reader->open( 'specials.xml' );while( $reader->next( 'menu_item') ) {	if ($reader->getAttribute( 'id' ) === $id) {		$xml->appendChild($xml->importNode($reader->expand(), true));		break;	}}if (!$xml->documentElement instanceof DOMElement) {//There is no menu_item with this ID. You can perform error handling and/or create an error document here}$xsl = new DOMDocument();$xsl->load( 'testdetails.xsl' );$xslt = new XSLTProcessor();$xslt->importStylesheet( $xsl );echo $xslt->transformToXML( $xml );?></body></html>

The XSLT of course also needs to be tweaked in order to work with menu_item as its root element. In particular:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">		<xsl:output				indent="yes"				method="xml"				omit-xml-declaration="yes" />						<xsl:template match="/menu_item">							  				Categoria:<xsl:value-of select="@course"/>				<br />				<br />				Codigo: <xsl:value-of select="@id"/><br />				<br />				Nome: <xsl:value-of select="name"/><br />				<br />				Descricao: <xsl:value-of select="description"/><br />				<br />				Price: £<xsl:value-of select="price"/>									  		</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

It looks like there's no match.Try to replace

if ($reader->getAttribute( 'id' ) === $id) {

with

if ($reader->getAttribute( 'id' ) == $id) {

(because PHP may be converting the ID to a number, at a which point it won't match the string of the same number)If that doesn't work, try to do some debugging... add some echoes around (e.g. inside the body of the same "if" above, inside the "if" later on, right after that "if"), etc.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...