Using PHP 5s XSL Extension to Perform XSL Transformations
By Tony Marston2005-04-19
XSL Include Files
It is possible to store common templates in separate files and incorporate them into your stylesheet at runtime by means of the <xsl:include> command. This is similar to calling a subroutine in other programming languages. Here are listings and explanations of the include files used in this example.
std.pagination.xsl
<?xml version='1.0'?>$curpage, $lastpage and $script are parameters defined with the <xsl:param> tag, and which can have values supplied at runtime during the XSL transformation process. Access to these values allows this template to output the following:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
//*****************************************************************************
// Copyright 2003-2004 by A J Marston <tony@marston-home.demon.co.uk>
// Distributed under the GNU General Public Licence
//*****************************************************************************
-->
<xsl:template name="pagination">
<table border="0" class="pagination">
<tr class="pagination">
<xsl:choose>
<xsl:when test="$curpage<=1">
<!-- we are on page 1, so there is no navigation backwards -->
<td class="pagination">FIRST</td>
<td class="pagination">PREV</td>
</xsl:when>
<xsl:otherwise>
<!-- insert links for first/previous page -->
<td class="pagination"><a href="{$script}?page=1"><b>FIRST</b></a></td>
<td class="pagination"><a href="{$script}?page={$curpage -1}"><b>PREV</b></a></td>
</xsl:otherwise>
</xsl:choose>
<!-- insert "page x of y" -->
<td class="pagination">
<xsl:text>Page </xsl:text>
<xsl:value-of select="$curpage"/>
<xsl:text> of </xsl:text>
<xsl:value-of select="$lastpage"/>
</td>
<xsl:choose>
<xsl:when test="$curpage=$lastpage">
<!-- we are on the last page, so there is no navigation forwards -->
<td class="pagination">NEXT</td>
<td class="pagination">LAST</td>
</xsl:when>
<xsl:otherwise>
<!-- insert links for last/next page -->
<td class="pagination"><a href="{$script}?page={$curpage +1}"><b>NEXT</b></a></td>
<td class="pagination"><a href="{$script}?page={$lastpage}"><b>LAST</b></a></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
* If we are currently beyond the first page then hyperlinks will be generated to go back to the FIRST and PREVious pages. If we are currently on the first page then there will be no hyperlinks, just plain text.
* We can show the user the current page number and the total number of pages.
* If we are currently before the last page then hyperlinks will be generated to go forwards to the NEXT and LAST pages. If we are currently on the last page then there will be no hyperlinks, just plain text.
std.actionbar.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
//*****************************************************************************
// Copyright 2003-2004 by A J Marston <tony@marston-home.demon.co.uk>
// Distributed under the GNU General Public Licence
//*****************************************************************************
-->
<xsl:template name="actbar">
<table border="0" class="actionbar">
<tr class="actionbar">
<xsl:for-each select="//actbar/*">
<!-- create a button for each element within actionbar -->
<td class="actionbar">
<input type="submit" name="{@id}" value="{node()}" />
</td>
</xsl:for-each>
</tr>
</table>
<p class="script_time">page loaded in <xsl:value-of select="$script_time"/> seconds</p>
</xsl:template>
</xsl:stylesheet>
| <xsl:for-each> | This is similar to PHP's foreach function. The " //actbar/* " directive will select all available children of the actbar node. |
| (@id) | This will output the contents of the attribute with the name 'id'. |
| {node()} | This will output the value from the current node. |
| $script_time | This is one of the parameters that were specified during the transformation process. |
This method means that the buttons on the action bar are not hard coded within the XSL script but specified within the XML data. Thus the process which generates the XML data can vary the action buttons depending on its own internal logic.
Tutorial Pages:
» Intended Audience
» Prerequisites
» A Sample XML File
» XML File Contents
» A Sample XSL File
» XSL File Contents
» XSL Include Files
» Performing the XSL Transformation
» Sample Output
» References
