XSLT processor

Learning project: PHP

03/2023

Educom’s training program introduced me to not only CSS as a styling language but also to Extensible Stylesheet Language (XSL), which is the styling language for XML documents.

What’s the program about?
In reference to that, I worked on a project using the XSLT language to transform a document into an XML document via XSL formatting.
As you can see below in the code section, the start point of this project was the file library.xml. In the file simple.xslt the formatting is being described and the xslt_processor.php converts library.xml file into a new XML format which can be displayed in the browser. The end result you can see in the picture.

PHP

library.xml

<?xml version="1.0" encoding="UTF-8"?> <library> <book> <title>Programming PHP, 2nd Ed.</title> <isbn>0-596-00681-0</isbn> <authors> <author>Rasmus Lerdorf</author> <author>Kevin Tatroe</author> <author>Peter MacIntyre</author> </authors> <publisher>OReilly</publisher> <price>39.99</price> <pubdate>April, 2006</pubdate> </book> <book> <title>PHP The Good Parts</title> <isbn>978-0596804374</isbn> <authors> <author>Peter MacIntyre</author> </authors> <publisher>OReilly</publisher> <price>37.99</price> <pubdate>March, 2010</pubdate> </book> <book> <title>Javascript: The Good Parts</title> <isbn>978-0-596-51774-8</isbn> <authors> <author>Douglas Crockford</author> </authors> <publisher>OReilly</publisher> <price>29.99</price> <pubdate>May, 2008</pubdate> </book> </library>

simple.xslt

<?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- This is the simplest identity function --> <xsl:template match="/"> <html> <body> <xsl:for-each select="library/book"> <b>Titel: </b> <xsl:value-of select = "title"/> <br/> <b>ISBN: </b> <xsl:value-of select = "isbn"/> <br/> <b>Authors: </b> <br/> <xsl:for-each select="authors/author"> ---- <xsl:value-of select = "."/> <br/> </xsl:for-each> <b>Publisher: </b> <xsl:value-of select = "publisher"/> <br/> <b>Price: </b> <xsl:value-of select = "price"/> <br/> <b>Publication date: </b> <xsl:value-of select = "pubdate"/> <br/> <hr width='300px' align='left' /> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

xslt_processor.php

<?php //Creating an XSLTProcessor //Loading an XSL document $xsl = new DOMDocument(); $xsl->load("simple.xslt"); // $xsl->loadXML($xslt_var); //Loading an XML document $xml = new DOMDocument(); $xml->load("library.xml"); // $xsl->loadXML($xml_var); //Creating an XSLTProcessor $proc = new XSLTProcessor(); //Importing the XSL document $proc->importStyleSheet($xsl); //Transforming the style to XML print($proc->transformToXML($xml)); ?>
Web shop – PHP and MySQL
Collatz conjecture – PHP