BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
PHP XML
PHP XSLT Function xslt_getopt
PHP XSLT Function xslt_free
PHP XSLT Function xslt_error
PHP XSLT Function xslt_create
PHP SimpleXML Function New_Doc
PHP SimpleXML Function Save
PHP SimpleXML Function load_string
PHP SimpleXML Function Load_File
PHP SimpleXML Function LoadXML
PHP SimpleXML Function Load
PHP SimpleXML Function Open_File
PHP SimpleXML Function xml_parser_create
PHP SimpleXML Create an XML Document
PHP XML RPC Function Server Call method
PHP XML RPC Parse Method Descriptions
PHP XML RPC Function ServerCreate
PHP XML RPC Function GetType
PHP XML RPC Function EncodeRequest
PHP XML RPC Function Encode
PHP XML RPC Function DecodeRequest
 
 

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :269

 

PHP XSLT Function xslt_process

PHP Version: PHP 4 >= 4.0.3

Description

This function performs a XSLT transformation.

Usage:

mixedxslt_process ( resource xh, string xmlcontainer, string xslcontainer [, string resultcontainer [, array arguments [, array parameters]]])

The xslt_process() function is the crux of the new XSLT extension. It allows you to perform an XSLT transformation using almost any type of input source - the containers. This is accomplished through the use of argument buffers -- a concept taken from the Sablotron XSLT processor (currently the only XSLT processor this extension supports). The input containers default to a filename 'containing' the document to be processed. The result container defaults to a filename for the transformed document. If the result container is not specified - i.e. NULL - than the result is returned.

Warning:

This function has changed its arguments, since version 4.0.6. Do NOT provide the actual XML or XSL content as 2nd and 3rd argument, as this will create a segmentation fault, in Sablotron versions up to and including 0.95.

Content of employee.xml:

<?xml version="1.0" encoding="UTF-8"?>

<employee>

     <employeename id="myId">test</employeename>

     <employeeaddress>test123</employeeaddress>

     <SSN>12343456</SSN>

     <company>XYZ Corporation</company>

</employee>

Content of employee.xsl:

<?xml version='1.0'?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<head><title>XSLT in PHP</title></head>

<body>

     <xsl:for-each select="//employee">

          <BR><B>Employee Name:<xsl:value-of select="employeename"/></B></BR>

          <BR><B>Employee Address:<xsl:value-of select="employeeaddress"/></B></BR>

          <BR><B>SSN:<xsl:value-of select="SSN"/></B></BR>

          <BR><B>Company:<xsl:value-of select="company"/></B></BR>

     </xsl:for-each>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

PHP Code:

<?php

//create a new XSLT processor

$xslt = xslt_create();

//initialize the name of xml file to be transformed

$xmlFile = "/xml_files/employee.xml";

//initialize the name of stylesheet to be used for transformation

$xslFile = "/xml_files/employee.xsl";

//set encoding type. This is not required but depends upon your application requirement

xslt_set_encoding($xslt, 'UTF-8');

// Process the document and store the result in a variable

$transformed =  xslt_process($xslt, $xmlFile, $xslFile, 'result.html');

if ($transformed) {

    echo "SUCCESS, employee.xml was transformed by sample.xsl into employee.html";

    echo "<pre>\n";

    readfile('employee.html');

    echo "</pre>\n";

} else {

    echo "Error Transforming employee.xml by applying employee.xsl into employee.html";

    echo "<BR>Error description " . xslt_error($xslt);

    echo "<BR>Error code  " . xslt_errno($xslt);

}

//free the xslt processor.

xslt_free($xslt);

?>

Output

Employee Name:test
Employee Address:test123
SSN:12343456
Company:XYZ Corporation

The above example produces a html file but sometimes especially for web development, it is required to print out the result directly into the web browser. So below example shows hot to use xslt_process() function to produce result as a variable.

// Process the document, returning the result into the $result variable

$result = xslt_process($xh, 'employee.xml', 'employee.xsl');

Now you can use $result to send the result to the client browser.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C
Sr. Software Engineer

View all Jobs (Add yours)
View all CV (Add yours)



reservationless conference calls
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses
efax


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP