Jump to content

xml xsl php tutorial


vchris
 Share

Recommended Posts

I know how xml and xsl work, it's when it comes to including php in the mix. How is the documents supposed to look? Can you have includes such as navigation.xml, header.xml and footer.xml like I do in php. I've been looking at tutorials but it's still not really clear to me how it works. I just want to separate presentation from the code. Some pages also draw from the database. If you know of a very good tutorial website on the combination of these 3 technologies let me know. If someone is running this kind of setup on a small website I'd like to see how it works, maybe provide sample pages.

Link to comment
Share on other sites

You can use XSLT's document() function to process additional XML documents if needed. You process those in the same way you process your main XML source document.As for drawing data from a DB, there are quite a few approaches to this. The easiest (though not the most efficient) is to generate an XML file with the DB output, and use that as an input for XSLT. Another less easier (but more efficient) approach is to generate "hooks" in your HTML output, traverse them with XMLReader, and populate the DB entries in the hook's places.A sample:XML source file

<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:s="urn:myTools:interactive">	<head>		<title>Products</title>	</head>	<body>		<div>			<s:paginate what="pictures" recordsPerPage="1">				<s:query>					<s:column name="location" />				</s:query>				<div id="controls">					<s:paginationControl type="first">◄=</s:paginationControl> 					<s:paginationControl type="previous">◄</s:paginationControl> <!--					<ul>						<s:paginationControl type="goTo" from="-1" to="1" keepDistance="true">							<s:variable name="pageNumber"><s:pageNumber/></s:variable>							<li><a href="{$pageNumber}"><s:pageNumber/></a></li>						</s:paginationControl>					</ul>-->					<s:paginationControl type="next">►</s:paginationControl> 					<s:paginationControl type="last">=►</s:paginationControl>				</div>				<s:forEachRecord>					<s:variable name="location" value="location"/>						<a href="/_images/{$location}" rel="external"><img src="/_images/thumbs/{$location}" /></a>				</s:forEachRecord>			</s:paginate>		</div>	</body></html>

an excerpt from the PHP engine:

<?php...	if (!$transformation = $xslt->transformToDoc($xml)) {		die('Error during transformation of "' . $xmlFile . '" with  "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT construct. The exact error message is:' . libxml_get_last_error()->message . ' on line ' . libxml_get_last_error()->line);	}	unset($xslt, $xml, $xsl);	//$transformation->formatOutput = true;	if ($transformation->getElementsByTagNameNS('urn:myTools:interactive', '*')->length === 0) {		echo $transformation->saveXML();	}else {		include_once 'myToolsEngine.php';		ob_implicit_flush(false);		myToolsExpansion($transformation->saveXML());	}...?>

The 'urn:myTools:interactive' namespace is my own hook I use for placing DB calls. If there are no hooks, I give the output directly, and if there are, I start parsing the output and replace the DB calls with the appropriate data.What the XSLT does is to draw the main layout, and insert the XML directly into it (i.e. it just passes my hooking namespace along).The engine itself is still very bulky and buggy, but this excerpt illustrates the main idea. That is actually a part of a site that I recently built, and is online now - http://kl-systems.net. I'm going to be moving it to a new server though, because this one has some interet connectivity issues.(and yes, I realize it feels very empty... I asked the client for more info, but he said he'd prefer to talk to interested parties in person as prices and products vary per customer)

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...