Skip to main content


developerWorks  >  XML | Open source  >

Separate content from presentation with XSLT, SimpleXML, and PHP 5

Take advantage of the PHP XSL and SimpleXML modules

developerWorks

Level: Intermediate

Jake Miles (jacob.miles@gmail.com), Freelance writer and developer, Twistage Inc.

07 Oct 2008

Register now or sign in using your IBM ID and password.

Over the years, developers have devised many strategies and frameworks to facilitate the separation of business logic and presentation logic. In this tutorial, you will explore two solutions to separating data and business logic from presentation logic: one using XSLT through the XSL module in PHP 5 and the other using the SimpleXML module in PHP 5. To do this, you'll use a Web page for a personal resumé stored as an XML file as an example.

In this tutorial

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

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

Objectives

  • Create a small driver script.

  • Implement a separation of content and presentation first with three XSLT stylesheets that work in sequence.

  • Implement the same transformation in PHP using the SimpleXML module.

Prerequisites

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.


System requirements

You will need JavaScript enabled in your browser.

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.


Duration

1 hour





Formats

html, pdf


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!


Back to top


Document options

Document options requiring JavaScript are not displayed


My developerWorks needs you!

Connect to your technical community