SlideShare a Scribd company logo
XML transformations with PHP PHPCon 2003 East April 25 th  2003, New York City, USA Stephan Schmidt
Agenda About the speaker XML transformations Drawbacks of XSLT Transforming XML with PHP Installation patXMLRenderer Adding fun by overloading XML namespaces Design and usage of extensions PEAR::XML_Transformer XSLT vs PHP Transformers
Stephan Schmidt Web Application Developer at Metrix Internet Design GmbH in Karlsruhe/Germany Programming since 1988, PHP since 1998 Publishing OS on  http://www.php-tools.net Contributor to the German PHP Magazine Regular speaker at conferences Maintainer of patXMLRenderer, patTemplate, patUser and others
XML transformations Data is stored in an XML document Needed in different formats and environments Other XML formats (DocBook, SVG, …) HTML Plain text LaTeX Anything else you can imagine Content remains the same
Transform XML to XML Source document: <example title= „My Example“ > <greeting> Hello  <imp> world </imp> ! <greeting> </example> Result of transformation into a different XML format: <document> <title> My Example </title> <section> <para>   Hello  <mark type= „important“ > world </mark> ! </para> </section> </document>
Transform XML to HTML Result of transformation to HTML: <html> <head> <title> My Example </title> </head> <body> <h1> Hello  <b> world </b></h1> </body> </html>
Transform XML to plain text Result of transformation to plain text: My Example ------------- Hello W O R L D !
Transform XML to LaTeX Result of transformation to LaTeX \documentclass{ article } \title{ My Example } \begin{ document } Hello  {\em   World } ! \end{ d ocument }
Introduction to XSLT XSLT has been developed for the task of transforming XML documents XSLT stylesheets are XML documents Transforms XML trees that are stored in memory Uses XPath to access parts of a document Based on pattern matching (“When see you something that looks like this, do that…”) “ awk of the XML universe” May only append to its output Functional syntax
„ Hello World“ Example (XML) Sample Document: <?xml version=&quot;1.0&quot;?> <greetings> <greeting> Hello World! </greeting> </greetings>
„ Hello World“ Example (XSL) <xsl:stylesheet xmlns:xsl= &quot;http://www.w3.org/1999/XSL/Transform&quot;  version= &quot;1.0&quot; > <xsl:output method= &quot;html&quot; /> <xsl:template match= &quot;/&quot; > <html> <body> <xsl:apply-templates select= &quot;greetings/greeting&quot; /> </body> </html> </xsl:template> <xsl:template match= &quot;greeting&quot; > <h1> <xsl:value-of select= &quot;.&quot; /> </h1> </xsl:template> </xsl:stylesheet>
„ Hello World“ Example (Output)
Drawbacks of XSLT XSLT is domain specific: Developed to work with XML Creating plain text/LaTeX is quite hard, as whitespace is important ( <xslt:text> ) Transforming “world” to “W O R L D” is next to impossible
Drawbacks of XSLT XSLT is verbose and circumstantial: <xsl:choose> <xsl:when test= &quot;@author&quot; > <xsl:value-of select= &quot;@author&quot; /> <xsl:text>  says:  </xsl:text> <xsl:value-of select= &quot;.&quot; /> </xsl:when> <xsl:otherwise> <xsl:text> Somebody says:  </xsl:text> <xsl:value-of select= &quot;.&quot; /> </xsl:otherwise> </xsl:choose>
Drawbacks of XSLT XSLT is hard to learn: Functional programming language Complex structure (see “if/else” example”) XPath is needed Designer needs to learn it
Drawbacks of XSLT XSLT is not up to the task of creating up-to-date websites: Not able to re-transform its own output XSL stylesheet is not able to manipulate itself  Not able to query databases , include files or even the current date (even LaTeX is able to do this with “ \today ”) So, why should I use XSLT?
Why use XSLT? You should use XSLT when… … you are transforming XML to XML or XHTML. … you need a portable application. … want to build a W3C compliant website. … you have too much free time and like hurting yourself Otherwise, you should do the transformation on your own.
Transforming XML using PHP Transforming an XML document is easy: Define a transformation rule for each tag Start at the root element Traverse the document recursively Insert the transformation result to the parent tag Go home early as you have completed the task faster than with XSLT.
Creating transformation rules Rules are simple: “ When you see this, replace it with that.” Implemented in PHP using templates Attributes of the tag are used as template variables PCData of the tag is used as template variable “CONTENT”
Example <section title= &quot;XML Transformation&quot; > <para> XML may be transformed using PHP. </para> </section> XML Template for  <section> Template for  <para> <table border= &quot;0&quot;  cellpadding= &quot;0&quot;  cellspacing= &quot;2&quot;  width= &quot;500&quot; > <tr><td><b> {TITLE} </b></td><tr> <tr><td> {CONTENT} </td></tr> </table> <font face= &quot; Arial &quot;  size= &quot; 2 &quot; > {CONTENT} <br> </font>
Example (Result) <table border= &quot;0&quot;  cellpadding= &quot;0&quot;  cellspacing= &quot;2&quot;  width= &quot;500&quot; > <tr><td><b> XML Transformation </b></td><tr> <tr><td> <font face= &quot; Arial &quot;  size= &quot; 2 &quot; > XML may be transformed using PHP <br > </ font> </td></tr> </table>
Implementing the transformer Use a template class to “implement the rules”: Create a template for each tag Include placeholders for attributes and content of the tag Make use of the internal SAX parser: Simple callbacks for events (start tag, end tag, cdata) Traverses the document recursively Template is parsed when closing tag is found Result is appended to the content of the parent tag
Don’t reinvent the wheel There already are XML transformers available for PHP: patXMLRenderer http://www.php-tools.net PEAR::XML_Transformer http:// pear.php.net phpTagLib http://chocobot.d2g.com
Installation of patXMLRenderer Download archive at  http://www.php-tools.de Unzip the archive Adjust all path names and options in the config file (cache, log, etc.) Create the templates (transformation rules) Create your XML files Let patXMLRenderer transform the files Finished: » It’s mere child’s play «
Using patXMLRenderer $randy  =  new  patXMLRenderer; // instantiate renderer $randy ->setExtensionDir(  $extDir  );  // set directory for extensions $randy ->setKnownExtensions(  $knownExtensions  );  // set list of extensions $randy ->setSkins(  $skins  ); // set list of available skins $randy ->selectSkin(  &quot;blue&quot;  ); // select one skin $randy ->setOption(  &quot;autoadd&quot; ,  &quot;on&quot;  ); // enable auto add for extensions $randy ->setOption(  &quot;cache&quot; ,  &quot;auto&quot;  ); // enable caching $randy ->setXMLDir(  $xmlDir  ); // set directory for all xml files $randy ->setXMLFile(  $file  ); // file to parse $template  =  new  patTemplate(); // template object $randy ->setTemplate(  $template  ); foreach (  $baseTemplates  as   $tmplFile  ) // load templates $randy ->addTemplateFile(  $tmplFile  ); $randy ->initRenderer(); // do some init routine $randy ->displayRenderedContent(); // transform and display (echo) result
Introduction to patTemplate PHP templating class published under LGPL Placeholder for variables have to be UPPERCASE and enclosed in { and } Uses  <patTemplate:tmpl name=&quot;...&quot;>  tags to split files into template blocks that may be addressed seperately Other Properties of the templates are written down as attributes, e.g: type=&quot;condition&quot; or whitespace=&quot;trim&quot; Emulation of simple switch/case and if/else statement by using  <patTemplate:sub condition=&quot;...&quot;>  tags
patTemplate Example simple Template with two variables (Corresponds to the XML tag <box>) <patTemplate:tmpl name=&quot;box&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;{WIDTH}&quot;> <tr> <td> {CONTENT} </td> </tr> </table> </patTemplate:tmpl>
patTemplate Example 2 Task: Box should be available in three sizes: “small”, “large” and “medium” (default) Solution: Condition Template to emulate a switch/case statment: Template type is &quot;condition&quot; Variable that should be checked is called &quot;size&quot; Three possible values for &quot;size&quot;: &quot;small&quot;, &quot;large&quot; and &quot;medium&quot; (or any other unknown value) » three Subtemplates.
patTemplate Example 2 <patTemplate:tmpl name=&quot;box&quot; type=&quot;condition&quot; conditionvar=&quot;size&quot;> <patTemplate:sub condition=&quot;small&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;200&quot;>   <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> <patTemplate:sub condition=&quot;large&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;800&quot;>   <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> <patTemplate:sub condition=&quot;default&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;500&quot;>   <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> </patTemplate:tmpl>
Adding fun Simple transformation is easy, but patXMLRenderer can do  more: Callbacks for events (start tag, end tag, data) can be dispatched to classes (dubbed “extensions”) that are responsible for the transformation of the tag. Extensions may access any PHP function Extensions may return XML that is still transformed
Overloading namespaces Extensions are added by overloading namespaces: On each event patXMLRenderer checks whether the current tag has a namespace patXMLRenderer checks whether an extension for the namespace has been defined patXMLRenderer instantiates the extension class (if not done already) patXMLRenderer calls the appropriate method of the class (startElement, endElement, characterData) Extension returns transformed tag and patXMLRenderer appends it to parent tag (or transforms it again)
Simple Example <example> Today is  <time:current format= “m-d-Y” /> . </example> Will be transformed to… <example> Today is 04-25-2004. </example> … Which will then be transformed using the rules defined in  the templates.
Architecture of patXMLRenderer RENDERER HTML / XML /LaTEX /etc... Any new extension XML TEMPLATES Time: Extension Dbc: Extension Ext. Entities
patXMLRenderer Example <page> <dbc:connection name= &quot;foo&quot; > <dbc:type> mysql </dbc:type> <dbc:host> localhost </dbc:host> <dbc:db> myDb </dbc:db> <dbc:user> me </dbc:user> <dbc:pass> secret </dbc:pass> </dbc:connection> ...place any XML code here... <dbc:query connection= &quot;foo&quot;  returntype= &quot;assoc&quot; > SELECT id,name,email FROM authors   WHERE id= <var:get scope= &quot;_GET&quot;  var= &quot;uid&quot; /> </dbc:query> <page>
patXMLRenderer Example (Result) <page> ...any static XML... <result> <row> <id> 1 </id> <name> Stephan </name> <email> [email_address] </email> </row> </result> </page>
patXMLRenderer example <control:switch>  <control:expression> <var:get scope= &quot;HTTP_GET_VARS&quot;  var= &quot;agree&quot; /> </control:expression>  <control:case>  <control:expression> yes </control:expression>  <control:body>  <para> Oh, It's nice that you agree! </para> </control:body>  </control:case>  <control:case> <control:expression> no </control:expression> <control:body>   <para> Mmh, maybe we should work harder to convince you! </para> </control:body> </control:case>  <control:default> <control:body>   <para> You should set the value to 'yes' or 'no'. I don't think that's too hard! </para> </control:body>  </control:default>  </control:switch>
Existing Extensions Repository on  http://www.php-tools.net Documentation is generated automatically Examples: <xml:...>  for XML highlighting <dbc:...>  database interface <var:...>  access to variables <control:...>  control structures <randy:...>  XML-API for patXMLRenderer <formgen:...>  for creating (HTML) forms <rss:...>  to include content from RSS feeds <file:...>  file operations and many more...
Getting bored? Let’s take a look at some real-life examples…
Creating new extensions Creating new extensions is easy: Create a new folder in the extensions directory Create a new PHP file containing the extension class Create a new extension class by deriving it from the “patXMLRendererExtension” class Implement at least a handler for the endElement Create a new XML file that contains documentation about your extension Open the administration interface of patXMLRenderer and add the extension
The extension class c lass   patXMLRendererPHPConExtension  extends  patXMLRendererExtension { var     $name   =  &quot;patXMLPHPConExtension&quot; ; var   $version   =  &quot;0.1&quot; ; var   $cacheAble   =   array ( &quot;RATE&quot;   => true ); }
The endElement handler f unction   endElement(  $parser ,  $ns ,  $tag  ) { $data   =   $this-> getData(); $tag   =   array_pop (  $this-> tagStack ); $attributes   =   array_pop (  $this-> attStack ); switch (  $tag  ) { case   &quot;RATE&quot; : return array(   &quot;data&quot;   =>   &quot;I think PHPCon 2003 is &quot;  .   $data ,   &quot;containsMarkup&quot;  =>  false ); break ; } }
The XML file <configuration> <!-- information about the extension --> <path name= &quot;extension&quot; > <configValue type= &quot;string&quot;  name= &quot;namespace&quot; > phpcon </configValue> <configValue type= &quot;string&quot;  name= &quot;name&quot; > PHPCon Extension </configValue> <configValue type= &quot;string&quot;  name= &quot;class&quot; > patXMLRendererPHPConExtension </configValue> <configValue type= &quot;float&quot;  name= &quot;version&quot; > 0.1 </configValue> <configValue type= &quot;string&quot;  name= &quot;requiredRandyVersion&quot; > 0.6 </configValue> <configValue type= &quot;string&quot;  name= &quot;description&quot; >   Just an example for the PHPCon 2003 East in NYC </configValue> </path> <!-- information about the author --> <path name= &quot;author&quot; > <configValue type= &quot;string&quot;  name= &quot;name&quot; > Stephan Schmidt </configValue> <configValue type= &quot;string&quot;  name= &quot;email&quot; > [email_address] </configValue> </path> <!-- documentation of the tags would follow here --> </configuration>
patXMLRenderer features More features of patXMLRenderer include: Automated, intelligent caching Use of external entities (or xinc) to include files Administration interface <?PHP  to include PHP code in XML files Parameters in XML attributes Offline Generator, generates static HTML pages
PEAR::XML_Transformer Developed by Kristian Köhntopp and Sebastian Bergmann Only used to overload namespaces or tags with Objects or functions No usage of a templating class, transformation from XML to HTML has to be done in PHP classes Existing Namespace handlers Simple image generation DocBook to HTML transformation Widget, to create HTML Widgets PHP, to define your own tags or eval PHP code
XML_Transformer example <html> <body> <img:gtextdefault bgcolor= &quot;#888888&quot;  fgcolor= &quot;#000000&quot;  font= &quot;cour.ttf&quot;  fontsize= &quot;32&quot;  border= &quot;1&quot;  spacing= &quot;2&quot;  cachable= &quot;yes&quot; /> <img:gtext> My Example </img:gtext> </body> </html> »  Same syntax and structure as patXMLRenderer
Comparison - -  Parameters Only with PHP code -  „ Intelligent“ Templates - All in one package  Ext. Repository - -  Administration  -  <?PHP ?> - -  External Entities - Yes, infinite Yes, infinite Recursion -   Namespaces - Yes, objects and functions Yes, only object Dynamic Content (Callbacks) yes, plain HTML - yes, patTemplate Templates phpTagLib PEAR patXMLRenderer
XSLT vs PHP Transformers Pro XSLT: W3C Standard XPath allows restructuring of the document, e.g. creation of a table of contents Portable (not limited to servers with PHP) Pro PHP Transformers: Easier to learn (designers are familiar with templates) Easy to extend Complete control during the whole transformation Result may be transformed again (recursive transformation)
Drawbacks of PHP Transformers Biggest disadvantages of PHP Transformers:   Only the current tag is accessible As a SAX parser is used, no restructuring or reordering is possible. Table of contents can not be generated Cross references are not possible Sorting is not possible Possible solutions: Two pass transformations (like LaTeX) Combination of XSLT and PHP Transformers
Combining forces Combining XSLT and patXMLRenderer is possible by using  the XSLT extension of patXMLRenderer: Uses XSLT to transform The whole document Parts of the document May be combined with all of patXMLRenderers features: inclusion of any dynamic content, control structures, caching, etc. Result of XSLT transformation may be transformed using templates.
patXMLRenderer and XSLT example <xslt:transform returntype= &quot;xml&quot; > <xslt:stylesheet type= &quot;file&quot; > extensions/xslt/xsl/example.xsl </xslt:stylesheet> <xslt:param name= &quot;displayToc&quot; >yes</xslt:param> <sections> <section title= &quot;Dummy Section 1&quot; > <para> This is just a dummy.... </para> </section> <section title= &quot;Dummy Section 2&quot; > <para> Another Section… </para> </section> </sections> </xslt:transform>
XSLT Stylesheet <xsl:stylesheet xmlns:xsl= &quot;http://www.w3.org/1999/XSL/Transform&quot;  version= &quot;1.0&quot; > <xsl:output method= &quot;xml&quot; /> <xsl:param name= &quot;displayToc&quot; /> <xsl:template match= &quot;/&quot; > <xsl:if test= &quot;$displayToc = 'yes'&quot; > <section title=&quot;Table of contents&quot;> <ol> <!-- Create TOC --> <xsl:for-each select= &quot;//section&quot; > <oli> <xsl:value-of select= &quot;@title&quot; /> </oli> </xsl:for-each> </ol> </section> </xsl:if> <!-- Display contents --> <xsl:for-each select= &quot;*|text()&quot; > <xsl:apply-templates select= &quot;*|text()&quot; /> </xsl:for-each> </xsl:template> <!-- xsl template to insert original content has been left out --> </xsl:stylesheet>
The End Thank you! More information: http://www.php-tools.net [email_address] Thanks to: Sebastian Mordziol, Gerd Schaufelberger,  Metrix Internet Design GmbH

More Related Content

PPT
The Big Documentation Extravaganza
PPT
Component and Event-Driven Architectures in PHP
PPT
PEAR For The Masses
PPT
Inroduction to XSLT with PHP4
PPT
Go OO! - Real-life Design Patterns in PHP 5
PPT
Session Server - Maintaing State between several Servers
PPT
XML and PHP 5
PPT
XML and Web Services with PHP5 and PEAR
The Big Documentation Extravaganza
Component and Event-Driven Architectures in PHP
PEAR For The Masses
Inroduction to XSLT with PHP4
Go OO! - Real-life Design Patterns in PHP 5
Session Server - Maintaing State between several Servers
XML and PHP 5
XML and Web Services with PHP5 and PEAR

What's hot (20)

PDF
lf-2003_01-0269
PPSX
Php and MySQL
PPT
PHP MySQL Workshop - facehook
PPT
Control Structures In Php 2
PDF
Web Development Course: PHP lecture 1
DOCX
PHP NOTES FOR BEGGINERS
PPT
Php Crash Course
ODP
PHP Web Programming
PPT
Overview of PHP and MYSQL
PPTX
Php Unit 1
PPTX
PPT
PHP Workshop Notes
PPT
Php mysql
PPT
What Is Php
 
PPT
PHP POWERPOINT SLIDES
PPT
PPT
Open Source Package Php Mysql 1228203701094763 9
PPT
Php(report)
PPT
Chapter 02 php basic syntax
PPT
PDF Localization
lf-2003_01-0269
Php and MySQL
PHP MySQL Workshop - facehook
Control Structures In Php 2
Web Development Course: PHP lecture 1
PHP NOTES FOR BEGGINERS
Php Crash Course
PHP Web Programming
Overview of PHP and MYSQL
Php Unit 1
PHP Workshop Notes
Php mysql
What Is Php
 
PHP POWERPOINT SLIDES
Open Source Package Php Mysql 1228203701094763 9
Php(report)
Chapter 02 php basic syntax
PDF Localization
Ad

Viewers also liked (9)

PDF
NZ Digital Marketing Trends 2016 - Presentation
PPTX
Using visuals, audio and video materials
PPTX
Promoting Teacher Development through Peer Observation
PPTX
Novas Tecnologias 17: Painéis Fotovoltaicos
PPTX
How Gamification Will Impact Corporate Learning - EI Design
PPTX
Gamification of Compliance Training Through a Serious Game Concept - EI Design
PDF
دليل ورشة العمل
PPTX
Thinking Tools for Creative Kids An Introduction
PDF
neix_24agosto
NZ Digital Marketing Trends 2016 - Presentation
Using visuals, audio and video materials
Promoting Teacher Development through Peer Observation
Novas Tecnologias 17: Painéis Fotovoltaicos
How Gamification Will Impact Corporate Learning - EI Design
Gamification of Compliance Training Through a Serious Game Concept - EI Design
دليل ورشة العمل
Thinking Tools for Creative Kids An Introduction
neix_24agosto
Ad

Similar to XML Transformations With PHP (20)

PPT
PPT
5 xsl (formatting xml documents)
PPT
XML and XSLT
PPT
Processing XML with Java
PPT
Introduction to XML
PPT
Java XML Parsing
PPT
Introduction to XML
PPT
Week 12 xml and xsl
PPT
Intro XML for archivists (2011)
PPT
Extensible Stylesheet Language
ODP
Xml Overview
PPT
Sax Dom Tutorial
PDF
Project Automation
PPTX
What is xml
PPT
XML processing with perl
ODP
Developing web apps using Erlang-Web
PPT
PPT
6 311 W
PPT
6 311 W
PPT
Learning XSLT
5 xsl (formatting xml documents)
XML and XSLT
Processing XML with Java
Introduction to XML
Java XML Parsing
Introduction to XML
Week 12 xml and xsl
Intro XML for archivists (2011)
Extensible Stylesheet Language
Xml Overview
Sax Dom Tutorial
Project Automation
What is xml
XML processing with perl
Developing web apps using Erlang-Web
6 311 W
6 311 W
Learning XSLT

More from Stephan Schmidt (17)

PDF
Das Web Wird Mobil - Geolocation und Location Based Services
PDF
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
PDF
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
PDF
Continuous Integration mit Jenkins
PDF
Die Kunst des Software Design - Java
PDF
PHP mit Paul Bocuse
PDF
Der Erfolgreiche Programmierer
PDF
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
KEY
Die Kunst Des Software Design
PDF
Software-Entwicklung Im Team
PDF
JSON-RPC Proxy Generation with PHP 5
PPT
Declarative Development Using Annotations In PHP
PPT
XML-Socket-Server zur Kommunikation mit Flash
PPT
Interprozesskommunikation mit PHP
PPT
PHP im High End
PPT
Dynamische Websites mit XML
PPT
Web 2.0 Mit Der Yahoo User Interface Library
Das Web Wird Mobil - Geolocation und Location Based Services
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
Continuous Integration mit Jenkins
Die Kunst des Software Design - Java
PHP mit Paul Bocuse
Der Erfolgreiche Programmierer
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
Die Kunst Des Software Design
Software-Entwicklung Im Team
JSON-RPC Proxy Generation with PHP 5
Declarative Development Using Annotations In PHP
XML-Socket-Server zur Kommunikation mit Flash
Interprozesskommunikation mit PHP
PHP im High End
Dynamische Websites mit XML
Web 2.0 Mit Der Yahoo User Interface Library

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
“AI and Expert System Decision Support & Business Intelligence Systems”
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
sap open course for s4hana steps from ECC to s4
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
Cloud computing and distributed systems.
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
MIND Revenue Release Quarter 2 2025 Press Release
20250228 LYD VKU AI Blended-Learning.pptx

XML Transformations With PHP

  • 1. XML transformations with PHP PHPCon 2003 East April 25 th 2003, New York City, USA Stephan Schmidt
  • 2. Agenda About the speaker XML transformations Drawbacks of XSLT Transforming XML with PHP Installation patXMLRenderer Adding fun by overloading XML namespaces Design and usage of extensions PEAR::XML_Transformer XSLT vs PHP Transformers
  • 3. Stephan Schmidt Web Application Developer at Metrix Internet Design GmbH in Karlsruhe/Germany Programming since 1988, PHP since 1998 Publishing OS on http://www.php-tools.net Contributor to the German PHP Magazine Regular speaker at conferences Maintainer of patXMLRenderer, patTemplate, patUser and others
  • 4. XML transformations Data is stored in an XML document Needed in different formats and environments Other XML formats (DocBook, SVG, …) HTML Plain text LaTeX Anything else you can imagine Content remains the same
  • 5. Transform XML to XML Source document: <example title= „My Example“ > <greeting> Hello <imp> world </imp> ! <greeting> </example> Result of transformation into a different XML format: <document> <title> My Example </title> <section> <para> Hello <mark type= „important“ > world </mark> ! </para> </section> </document>
  • 6. Transform XML to HTML Result of transformation to HTML: <html> <head> <title> My Example </title> </head> <body> <h1> Hello <b> world </b></h1> </body> </html>
  • 7. Transform XML to plain text Result of transformation to plain text: My Example ------------- Hello W O R L D !
  • 8. Transform XML to LaTeX Result of transformation to LaTeX \documentclass{ article } \title{ My Example } \begin{ document } Hello {\em World } ! \end{ d ocument }
  • 9. Introduction to XSLT XSLT has been developed for the task of transforming XML documents XSLT stylesheets are XML documents Transforms XML trees that are stored in memory Uses XPath to access parts of a document Based on pattern matching (“When see you something that looks like this, do that…”) “ awk of the XML universe” May only append to its output Functional syntax
  • 10. „ Hello World“ Example (XML) Sample Document: <?xml version=&quot;1.0&quot;?> <greetings> <greeting> Hello World! </greeting> </greetings>
  • 11. „ Hello World“ Example (XSL) <xsl:stylesheet xmlns:xsl= &quot;http://www.w3.org/1999/XSL/Transform&quot; version= &quot;1.0&quot; > <xsl:output method= &quot;html&quot; /> <xsl:template match= &quot;/&quot; > <html> <body> <xsl:apply-templates select= &quot;greetings/greeting&quot; /> </body> </html> </xsl:template> <xsl:template match= &quot;greeting&quot; > <h1> <xsl:value-of select= &quot;.&quot; /> </h1> </xsl:template> </xsl:stylesheet>
  • 12. „ Hello World“ Example (Output)
  • 13. Drawbacks of XSLT XSLT is domain specific: Developed to work with XML Creating plain text/LaTeX is quite hard, as whitespace is important ( <xslt:text> ) Transforming “world” to “W O R L D” is next to impossible
  • 14. Drawbacks of XSLT XSLT is verbose and circumstantial: <xsl:choose> <xsl:when test= &quot;@author&quot; > <xsl:value-of select= &quot;@author&quot; /> <xsl:text> says: </xsl:text> <xsl:value-of select= &quot;.&quot; /> </xsl:when> <xsl:otherwise> <xsl:text> Somebody says: </xsl:text> <xsl:value-of select= &quot;.&quot; /> </xsl:otherwise> </xsl:choose>
  • 15. Drawbacks of XSLT XSLT is hard to learn: Functional programming language Complex structure (see “if/else” example”) XPath is needed Designer needs to learn it
  • 16. Drawbacks of XSLT XSLT is not up to the task of creating up-to-date websites: Not able to re-transform its own output XSL stylesheet is not able to manipulate itself Not able to query databases , include files or even the current date (even LaTeX is able to do this with “ \today ”) So, why should I use XSLT?
  • 17. Why use XSLT? You should use XSLT when… … you are transforming XML to XML or XHTML. … you need a portable application. … want to build a W3C compliant website. … you have too much free time and like hurting yourself Otherwise, you should do the transformation on your own.
  • 18. Transforming XML using PHP Transforming an XML document is easy: Define a transformation rule for each tag Start at the root element Traverse the document recursively Insert the transformation result to the parent tag Go home early as you have completed the task faster than with XSLT.
  • 19. Creating transformation rules Rules are simple: “ When you see this, replace it with that.” Implemented in PHP using templates Attributes of the tag are used as template variables PCData of the tag is used as template variable “CONTENT”
  • 20. Example <section title= &quot;XML Transformation&quot; > <para> XML may be transformed using PHP. </para> </section> XML Template for <section> Template for <para> <table border= &quot;0&quot; cellpadding= &quot;0&quot; cellspacing= &quot;2&quot; width= &quot;500&quot; > <tr><td><b> {TITLE} </b></td><tr> <tr><td> {CONTENT} </td></tr> </table> <font face= &quot; Arial &quot; size= &quot; 2 &quot; > {CONTENT} <br> </font>
  • 21. Example (Result) <table border= &quot;0&quot; cellpadding= &quot;0&quot; cellspacing= &quot;2&quot; width= &quot;500&quot; > <tr><td><b> XML Transformation </b></td><tr> <tr><td> <font face= &quot; Arial &quot; size= &quot; 2 &quot; > XML may be transformed using PHP <br > </ font> </td></tr> </table>
  • 22. Implementing the transformer Use a template class to “implement the rules”: Create a template for each tag Include placeholders for attributes and content of the tag Make use of the internal SAX parser: Simple callbacks for events (start tag, end tag, cdata) Traverses the document recursively Template is parsed when closing tag is found Result is appended to the content of the parent tag
  • 23. Don’t reinvent the wheel There already are XML transformers available for PHP: patXMLRenderer http://www.php-tools.net PEAR::XML_Transformer http:// pear.php.net phpTagLib http://chocobot.d2g.com
  • 24. Installation of patXMLRenderer Download archive at http://www.php-tools.de Unzip the archive Adjust all path names and options in the config file (cache, log, etc.) Create the templates (transformation rules) Create your XML files Let patXMLRenderer transform the files Finished: » It’s mere child’s play «
  • 25. Using patXMLRenderer $randy = new patXMLRenderer; // instantiate renderer $randy ->setExtensionDir( $extDir ); // set directory for extensions $randy ->setKnownExtensions( $knownExtensions ); // set list of extensions $randy ->setSkins( $skins ); // set list of available skins $randy ->selectSkin( &quot;blue&quot; ); // select one skin $randy ->setOption( &quot;autoadd&quot; , &quot;on&quot; ); // enable auto add for extensions $randy ->setOption( &quot;cache&quot; , &quot;auto&quot; ); // enable caching $randy ->setXMLDir( $xmlDir ); // set directory for all xml files $randy ->setXMLFile( $file ); // file to parse $template = new patTemplate(); // template object $randy ->setTemplate( $template ); foreach ( $baseTemplates as $tmplFile ) // load templates $randy ->addTemplateFile( $tmplFile ); $randy ->initRenderer(); // do some init routine $randy ->displayRenderedContent(); // transform and display (echo) result
  • 26. Introduction to patTemplate PHP templating class published under LGPL Placeholder for variables have to be UPPERCASE and enclosed in { and } Uses <patTemplate:tmpl name=&quot;...&quot;> tags to split files into template blocks that may be addressed seperately Other Properties of the templates are written down as attributes, e.g: type=&quot;condition&quot; or whitespace=&quot;trim&quot; Emulation of simple switch/case and if/else statement by using <patTemplate:sub condition=&quot;...&quot;> tags
  • 27. patTemplate Example simple Template with two variables (Corresponds to the XML tag <box>) <patTemplate:tmpl name=&quot;box&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;{WIDTH}&quot;> <tr> <td> {CONTENT} </td> </tr> </table> </patTemplate:tmpl>
  • 28. patTemplate Example 2 Task: Box should be available in three sizes: “small”, “large” and “medium” (default) Solution: Condition Template to emulate a switch/case statment: Template type is &quot;condition&quot; Variable that should be checked is called &quot;size&quot; Three possible values for &quot;size&quot;: &quot;small&quot;, &quot;large&quot; and &quot;medium&quot; (or any other unknown value) » three Subtemplates.
  • 29. patTemplate Example 2 <patTemplate:tmpl name=&quot;box&quot; type=&quot;condition&quot; conditionvar=&quot;size&quot;> <patTemplate:sub condition=&quot;small&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;200&quot;> <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> <patTemplate:sub condition=&quot;large&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;800&quot;> <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> <patTemplate:sub condition=&quot;default&quot;> <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;500&quot;> <tr><td> {CONTENT} </td></tr> </table> </patTemplate:sub> </patTemplate:tmpl>
  • 30. Adding fun Simple transformation is easy, but patXMLRenderer can do more: Callbacks for events (start tag, end tag, data) can be dispatched to classes (dubbed “extensions”) that are responsible for the transformation of the tag. Extensions may access any PHP function Extensions may return XML that is still transformed
  • 31. Overloading namespaces Extensions are added by overloading namespaces: On each event patXMLRenderer checks whether the current tag has a namespace patXMLRenderer checks whether an extension for the namespace has been defined patXMLRenderer instantiates the extension class (if not done already) patXMLRenderer calls the appropriate method of the class (startElement, endElement, characterData) Extension returns transformed tag and patXMLRenderer appends it to parent tag (or transforms it again)
  • 32. Simple Example <example> Today is <time:current format= “m-d-Y” /> . </example> Will be transformed to… <example> Today is 04-25-2004. </example> … Which will then be transformed using the rules defined in the templates.
  • 33. Architecture of patXMLRenderer RENDERER HTML / XML /LaTEX /etc... Any new extension XML TEMPLATES Time: Extension Dbc: Extension Ext. Entities
  • 34. patXMLRenderer Example <page> <dbc:connection name= &quot;foo&quot; > <dbc:type> mysql </dbc:type> <dbc:host> localhost </dbc:host> <dbc:db> myDb </dbc:db> <dbc:user> me </dbc:user> <dbc:pass> secret </dbc:pass> </dbc:connection> ...place any XML code here... <dbc:query connection= &quot;foo&quot; returntype= &quot;assoc&quot; > SELECT id,name,email FROM authors WHERE id= <var:get scope= &quot;_GET&quot; var= &quot;uid&quot; /> </dbc:query> <page>
  • 35. patXMLRenderer Example (Result) <page> ...any static XML... <result> <row> <id> 1 </id> <name> Stephan </name> <email> [email_address] </email> </row> </result> </page>
  • 36. patXMLRenderer example <control:switch> <control:expression> <var:get scope= &quot;HTTP_GET_VARS&quot; var= &quot;agree&quot; /> </control:expression> <control:case> <control:expression> yes </control:expression> <control:body> <para> Oh, It's nice that you agree! </para> </control:body> </control:case> <control:case> <control:expression> no </control:expression> <control:body> <para> Mmh, maybe we should work harder to convince you! </para> </control:body> </control:case> <control:default> <control:body> <para> You should set the value to 'yes' or 'no'. I don't think that's too hard! </para> </control:body> </control:default> </control:switch>
  • 37. Existing Extensions Repository on http://www.php-tools.net Documentation is generated automatically Examples: <xml:...> for XML highlighting <dbc:...> database interface <var:...> access to variables <control:...> control structures <randy:...> XML-API for patXMLRenderer <formgen:...> for creating (HTML) forms <rss:...> to include content from RSS feeds <file:...> file operations and many more...
  • 38. Getting bored? Let’s take a look at some real-life examples…
  • 39. Creating new extensions Creating new extensions is easy: Create a new folder in the extensions directory Create a new PHP file containing the extension class Create a new extension class by deriving it from the “patXMLRendererExtension” class Implement at least a handler for the endElement Create a new XML file that contains documentation about your extension Open the administration interface of patXMLRenderer and add the extension
  • 40. The extension class c lass patXMLRendererPHPConExtension extends patXMLRendererExtension { var $name = &quot;patXMLPHPConExtension&quot; ; var $version = &quot;0.1&quot; ; var $cacheAble = array ( &quot;RATE&quot; => true ); }
  • 41. The endElement handler f unction endElement( $parser , $ns , $tag ) { $data = $this-> getData(); $tag = array_pop ( $this-> tagStack ); $attributes = array_pop ( $this-> attStack ); switch ( $tag ) { case &quot;RATE&quot; : return array( &quot;data&quot; => &quot;I think PHPCon 2003 is &quot; . $data , &quot;containsMarkup&quot; => false ); break ; } }
  • 42. The XML file <configuration> <!-- information about the extension --> <path name= &quot;extension&quot; > <configValue type= &quot;string&quot; name= &quot;namespace&quot; > phpcon </configValue> <configValue type= &quot;string&quot; name= &quot;name&quot; > PHPCon Extension </configValue> <configValue type= &quot;string&quot; name= &quot;class&quot; > patXMLRendererPHPConExtension </configValue> <configValue type= &quot;float&quot; name= &quot;version&quot; > 0.1 </configValue> <configValue type= &quot;string&quot; name= &quot;requiredRandyVersion&quot; > 0.6 </configValue> <configValue type= &quot;string&quot; name= &quot;description&quot; > Just an example for the PHPCon 2003 East in NYC </configValue> </path> <!-- information about the author --> <path name= &quot;author&quot; > <configValue type= &quot;string&quot; name= &quot;name&quot; > Stephan Schmidt </configValue> <configValue type= &quot;string&quot; name= &quot;email&quot; > [email_address] </configValue> </path> <!-- documentation of the tags would follow here --> </configuration>
  • 43. patXMLRenderer features More features of patXMLRenderer include: Automated, intelligent caching Use of external entities (or xinc) to include files Administration interface <?PHP to include PHP code in XML files Parameters in XML attributes Offline Generator, generates static HTML pages
  • 44. PEAR::XML_Transformer Developed by Kristian Köhntopp and Sebastian Bergmann Only used to overload namespaces or tags with Objects or functions No usage of a templating class, transformation from XML to HTML has to be done in PHP classes Existing Namespace handlers Simple image generation DocBook to HTML transformation Widget, to create HTML Widgets PHP, to define your own tags or eval PHP code
  • 45. XML_Transformer example <html> <body> <img:gtextdefault bgcolor= &quot;#888888&quot; fgcolor= &quot;#000000&quot; font= &quot;cour.ttf&quot; fontsize= &quot;32&quot; border= &quot;1&quot; spacing= &quot;2&quot; cachable= &quot;yes&quot; /> <img:gtext> My Example </img:gtext> </body> </html> » Same syntax and structure as patXMLRenderer
  • 46. Comparison - -  Parameters Only with PHP code -  „ Intelligent“ Templates - All in one package  Ext. Repository - -  Administration  -  <?PHP ?> - -  External Entities - Yes, infinite Yes, infinite Recursion -   Namespaces - Yes, objects and functions Yes, only object Dynamic Content (Callbacks) yes, plain HTML - yes, patTemplate Templates phpTagLib PEAR patXMLRenderer
  • 47. XSLT vs PHP Transformers Pro XSLT: W3C Standard XPath allows restructuring of the document, e.g. creation of a table of contents Portable (not limited to servers with PHP) Pro PHP Transformers: Easier to learn (designers are familiar with templates) Easy to extend Complete control during the whole transformation Result may be transformed again (recursive transformation)
  • 48. Drawbacks of PHP Transformers Biggest disadvantages of PHP Transformers: Only the current tag is accessible As a SAX parser is used, no restructuring or reordering is possible. Table of contents can not be generated Cross references are not possible Sorting is not possible Possible solutions: Two pass transformations (like LaTeX) Combination of XSLT and PHP Transformers
  • 49. Combining forces Combining XSLT and patXMLRenderer is possible by using the XSLT extension of patXMLRenderer: Uses XSLT to transform The whole document Parts of the document May be combined with all of patXMLRenderers features: inclusion of any dynamic content, control structures, caching, etc. Result of XSLT transformation may be transformed using templates.
  • 50. patXMLRenderer and XSLT example <xslt:transform returntype= &quot;xml&quot; > <xslt:stylesheet type= &quot;file&quot; > extensions/xslt/xsl/example.xsl </xslt:stylesheet> <xslt:param name= &quot;displayToc&quot; >yes</xslt:param> <sections> <section title= &quot;Dummy Section 1&quot; > <para> This is just a dummy.... </para> </section> <section title= &quot;Dummy Section 2&quot; > <para> Another Section… </para> </section> </sections> </xslt:transform>
  • 51. XSLT Stylesheet <xsl:stylesheet xmlns:xsl= &quot;http://www.w3.org/1999/XSL/Transform&quot; version= &quot;1.0&quot; > <xsl:output method= &quot;xml&quot; /> <xsl:param name= &quot;displayToc&quot; /> <xsl:template match= &quot;/&quot; > <xsl:if test= &quot;$displayToc = 'yes'&quot; > <section title=&quot;Table of contents&quot;> <ol> <!-- Create TOC --> <xsl:for-each select= &quot;//section&quot; > <oli> <xsl:value-of select= &quot;@title&quot; /> </oli> </xsl:for-each> </ol> </section> </xsl:if> <!-- Display contents --> <xsl:for-each select= &quot;*|text()&quot; > <xsl:apply-templates select= &quot;*|text()&quot; /> </xsl:for-each> </xsl:template> <!-- xsl template to insert original content has been left out --> </xsl:stylesheet>
  • 52. The End Thank you! More information: http://www.php-tools.net [email_address] Thanks to: Sebastian Mordziol, Gerd Schaufelberger, Metrix Internet Design GmbH