Sunday, 19 April 2015

Using xslt and cURL



This is my first attempt to perform a transform on an xml returned from cURL in the form of a string. I have a php that I think is written properly as I can at least echo back the string contained in $xml_data . However, whenever I attempt to view the php through a browser I get a blank page (and source). What am I doing wrong? Any pointers would be helpful and appreciated..


php



<?php
$ch = curl_init("http://ift.tt/1H5u18S");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml_data=curl_exec($ch);
curl_close($ch);

$xmlDoc = new DOMDocument();
$xml->$xml_data;

$xslDoc = new DOMDocument();
$xslDoc->load("new_releases.xsl");

$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);

$xmlDoc->loadXML($xml_data);

echo $proc->transformToXML($xmlDoc);

$proc->setParameter('', 'albumid', $_POST["id"]);

?>


The XSL (note this is not complete, at this point I'm just attempting to get some of the data from xml on the screen and style it)



<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR" xmlns="http://ift.tt/lH0Osb">

<xsl:output method="html" />

<xsl:template match="/">
<html><body>
<div style="position: relative; font-weight: bold; font-size: 24pt;">
New music for the week of <xsl:value-of select="release_week_start"/></div>
<xsl:for-each select="/album">
<a xmlns="http://ift.tt/lH0Osb"
href="{../@id}.xhtml">
<xsl:value-of select="album/artist"/>
</a>
</xsl:for-each>
</body></html>



</xsl:stylesheet>


If I include echo $xml_data in my php I get the following xml string



<?xml version="1.0"?>
<new_release>
<release_week_start date="January 26, 2014">
<album id="AD1" albumart="http://ift.tt/1H5u0ln">
<artist>After The Disco</artist>
<name>Broken Bells</name>
<year>2014</year>
<release_date>January 31, 2014</release_date>
<label>Columbia Records</label>
<disc>1</disc>
<totaldiscs>1</totaldiscs>
<tracklist>
<track id="1">Perfect World</track>
<track id="2">After The Disco</track>
<track id="3">Holding On For Life</track>
<track id="4">Leave It Alone</track>
<track id="5">The Changing Lights</track>
<track id="6">Control</track>
<track id="7">Lazy Wonderland</track>
<track id="8">Medicine</track>
<track id="9">No Matter What You're Told</track>
<track id="10">The Angel And The Fool</track>
<track id="11">The Remains of Rock And Roll</track>
</tracklist>
</album>
<album id="CC1" albumart="http://ift.tt/1H5u0ln">
<artist>Casting Crowns</artist>
<name>Thrive</name>
<year>2014</year>
<release_date>January 28, 2014</release_date>
<label>Reunion Records</label>
<disc>1</disc>
<totaldiscs>1</totaldiscs>
<tracklist>
<track id="1">Thrive</track>
<track id="2">All You've Ever Wanted</track>
<track id="3">Just Be Held</track>
<track id="4">You Are The Only One</track>
<track id="5">Broken Together</track>
<track id="6">Love You With The Truth</track>
<track id="7">This Is Now</track>
<track id="8">Dream For You</track>
<track id="9">Follow Me</track>
<track id="10">Heroes</track>
<track id="11">House Of Their Dreams</track>
<track id="12">Waiting On The Night To Fall</track>
</tracklist>
</album>
</release_week_start>
</new_release>

No comments:

Post a Comment