Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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

Take advantage of the PHP XSL and SimpleXML modules

Jake Miles (jacob.miles@gmail.com), Freelance writer and developer, Twistage Inc.
Photo of Jacob Miles
Jake Miles is Senior Technical Liason at Twistage, Inc, a young company providing a full-stack video Web solution to businesses. He has experience with many languages and software technologies, has worked as a professional developer for 10 years and been an avid student and tinkerer since he was 10. He also teaches on a volunteer basis, and believes that anyone can learn anything if taught clearly enough.

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

Date:  07 Oct 2008
Level:  Intermediate PDF:  A4 and Letter (173 KB | 28 pages)Get Adobe® Reader®

Activity:  21300 views
Comments:  

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.

About 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

This tutorial will demonstrate two implementations of this separation of concerns.

Frequently used acronyms

  • CSS: Cascading stylesheet
  • HTML: Hypertext Markup Language
  • PHP: PHP Hypertext Preprocessor
  • XML: Extensible Markup Language
  • XSD: XML Schema Definition
  • XSL: Extensible Stylesheet Language
  • XSLT: XSL Transformations

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.


Prerequisites

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.

1 of 8 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Open source
ArticleID=343015
TutorialTitle=Separate content from presentation with XSLT, SimpleXML, and PHP 5
publish-date=10072008
author1-email=jacob.miles@gmail.com
author1-email-cc=dwxed@us.ibm.com