Saturday, 10 January 2015

transforming XML with XSL



As the title says, I'm trying to transform an XML file using XSL at the server. My XML file saves to the server and displays fine. However if I try transforming it, nothing displays. Here is the code in question.


XML.PHP



<?php
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php");
$connection = mysqli_connect($hostname, $username, $password, $databaseName) or die("you did not connect");
$query = "SELECT * FROM art";
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
//Top of xml file
$_xml = '<?xml version="1.0"?>';
$_xml .="<art>";
while($row = mysqli_fetch_array($result)) {
$_xml .="<art>";
$_xml .="<art_name>".$row['name']."</art_name>";
$_xml .="<art_category>".$row['category']."</art_category>";
$_xml .="<art_price>".$row['price']."</art_price>";
$_xml .="</art>";
}
$_xml .="</art>";
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//And output
//print $xmlobj->asXML();
//or we could write to a file
$xmlobj->asXML('art.xml');
?>


XSL.PHP



<?php require 'header.php';?>
<div class="sixteen columns">
<?php
//Create a DomDocument object

$xml = new DOMDocument;

// Load the XML source

$xml -> load('art.xml');


//Similar with XSL

$xsl = new DOMDocument;

$xsl -> load('art.xsl');

// Create and Configure the transformer

$proc = new XSLTProcessor;

// attach the xsl rules

$proc -> importStyleSheet($xsl);

//Output

echo $proc -> transformToXML($xml);


?>
</div>
<?php require 'footer.php'; ?>

No comments:

Post a Comment