PHP  XSL   XSLTProcessor

xsl(extensible stylesheet language) is actually a group of languages that allow you to convert an xml document into another format. the main language within this group is xslt.

what is XSLT

xslt, the extensible stylesheet language for provides a flexible, powerful language for transforming xml document into xhtml document or to other xml document.

some quick point about the xslt language.

  • xslt stands for extensible stylesheet language transformations.
  • xslt provides a flexible, powerful document transforming facility.
  • xslt transforms an xml document into another xhtml/xml document.
  • xslt is a declarative and functional language.
  • xslt has a special namespace of tags, usually indicated by xsl:
  • xslt is an official recommendation of the word wide web consortium(w3c).

this 'stocklist.xml' file use in below example:

     <?xml    version="1.0"    encoding= "utf-8" ?>
               <item  type = "fruit">
                      <description>you want to buy how many apples</description>

php provides you with a class called xsltprocessor that you can use to carry out xslt transformations. in this section you learn how to use it to convert the above 'stocklist.xml' file into a nice, human-readable web page.

example: first step

in this example below, the xslt style sheet that contains the rules for the transformation:

  <xml   version="1.0"   encoding="utf-8" ?>
      version= "1.0"
      xmlns:xsl =""
      xmlns =">
      <xsl:output    method = "html" />
       <xsl:template     match="/stocklist">
<html    xmlns = ""  xml:lang="en"  lang = "en" >
    <head> <title> transforming  xml   to   xhtml</title>
     <link    rel="stylesheet"  type="text/css"  href="common.css" />
       <h1>stock list</h1>
       <ul style="list-style:none;">
       <xsl:apply-templates    select = "item" />
      <xsl:template    match= "item" >
                 <dt>name : </dt>
                  <dd><xsl:value-of     select = "name" /></dd>
                  <dt>price : </dt>     
                  <dd>$<xsl:value-of     select = "price" /></dd>
                  <dt>quantity : </dt>
                  <dd><xsl:value-of     select = "quantity" /></dd>
                  <dt>description : </dt>
                  <dd><xsl:value-of     select = "description" /></dd>

note : save this file 'stockxhtml.xslt' in your document root folder.

"stockxhtml.xml" file code to transform above 'stocklist.xml' file.

second step : now you can use php xsltprocessor class to apply the style sheet to the document, converting it into an easy-to-read xhtml web page. look below code:

$dom =  new   domdocument();  // creates  a new  domdocument  object 
$pro =  new   xsltprocessor();  // this code creates a new xsltprocessor object
$dom->load('.\stockxhtml.xslt');  //load the 'stockxhtml.xslt' file into the $dom object
$pro->importstylesheet($dom);//import the $dom object into the xsltprocessor object
$dom->load( ".\stocklist.xml" );// load the 'stocklist.xml' document into the dom object $dom
echo  $pro->transformtoxml( $dom ); 

save 'xsltprocessor.php' file in your document root folder and visit its url in your web browser.

note : if you get a class 'xsltprocessor' not found error. you need to install or enable the xsl extension.

if you installed all the packages then edit your php.ini file and remove the semicolon from the line - ;extension=php_xsl.dll and restart web server.