For all your music news related needs


Week 11:

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other forms, such as HTML or even another XML file. There are many reasons one may wish to incorporate external feeds into their websites, including the ability to have a wider range of information from multiple sources. The ability to include data from external sources is especially important for information that changes or needs to be changed frequently.

The addition of an external data source can be completed through many methods, including a recursive PHP printing loop, or the preferred XSLT method.

The process for the PHP method involves loading in the file using simplexml_load_file();, then encoding and decoding this using json_encode and json_decode. The process of encoding then decoding the file allows for the data to be in an array format which inherently is more manageable than the object item you are given through importation. This can be seen below;

$xml = simplexml_load_file('./rssFeed.xml');

$json_string = json_encode($xml);

$result_array = json_decode($json_string, TRUE);

From here, you can then grab the specific item data using the following code inside a for loop:

title = $result_array['channel']['item'][$i]['title'];

link = $result_array['channel']['item'][$i]['link'];

description = $result_array['channel']['item'][$i]['description'];

pubDate = $result_array['channel']['item'][$i]['pubDate'];

media = $result_array['channel']['item'][$i]['media'];

This is then displayed on the page in the desired format. using HTML tags with the newly declared variables. This format can be easily looped through using PHP

The latter method, XSLT, is thought to be far more attractive as you have the ability to declare the format for said data in advance with an additional .xsl document which is read in for formatting and styling purposes. With the XSLT method, you must declare the constructor new XSLTProcessor(); which will allow you manage the stylesheet and the transformations.

The XSLT file is as follows:

<?php

 $xml = simplexml_load_file('https://pitchfork.com/rss/news/') or die("Error: Cannot create object");

 $xsl = simplexml_load_file("../controller/template.xsl");

 $proc = new XSLTProcessor();

 $proc->importStyleSheet($xsl);

 $result = $proc->transformtoXML($xml);

 echo $result;

?>

The XSLT file is in control of calling the external rss feed, calling the required template xsl file, importing the styled template then transforming the rss feed and displaying in HTML.

The XSL template is as follows:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


 <xsl:template match="/">

  <xsl:for-each select="rss/channel/item">

   <xsl:variable name="myVar" xmlns:media="http://search.yahoo.com/mrss/" select="media:thumbnail/@url"/>

   <div class="col-12 col-sm-12 col-md-6 col-lg-4 align-items-stretch">

    <div class="card mb-4 box-shadow h-md-250">

     <div class="card-body align-items-start row-relative">

      <h3 class="mb-0">

       <a class="text-dark text-center" href="{link}"><xsl:value-of select="title"/></a>

      </h3>

      <div class="mb-1 text-muted"><xsl:value-of select="pubDate"/></div>

      <varoutput>

       <img src='{$myVar}' class="img-responsive w-100" />

      </varoutput>

      <p class="card-text mb-auto"><xsl:value-of select="description"/></p>

      <a href="{link}">Continue reading</a>

     </div>

    </div>

   </div>

  </xsl:for-each>

 </xsl:template>

</xsl:stylesheet>

As can see been above, the XSL file uses tags not unlike HTML. It is easy to create clean and preccise with this system and it is easy to grab data from the XML file using the <xsl:value-of select="pubDate"/> tag.

XSLT is the preferred and most advantagous method for several reasons. Using XSLT means that you have a cleaner, and more concise template. XSLT allows for transformations of XML directly into HTML which is makes usage much easier and much faster.

The php version can be found here and the XSLT version can be found here. It should be noted that these pages display the same data and information on the frontend however the backends are using different methods as explained above.

word count: 408