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 websites 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 libxslt C library to provide XSLT support.
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, and then destroy the processor.
Create a processor by creating a new XsltProcessor object:
$processor=newXsltProcessor;
Parse the XML and XSL files into DOM objects:
$xml=newDomDocument;$xml->load($filename);$xsl=newDomDocument;$xsl->load($filename);
Attach the XML rules to the object:
$processor->importStyleSheet($xsl);
Process a file with the transformToDoc(), transformToUri(), or transformToXml() methods:
$result=$processor->transformToXml($xml);
Each takes the DOM object representing the XML document as a parameter.
Example 11-10 is the XML document we’re going to transform. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access