XML and XSLT technologies provides standard ways of separation of presentation and data.
This article contains an example of simple php "xslt engine" for XML driven web-sites which implements
caching techniques and Apache-based XML file processing.
First, we have to set up our engine to process every *.xml file on web server. We use following Apache directives: config file: .htaccess, httpd.conf AddHandler ae_xslt xml # we add handler 'ae_xslt' associated with xml files Action ae_xslt /ae-xslt.php # we set our script '/ae-xslt.php' with handler 'ae_xslt' # thus every *.xml file should pass through our script '/ae-xslt.php' DirectoryIndex index.xml index.php index.html # adding index.xml to directory index list, specifies that # yoursite.com -> yoursite.com/index.xml # yoursite.com/folder/ -> yoursite.com/folder/index.xml In case you're curious, here is description of there directives from Apache documentation: AddHandler, Action You may put these lines in .htaccess file in the root folder of your site (your hosting provider should allow override 'FileInfo' in .htaccess files) or directly to httpd.conf of Apache. Our script will receive file information of processed xml files in two environmental variables: PATH_INFO (http-server path to file, like /page1.xml ) and PATH_TRANSLATED (filesystem path to file, like /var/www/htdocs/page1.xml or something ) As web-server does not check existence of the handled files, engine should do additional checking and output 404 error message if requested xml file does not exist. Afterwards, our engine check if there is a fresh cached version of requested file. Checking is done comparing file modification times. If cached version is valid, engine outputs it and exists. Otherwise, engine loads main XSLT file 'ae-site.xslt', loads requested xml file, does transformation and saves new cached file. Here is the source code: source code: php <?phpAs you may see, cached files is stored in '.cache' subfolder of web-site. Make sure it exists and is writable to your PHP scripts How to use it? Look at the XML file: source code: xml <?xml version="1.0"?> <page> <title>Page 2</title> <subtitle>Famous panagrams:</subtitle> <paragraph> The quick brown fox jumped over the lazy dog's typewriter. </paragraph> <paragraph> Cozy lummox gives smart squid who asks for job pen </paragraph> <references> <item url="http://www.anyexample.com">AnyExample</item> <item url="http://en.wikipedia.org/wiki/Panagram">Wikipedia panagrams</item> </references> </page> Web site's main XSLT file 'ae-site.xslt' contains following template: source code: XSLT <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- XML output mode --> <xsl:output method="xml" standalone="yes" indent="no" encoding="utf-8"/> <!-- we do not need spaces in output file --> <xsl:strip-space elements="*"/> <!-- this template copies unknown XML tags to output file, allows use of XHTML --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <!-- Main page template --> <xsl:template match="page"> <html> <head> <title><xsl:value-of select="//title"/></title> </head> <body> <div style="width: 50%; padding: 8px; background-color: #DDD;"> <b>Menu: </b> <a href="index.xml">Main page</a>, <a href="page1.xml">Page 1</a>, <a href="page2.xml">Page 2</a> </div> <div style="width: 50%; padding: 4px; background-color: #EEE;"> <h1><xsl:value-of select="//title"/></h1> <xsl:apply-templates match="content"/> </div> </body> </html> </xsl:template> <!-- For references section --> <xsl:template match="references"> <ol> <xsl:apply-templates match="item"/> </ol> </xsl:template> <xsl:template match="references/item"> <li> <xsl:choose> <xsl:when test="@url"> <!-- item tag has url attibute --> <a> <!-- enclose text in <a href="" --> <xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute> <xsl:apply-templates/></a> </xsl:when> <xsl:otherwise> <!-- Otherwise, make text italic --> <i>--<xsl:apply-templates/></i> </xsl:otherwise> </xsl:choose> </li> </xsl:template> <!-- paragraph tag --> <xsl:template match="paragraph"> <p> <xsl:apply-templates/> </p> </xsl:template> <!-- subtitile tag --> <xsl:template match="subtitle"> <h2> <xsl:apply-templates/> </h2> </xsl:template> <!-- Empty tempate: we use values from these tags in other templates --> <xsl:template match="title" /> </xsl:stylesheet> So, when web site visitor asks for page1.xml, XSLT transformation will substitute <page> tag to <html><head<..., set page title and H1 header from <title> tag, transform <paragraph> tag to <p>... — correctly converting page2.xml from pure XML to XHTML. Download whole XSLT engine example site in one zip archive. Check out other articles about XSLT on the net:
|
|