Page 1 of 1

Inserting XSL Stylesheet into PHP Generated XML File

Posted: Fri May 18, 2007 3:10 am
by maddenuk
I've generated an xml file from my database items and have created a XSL stylesheet. What i want is add the XSL stylesheet refernece to the XML document but i couldn't find in the DOM Document Reference what i would use to do this. Any ideas?

Code: Select all

<? 

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'); 

// 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

Posted: Fri May 18, 2007 11:53 am
by volka
You can use the processing instruction xml-stylesheet, something like <?xml-stylesheet href="foo.xsl" type="text/xsl"?>
see http://de2.php.net/function.dom-domdocu ... nstruction

Code: Select all

//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="yourXSLFIle.xsl"');
$doc->appendChild($child)

// create root node