Transforming XML with XSLT

Extensible Stylesheet Language Transformations (XSLT) is a language for transforming XML documents into different XML, HTML, or any other format. For example, many web sites offer several formats of their content—HTML, printable HTML, and WML (Wireless Markup Language) are common. The easiest way to present these multiple views of the same information is to maintain one form of the content in XML and use XSLT to produce the HTML, printable HTML, and WML.

PHP’s XSLT extension uses the Sablotron C library to provide XSLT support. Sablotron does not ship with PHP—you’ll need to download it from http://www.gingerall.com, install it, and then rebuild PHP with the --enable-xslt --with-xslt-sablot option to configure.

PHP’s XSLT support is still experimental at the time of writing, and the exact implementation details may change from what is described here. However, this description should give you a good foundation for how to use PHP’s XSLT functions, even if the implementation changes in the future.

Three documents are involved in an XSLT transformation: the original XML document, the XSLT document containing transformation rules, and the resulting document. The final document doesn’t have to be in XML—a common use of XSLT is to generate HTML from XML. To do an XSLT transformation in PHP, you create an XSLT processor, give it some input to transform, then destroy the processor.

Create a processor with xslt_create( ) :

$xslt = xslt_create(  );

Process a ...

Get Programming PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.