Before you start
This tutorial is for Web application developers looking to separate data and business logic from presentation logic. PHP experience, knowledge of XML in general, and familiarity with XSLT are all necessary requirements.
Most Web applications take user data, translate it into a form storable in a database, and then take that database data and translate it into a Web page. When creating a Web page from data, one approach is to create a single PHP page that acts as a template for the Web page, which contains all of the database queries and other logic necessary to restructure the data into a form usable by the page. One problem with this approach, however, is that it mixes concerns together:
- One concern is that of the Web page designer, creating the layout of the page
- Another is of the developer extracting the data from the database
- A third concern is the intermediate restructuring of the data to make it more easily used in the page
Separation of concerns refers to extracting the code into various layers:
- A layer to deal with the database
- A layer to manipulate the data
- A presentation layer creating the user interface
This tutorial will demonstrate two implementations of this separation of concerns.
Separation of concerns is fundamental to well-structured code, because it encapsulates responsibilities and dependencies. For this reason, over the years, developers have devised many strategies and frameworks designed to facilitate or even enforce the separation of business logic and presentation logic, since the two often vary independently of each other. A sequence of XSLT stylesheets makes this separation quite clear, because you can split up the translation of data from its persisted form to a visual presentation into discrete phases as the XML is transformed by one stylesheet and then the next. However, XSLT syntax and methodology, a declarative paradigm, is not the norm for most programmers who use procedural languages. The SimpleXML module in PHP is a procedural answer to XSLT, that lets you traverse an XML document as a standard PHP object.
In this tutorial you will implement a Web page for a personal resumé stored as an XML file and explore two solutions to separating data and business logic from presentation logic:
- One using XSLT through the XSL module in PHP 5
- The other uses the SimpleXML module in PHP 5
First, you'll create a small driver script, and then you'll implement a separation of content and presentation first by implementing three XSLT stylesheets that work in sequence, and finally by implementing the same transformation in PHP using the SimpleXML module. Although the fundamental mechanism underlying these two technologies (XSLT and PHP) is quite different, the resulting code will have a remarkably similar structure between the two implementations.
Although most data is stored in a relational database these days, for a small data set that changes infrequently and doesn't require record locking, like a personal resumé, XML is an ideal format that allows for easy additions. In this tutorial you'll explore XSLT and SimpleXML in rendering an XML resumé document as an HTML Web page.
You'll need the following tools in this tutorial:
- This tutorial uses PHP version 5.2.6.
- SimpleXML is available in all versions of PHP 5.0 or later and is enabled by default.
- PHP 5 includes the XSL extension by default. To enable it, you can add the argument
--with-xsl[=DIR]to your configure line. For more information on how to install and configure the XSL extension, see http://us.php.net/manual/en/xsl.setup.php.
- An XML editor.