Transform XML in PHP

XML has now been around for quite a few years. If you are doing any type of web development it’s pretty hard to avoid XML. All RSS Feeds are really just XML documents that you access via a URL. For RSS, there are some well defined standard schemas (i.e., structured definitions) that the XML documents are created as.


RSS feeds are a common way of adding “self-updating” content to your web site. If you are using a platform like Wordpress then you can find some Wordpress plugins that make it quite easy to put content into your page or post from an RSS feed without any coding on your part. The downside, of course, is that you’re stuck with the format/layout that the plugin produces.


XSLT to the Rescue

XSLT is used to “transform” an XML document. With XSLT you can take an XML document and transform it into HTML, plain text, XML, and more. It’s a great way to turn an RSS Feed into a web page (or part of a web page) that is unique and fits your theme.


So, by now you’re probably wondering what it takes to actually transform XML using XSLT. The example I’m going to provide you is done using PHP. You can do this in almost any web development language, it’s just a matter of learning the proper syntax and APIs to use.


For my example I’m going to use the following RSS Feed:

http://www.NothingButZunes.com/zune-feed.xml

I’m going to show you how to use some very simple XSLT and the XML library in PHP to transform it into HTML that will look like this:

Top Selling Zunes, Zune Accessories, and More

Lists the top selling zune, zune media players, and zune accessories.

Zune Home A/V Pack

Zune Home A/V Pack Manufactured By:   Zune
Release Date:   2006-11-21
Rating: , 47 Reviews
List Price:   $99.99
Buy New (17): $21.71
Buy Used (2): $24.99

Buy now and save $78.28
Usually ships in 1-2 business days


BUY NOW!

Zune 80 GB Digital Media Player Black (2nd Generation)

Zune 80 GB Digital Media Player Black (2nd Generation) Manufactured By:   Zune
Release Date:   2007-11-13
Rating: , 299 Reviews
List Price:   $249.00
Buy New (10): $248.85
Buy Used (3): $223.97

Buy now and save $0.15
Usually ships in 24 hours


BUY NOW!

Zune Home A/V Pack

Zune Home A/V Pack Manufactured By:   Zune
Release Date:   2006-11-21
Rating: , 47 Reviews
List Price:   $99.99
Buy New (17): $21.71
Buy Used (2): $24.99

Buy now and save $78.28
Usually ships in 1-2 business days


BUY NOW!

Altec Lansing M604 Powered Audio System for Zune

Altec Lansing M604 Powered Audio System for Zune Manufactured By:   Altec Lansing Technologies
Rating: , 101 Reviews
List Price:   $199.99
Buy New (3): $98.99
Buy Used (1): $79.00

Buy now and save $101.00
Usually ships in 1-2 business days


BUY NOW!

Zune Leather Case 80 GB

Zune Leather Case 80 GB Manufactured By:   Zune
Rating: , 19 Reviews
List Price:   $49.99
Buy New (14): $15.98
Buy Used (1): $23.94

Buy now and save $34.01
Usually ships in 1-2 business days


BUY NOW!

Belkin Sports Jacket Case with Armband for Zune 30 GB (Black)

Belkin Sports Jacket Case with Armband for Zune 30 GB (Black) Manufactured By:   Belkin Components
Release Date:   2006-10-04
Rating: , 16 Reviews
List Price:   $19.99
Buy New (4): $14.99
Buy Used (1): $8.64

Buy now and save $5.00
Usually ships in 1-2 business days


BUY NOW!

Unviersal Car Windshield Mount Holder for Htc 8525, 8125, Wing, Dash, Mda, Ppc-6800, Blackberry 8300 Curve, 8100 Pearl, 8800, 8830, Palm Treo 680, 750, 700w, Nokia N95, N75, N80, Lg Vx8500, Vx9900, Vx8350, Vx8600, Cu575, Audiovox, Motorola, Samsung, Sanyo, All Hp Ipaq, Nextel, Sony Ericsson W810, W580i, Sidekick, Apple Ipod Touch, Classic, Video, Iphone, Creative Zen, Microsoft Zune, Sandisk Sansa, Gps, PSP

Unviersal Car Windshield Mount Holder for Htc 8525, 8125, Wing, Dash, Mda, Ppc-6800, Blackberry 8300 Curve, 8100 Pearl, 8800, 8830, Palm Treo 680, 750, 700w, Nokia N95, N75, N80, Lg Vx8500, Vx9900, Vx8350, Vx8600, Cu575, Audiovox, Motorola, Samsung, Sanyo, All Hp Ipaq, Nextel, Sony Ericsson W810, W580i, Sidekick, Apple Ipod Touch, Classic, Video, Iphone, Creative Zen, Microsoft Zune, Sandisk Sansa, Gps, PSP Manufactured By:   Bargaincell
Rating: , 9 Reviews
List Price:   $29.99
Buy New (1): $8.57

Buy now and save $21.42
Usually ships in 1-2 business days


BUY NOW!

Bluetooth Handsfree Fm Transmitter for Car Stereo Modulator 200 Channel / Ipod / Zune Car Kit-black with Built in Backnoise Cancelation Microphone AD2P Ready(Black)

Bluetooth Handsfree Fm Transmitter for Car Stereo Modulator 200 Channel / Ipod / Zune Car Kit-black with Built in Backnoise Cancelation Microphone AD2P Ready(Black) Manufactured By:   Satechi
Rating: , 27 Reviews
Buy New (1): $99.00

Buy now and save $30.99
Usually ships in 1-2 business days


BUY NOW!

JBuds Hi-Fi Noise-Reducing Ear Buds (Black)

JBuds Hi-Fi Noise-Reducing Ear Buds (Black) Manufactured By:   JLab Audio
Rating: , 676 Reviews
List Price:   $49.95
Buy New (12): Too low to display
Buy Used (1): $41.58

Buy now and save $32.62
Usually ships in 1-2 business days


BUY NOW!

Coby CVE92 Isolation Stereo Earphones

Coby CVE92 Isolation Stereo Earphones Manufactured By:   Coby
Rating: , 143 Reviews
List Price:   $19.99
Buy New (29): $4.13

Buy now and save $15.86
Usually ships in 1-2 business days


BUY NOW!

Digital Media Converter by DeskShare

Digital Media Converter by DeskShare Manufactured By:   DeskShare, Inc.
Rating: , 1 Reviews
List Price:   $49.95
Buy New (1): $39.95

Buy now and save $10.00
Usually ships in 24 hours


BUY NOW!

Xilisoft Microsoft Zune DVD & Video Converter Suite

Xilisoft Microsoft Zune DVD & Video Converter Suite Manufactured By:   Xilisoft
List Price:   $45.00

How to Do Everything with Your Zune

How to Do Everything with Your Zune Manufactured By:   McGraw-Hill Osborne Media
Release Date:   2007-07-13
Rating: , 4 Reviews
List Price:   $24.99
Buy New (1): $9.99

Buy now and save $15.00
Usually ships in 24 hours


BUY NOW!

Tinderbox

[No Preview Image] Manufactured By:   Common Courage Press
Rating: , 7 Reviews
List Price:   $29.95
Buy Used (4): $6.77

Laptop Speakers - USB, Portable, Compact, Travel Speaker for PC and Mac - B-Flex 2 Hi-Fi Stereo USB Speaker (Black)

Laptop Speakers - USB, Portable, Compact, Travel Speaker for PC and Mac - B-Flex 2 Hi-Fi Stereo USB Speaker (Black) Manufactured By:   JLab Audio
Rating: , 21 Reviews
List Price:   $69.95
Buy New (1): $39.95

Buy now and save $30.00
Usually ships in 1-2 business days


BUY NOW!

White Travel Size Foldable Multimedia Speaker for iPod, iPod (video), iPod nano, iPod Photo, iPod Mini, and iPod Shuffle

White Travel Size Foldable Multimedia Speaker for iPod, iPod (video), iPod nano, iPod Photo, iPod Mini, and iPod Shuffle Manufactured By:   Insten
Rating: , 11 Reviews
List Price:   $39.99
Buy New (5): $3.50
Buy Used (1): $2.99

Buy now and save $36.49
Usually ships in 1-2 business days


BUY NOW!


XML, XSLT, PHP

I’ve listed the PHP code below. The $xslt variable contains the text of the XSLT that is used when transforming the RSS Feed.

<?php
$xslt = '<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" omit-xml-declaration="no"/>
<xsl:template match="/rss/channel">
<h1><a href="{link}"><xsl:value-of select="title"/></a></h1> <p><xsl:value-of select="description"/></p>
<xsl:for-each select="item">
<h2><a href="{link}"><xsl:value-of select="title"/></a></h2>
<xsl:value-of disable-output-escaping="yes" select="description"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>';

function Transform($xmlData, $xslText)
{
$output = "";

$CurVer = phpversion();
if (version_compare($CurVer, '5.0.0') < 0)
{
$xml = domxml_open_mem($xmlData);
$xsl = domxml_xslt_stylesheet($xslText);
$result = $xsl->process($xml);
$output = $result->dump_mem();
}
else
{
$xml = new DOMDocument();
$xml->loadXML($xmlData);

$xsl = new DOMDocument();
$xsl->loadXML($xslText);

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

$output = $proc->transformToXML($xml);
}

echo $output;
}

function ShowRssFeed($url, $xslText)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$row = curl_exec($ch);
curl_close($ch);

if ($row == ”)
{
echo ‘

Data feed not available

‘;
}
else
{
file_put_contents(’zune.xml’, $row);

Transform($row, $xslText);
}
}

ShowRssFeed(’http://www.NothingButZunes.com/zune-feed.xml’, $xslt);
?>

No Comments

Leave a reply

You must be logged in to post a comment.