By Nabil ADOUI, member of the 4D Technical Support team

Similar documents
XSL - Introduction and guided tour

Web Server Logs Analyze Using the XML Technologies

PHP Oracle Web Development Data Processing, Security, Caching, XML, Web Services and AJAX

Data Integration through XML/XSLT. Presenter: Xin Gu

XML WEB TECHNOLOGIES

Interactive, dynamic Scalable Vector Graphics

T XML in 2 lessons! %! " #$& $ "#& ) ' */,: -.,0+(. ". "'- (. 1

Extending XSLT with Java and C#

Introduction to XML Applications

Chapter 3: XML Namespaces

10CS73:Web Programming

Managing XML Documents Versions and Upgrades with XSLT

Thursday, February 7, DOM via PHP

XSL Elements. xsl:copy-of

XML Processing and Web Services. Chapter 17

XML for RPG Programmers: An Introduction

WIRIS quizzes web services Getting started with PHP and Java

Overview of DatadiagramML

XSLT Mapping in SAP PI 7.1

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

Concrete uses of XML in software development and data analysis.

JBoss Portlet Container. User Guide. Release 2.0

Electronic Bank Account Management - EBAM

FileMaker Server 9. Custom Web Publishing with PHP

San Joaquin County Office of Education Career & Technical Education Web Design ~ Course Outline CBEDS#: 4601

Building A Very Simple Website

Localizing dynamic websites created from open source content management systems

Web Development. Owen Sacco. ICS2205/ICS2230 Web Intelligence

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

WTP-101 Developing Web Applications with Standards. using W3C org standard technologies such as, HTML, CSS, XML, XSD and XSL

A DIAGRAM APPROACH TO AUTOMATIC GENERATION OF JSP/SERVLET WEB APPLICATIONS

Short notes on webpage programming languages

An Introduction to Developing ez Publish Extensions

PeopleSoft Compare Process

Terms and Definitions for CMS Administrators, Architects, and Developers

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Web Services Technologies

PHP and XML. Brian J. Stafford, Mark McIntyre and Fraser Gallop

Google Web Toolkit. Progetto di Applicazioni Software a.a. 2011/12. Massimo Mecella

WTP-101 Developing Rich Internet Applications with Java Web Frameworks

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Developing Web Views for VMware vcenter Orchestrator

CSET 3100 Advanced Website Design (3 semester credit hours) IT Required

DTD Tutorial. About the tutorial. Tutorial

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 6 Professional Graduate Diploma in IT WEB ENGINEERING

A Form-based Approach for Application Development By Web Service Integration

Data Exchange with. 2012, Business-DNA Solutions GmbH 1

Web Container Components Servlet JSP Tag Libraries

ONTOLOGY-BASED MULTIMEDIA AUTHORING AND INTERFACING TOOLS 3 rd Hellenic Conference on Artificial Intelligence, Samos, Greece, 5-8 May 2004

Chapter 2: Interactive Web Applications

ACE: Dreamweaver CC Exam Guide

PL/JSON Reference Guide (version 1.0.4)

An XML Based Data Exchange Model for Power System Studies

White Paper Using PHP Site Assistant to create sites for mobile devices

Software documentation systems

XML and Tools. Muhammad Khalid Sohail Khan Mat #: University of Duisburg Essen Germany

.NET Best Practices Part 1 Master Pages Setup. Version 2.0

JobScheduler Web Services Executing JobScheduler commands

How To Use Dreamweaver With Your Computer Or Your Computer (Or Your Computer) Or Your Phone Or Tablet (Or A Computer)

Firewall Builder Architecture Overview

HOW TO CREATE THEME IN MAGENTO 2

Building A Very Simple Web Site

Calculator Notes for the TI-Nspire and TI-Nspire CAS

CONFIGURING A WEB SERVER AND TESTING WEBSPEED

Visualizing ECL Results Technical Preview

INTRO TO XMLSPY (IXS)

ERIE COMMUNITY COLLEGE COURSE OUTLINE A. COURSE NUMBER CS WEB DEVELOPMENT & PROGRAMMING I AND TITLE:

An Oracle White Paper May Creating Custom PDF Reports with Oracle Application Express and the APEX Listener

Standard Registry Development and Publication Process

Exploiting XML Digital Signature Implementations

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Sitecore Dashboard User Guide

JJY s Joomla 1.5 Template Design Tutorial:

Visual Basic Programming. An Introduction

General principles and architecture of Adlib and Adlib API. Petra Otten Manager Customer Support

Documentum Developer Program

MICROSOFT ACCESS 2003 TUTORIAL

XML- New meta language in e-business

Introduction to Web Development

Last Week. XML (extensible Markup Language) HTML Deficiencies. XML Advantages. Syntax of XML DHTML. Applets. Modifying DOM Event bubbling

XML and the College Website A Practical Look at the Use of XML and XSL

Drupal CMS for marketing sites

Here is a quick diagram of the ULV SSO/Sync Application. Number 3 is what we deal with in this document.

REDUCING THE COST OF GROUND SYSTEM DEVELOPMENT AND MISSION OPERATIONS USING AUTOMATED XML TECHNOLOGIES. Jesse Wright Jet Propulsion Laboratory,

Whitepapers at Amikelive.com

Address Phone & Fax Internet

Java Server Pages combined with servlets in action. Generals. Java Servlets

Schematron Validation and Guidance

Transcription:

XSLT with PHP By Nabil ADOUI, member of the 4D Technical Support team

Contents Summary... 3 Introduction... 3 Important elements... 3 The PHP XSL library... 4 The PHP XSL API... 5 XSLTProcessor:: construct... 5 XSLTProcessor::setParameter... 5 XSLTProcessor::importStylesheet... 6 XSLTProcessor::transformToXML... 6 XSLTProcessor::transformToURI... 6 Demonstration... 7 Error management... 11 Conclusion... 11

Summary In this technical note, we are going to cover the XSL(PHP) extension used to perform XSL Transformations. This extension will replace these 4D commands: XSLT APPLY TRANSFORMATION, XSLT SET PARAMETER and XSLT GET ERROR. Introduction XSLT is a standard defined by W3C. This language transforms an XML file to another type of file (XML, HTML or any other type recognized by the browser) in order to give a more meaningful visual presentation to the original document. Important elements An XSL Transformation receives two elements as input: The XML document that you want to transform An XSL style sheet (XML style sheets). This is an XML file describing the presentation of the contents of our document. It must be declared as follows: <? xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transfor m" version="1.0">... </xsl:stylesheet> Both these files will be used by an XSLT processor based on the PHP XSL library that will replace the XALAN C++ processor currently used by 4D. The result is an XML document that includes the contents of the original document represented according to the specified style sheet.

The PHP XSL library PHP XSL is an implementation of the XSLT standard that uses the libxslt library (XSLT C library for GNOME). It contains a single XSLTProcessor class with a dozen or so methods shown below: XSLTProcessor XSLTProcessor class XSLTProcessor:: construct Creates a new XSLTProcessor object XSLTProcessor::getParameter Returns the value of the parameter XsltProcessor::getSecurityPrefs Returns the security preferences XSLTProcessor::hasExsltSupport Determines whether PHP supports XSLT XSLTProcessor::importStylesheet Imports style sheet XSLTProcessor::registerPHPFunctions Allows the use of PHP functions as XSLT functions XSLTProcessor::removeParameter Deletes a parameter XSLTProcessor::setParameter Sets the value of a parameter XSLTProcessor::setProfiling Sets an output file for a profile XsltProcessor::setSecurityPrefs Sets the security parameters XSLTProcessor::transformToDoc Transforms to a DOM document XSLTProcessor::transformToUri Transforms to a URI XSLTProcessor::transformToXML Transforms to an XML In the remainder of this technical note, we re going to focus on just 5 of these methods: XSLTProcessor:: construct XSLTProcessor::setParameter XSLTProcessor::importStylesheet XSLTProcessor::transformToXML XSLTProcessor::transformToURI

The PHP XSL API XSLTProcessor:: construct This method is simply the constructor of the XSLTProcessor class. It can be used to create a new XSLTProcess type object. It does not accept any parameters or return any values. Signature: XSLTProcessor:: construct ( void) Example call: $xsl = new XSLTProcessor; XSLTProcessor::setParameter This method saves one or more parameters to be used in the transformations that follow it. You can call this parameter in your XSL file using the «$» symbol followed by the parameter name. Signatures: bool XSLTProcessor::setParameter ( string $namespace, string $name, string $value) bool XSLTProcessor::setParameter ( string $namespace, array $options ) $namespace: URI namespace of the XSLT parameter, $name: local name of XSLT parameter $value: new value of XSLT parameter $options: array of name => value pairs Example call: $xsl = new XSLTProcessor(); $xsl->setparameter('', 'param1','value1'); You can also pass an array of parameters:

$params['param1'] = 'value1' ; $params['param2'] = 'value2' ; $xsl->etparameter('http://www.w3.org/1999/xsl/transform', $params); XSLTProcessor::importStylesheet This method imports the style sheet represented by the XSL file that we want to apply during the transformation. Signature: void XSLTProcessor::importStylesheet ( object $stylesheet ) Example: $xsl = new DOMDocument; $xsl->load('xslt2.xsl'); $proc = new XSLTProcessor; $proc->importstylesheet($xsl); XSLTProcessor::transformToXML TransformToXML converts the source document to a text by applying the XSL file imported using the importstylesheet method. Signature: string XSLTProcessor::transformToXML ( DOMDocument $doc ) XSLTProcessor::transformToURI This method transforms the source document to a file that will be saved to the URI specified as a parameter of the method, after applying the chosen style sheet. Signature: int XSLTProcessor::transformToURI ( DOMDocument $doc, string $uri )

Demonstration For our example, we will use the following XML document containing song/album titles and performers, as well as the years they were released: <?xml version="1.0" encoding="utf-8"?> <catalog> <title>empire Burlesque</title> <performer>bob Dylan</performer> <year>1985</year> <title>hide your heart</title> <performer>bonnie Tyler</performer> <year>1988</year> <title>greatest Hits</title> <performer>dolly Parton</performer> <year>1982</year> <title>still got the blues</title> <performer>gary Moore</performer> <year>1990</year> <title>eros</title> <performer>eros Ramazzotti</performer> <year>1997</year> <title>one night only</title> <performer>bee Gees</performer> <year>1998</year> <title>sylvias Mother</title> <performer>dr.hook</performer> <year>1973</year> <title>maggie May</title> <performer>rod Stewart</performer> <year>1990</year> <title>romanza</title> <performer>andrea Bocelli</performer> <year>1996</year> </catalog>

Next we are going to apply an XSL transformation with PHP XSL based on the following style sheet: <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>my collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>performer</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <xsl:choose> <xsl:when test="year > $param1"> <td bgcolor="red"><xsl:value-of select="title" /></td> <td bgcolor="red"><xsl:value-of select="performer" /></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="title" /></td> <td><xsl:value-of select="performer" /></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> This style sheet will group the data of the XML file into a two-column array. To make the task a bit more interesting, we re going to highlight rows where the release year is higher than the ($param1) parameter that we will provide in our PHP code.

So here is our PHP code: <?php $xml = new DOMDocument; $xml->load('xml_file.xml'); // Loading XML document $xsl = new DOMDocument; $xsl->load('xsl_file.xsl'); // Loading style sheet $proc = new XSLTProcessor; // Creating XSLT processor $proc->importstylesheet($xsl); // Attaching XSL rules $proc->setparameter('','param1','1996'); // Inserting $param1 parameter echo $proc->transformtoxml($xml); // Applying transformation?> In this example, we assume that the XML and XSL files are located in the same directory as the source PHP file; otherwise, we would have to provide the complete pathname. On the 4D side, we ll execute this PHP code using the following commands: $filepath:=get 4D folder (Current resources folder)+"php.php" $isok:=php Execute($filePath;"";$res) PHP GET FULL RESPONSE (stdout;errlabels;errvalues;httpheaderfields;httpheadervalues) If ($isok) vhtml:=$res End if The result of the transformation will be saved in the vhtml variable.

After this 4D method is executed, vhtml will contain the following HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd">\n<html><body>\n<h2>my collection</h2>\n<table border="1">\n<tr bgcolor="#9acd32">\n<th>title</th>\n<th>performer</th>\n</tr>\n<tr>\n<td>empire Burlesque</td>\n<td>Bob Dylan</td>\n</tr>\n<tr>\n<td>Hide your heart</td>\n<td>bonnie Tyler</td>\n</tr>\n<tr>\n<td>Greatest Hits</td>\n<td>Dolly Parton</td>\n</tr>\n<tr>\n<td>Still got the blues</td>\n<td>gary Moore</td>\n</tr>\n<tr>\n<td bgcolor="red">eros</td>\n<td bgcolor="red">eros Ramazzotti</td>\n</tr>\n<tr>\n<td bgcolor="red">one night only</td>\n<td bgcolor="red">bee Gees</td>\n</tr>\n<tr>\n<td>Sylvias Mother</td>\n<td>Dr.Hook</td>\n</tr>\n<tr>\n<td>Maggie May</td>\n<td>Rod Stewart</td>\n</tr>\n<tr>\n<td>Romanza</td>\n<td>Andrea Autrement, vous pouvez utiliser la méthode XSLTProcessor::transformToURI de la manière suivante ; <?php $xml = new DOMDocument; $xml->load('xml_file.xml'); $xsl = new DOMDocument; $xsl->load('xsl_file.xsl'); $proc = new XSLTProcessor; $proc->importstylesheet($xsl); $proc->setparameter('','param1','1996'); echo $proc->transformtouri($xml,'out.html');?> The result will be saved in the «out.html» file next to the PHP code file; however, you can provide another path if you want.

Here is a preview of the result: Error management If an error occurs during the transformation, the error message will be copied into the errvalues variable that was passed in the PHP GET FULL RESPONSE command. Conclusion The PHP XSL library is an excellent tool which can replace the XSLT APPLY TRANSFORMATION, XSLT SET PARAMETER and XSLT GET ERROR commands after their removal. This library provides a rich API allowing you to carry out all the operations necessary for your XSL transformations.