Click to See Complete Forum and Search --> : XML File Doesn't apply XSL Stylesheet


martinsandhu
05-20-2007, 07:37 AM
I've generated a XML File using PHP and created a XSL Stylesheet to create the markup however the XML file doesn't seem to apply the Stylsheet and i am unsure why?

Any advice?

http://www.warwickwebvisions.com/calls-abroad/xml-transformation.php

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="root/country/">
<select>
<option id="<xsl:value-of select='id'>"><xsl:value-of select="location"></option>
</select>
</xsl:for-each>
</body>
</html>



</xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="country-list.xsl"?>

<root>
<country>
<id>1</id>
<location>India</location>
<content>Test Data for India</content>
</country>
<country>
<id>2</id>
<location>Pakistian</location>
<content>Test data for Pakistian</content>
</country>
</root>

Charles
05-20-2007, 07:44 AM
Your server is telling my browser to display the document as HTML. You need to add, as the first thing sent to me, header('Content-Type: application/xml; utf-8'); or header('Content-Type: text/xml; utf-8');

martinsandhu
05-20-2007, 09:59 AM
thanks for that info i put the code below but i keep getting the above error..

<?
header('Content-Type: text/xml; utf-8');
require_once('classes/database.class.php');


$connection = new database();
$link = $connection->database_connection();

$table_id = 'country';
$query = "select * from country";
$results = mysql_query($query) or die(mysql_error());
if (!$results)
{
print 'There was a database error when executing';
print mysql_error();
exit;
}



//create new xml document
$doc = new DomDocument('1.0' , 'ISO-8859-1');

// link to style sheet
$child = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="country-list.xsl"');
$doc->appendChild($child);

// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);

//process one row at a time
while($row = mysql_fetch_assoc($results)) {


//add node for each row

$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);

//add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {

$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);

$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);

} //foreach
} //while

$xml_string = $doc->saveXML();

echo $xml_string;

?>

martinsandhu
05-20-2007, 10:00 AM
sorry heres the error

http://www.warwickwebvisions.com/calls-abroad/xml-transformation.php

Charles
05-20-2007, 12:44 PM
I'm not sure, perhaps because you have white space before the XML declaration.

And there is a big problem with your style sheet. Use instead<xsl:template match="/">
<html>
<body>
<xsl:for-each select="root/country/">
<select>
<option>
<xsl:attribute name="id">
<xsl:value-of select="id">
</xsl:attribute>
<xsl:value-of select="location">
</option>
</select>
</xsl:for-each>

</body>
</html>



</xsl:template>

</xsl:stylesheet>

martinsandhu
05-20-2007, 02:32 PM
Thanks,


I've done the changes you requested with no luck.

I was thinking can i put the content type in my cpanel? what would the defined MIME Type be?

Thanks

Charles
05-20-2007, 02:48 PM
Try putting the header function back in, but make certain that there is no white space on the page before your PHP.

View http://www.warwickwebvisions.com/calls-abroad/xml-transformation.php in your browser but look at the source and you'll see what I'm going on about.

jkmyoung
05-22-2007, 04:58 PM
<option id="<xsl:value-of select='id'>">
This will give you the string '<xsl:value-of select='id'>'. Change this to:
<option id="{id}">

Further, there appears to be a problem with the generation of your xml doc, as it is creating whitespace in the beginning, possibly an improper encoding. How are you generating the xml file?