Vous êtes sur la page 1sur 21

XML Transformations with PHP

Sebastian Bergmann
<http://www.sebastian-bergmann.de/>

International PHP Conference 2003 – Spring Edition


Amsterdam, May, 9th 2003
Please bear with me ...
● Maybe I should begin this presentation with:
„My written English is better than my spoken
English.“.
● This is the second time (the first time was
only some hours ago) I give a presentation in
English. Please bear with me ;-)
● Feel free to ask questions at any time!
Why transform XML?
● Transforming XML documents from one
format to another becomes more and more
important.
● In a Content Management System (CMS)
setting custom additions to XHTML like
<navigation/> allow designers to work with
tools they are familar with: tags.
● Such a tag would trigger a functionality
implemented by a programmer; the designer
does not need to know how the tag produces
its output.
Why not use XSLT?
● XSLT is a domain specific language for
transforming XML documents into other XML
documents.
● XSLT transforms XML trees like awk
transforms document lines. Both languages
have a strict condition-action syntax.
● XSLT programs are written in XML syntax.
This makes for a very verbose syntax.
Why not use XSLT?
Why not use XSLT?
● XSLT is a Functional Programming language.
● This would not present a problem if you were
able to leverage the potential of functional
programming.
● But XSLT only ''provides'' the programmer
with the drawbacks of functional
programming – like using recursion to
express loops – while omitting its benefits.
For instance the use of functions as data
types is not possible in XSLT.
Solving problems with the XML Transformer

● The XML Transformer provides an elegant


solution to ''bring PHP objects to the web''.
● It allows the PHP programmer to describe the
transformations from one XML format to
another using his favourite programming
language: PHP.
Installing the XML Transformer
● The XML Transformer is a PEAR (PHP
Extension and Application Repository)
package.
● It is hosted at http://pear.php.net/ and can
easily be installed using the PEAR Installer.
A First Example
Using the Output Buffer Driver
Bundled Namespace Handlers: Anchor
Bundled Namespace Handlers: Image
Bundled Namespace Handlers: PHP
Writing Namespace Handlers
● Inherit a class from the
XML_Transformer_Namespace class.
● For each element that is to be transformed
write a start_ELEMENT($attributes) and a
end_ELEMENT($cdata) method.
● The XML Transformer will call these methods
for the opening and closing tags of the
ELEMENT.
XML_Transformer_Util::attributesToString()
XML_Transformer_Util::qualifiedElement()
Debugging the XML Transformer
● The XML Transformer provides a Debugging
Mode.
● When enabled, for each event a debug
message is sent to either the screen or to a
logfile.
XML Transformer Error Handling
● The XML Transformer tries to provide useful
error messages.
● An error occurs, for instance, when a
transformation methods returns a string that
is not well-formed XML.
Deploying XML Transformer Efficiently

● Leveraging Apache's .htaccess and PHP's


auto_prepend_file mechanisms we will now
discuss an efficient deployment strategy for
XML Transformer driven applications.
Deploying XML Transformer Efficiently

Vous aimerez peut-être aussi