Xml style sheet doesnt work with while loop

Hi everyone i have an xml which display data from the database i have it on a while loop but i am unable to style i don’t know why is not wrokin can someone help me out please.

xml file

<?php
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ www.developphp.com - April 05, 2010
// View the video that is tied to this script for maximum understanding
// -------------------------------------------------------------------
header("Content-Type: text/xml"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<XML>";
// Connect to your MySQL database whatever way you like to here
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("admin") or die ("no database");
// Execute the Query on the database to select items(20 in this example)
$sql = mysql_query("SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20");
while($row = mysql_fetch_array($sql)){
    // Set DB variables into local variables for easier use
    $news_id = $row["news_id"];
    $subject = $row["subject"];  
    $news_date = strftime("%b %d, %Y", strtotime($row["news_date"]));
    $news_artical = $row["news_artical"];  
    // Start filling the $xmlBody variable with looping content here inside the while loop
    // It will loop through 20 items from the database and render into XML format
   
    $xmlBody .= '
    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
    <cd>

    <title>' . $news_id . '</title>
    <artist>' . $subject . '</artist>
   <country>' . $news_date . '</country>
   <company>' . $description . '</company>
   
    </cd>

</catalog>';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
?>

Style sheet

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">

        <th align="left">Title</th>
        <th align="left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>

      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The first thing I wanted to do was test the script’s output. But I was too lazy to make a database for it so I commented out the db lines and used an array.

$db_arr = array(
				array('news_id'=>'12',
				'subject'=>'test one',
				'news_date'=>'2010-12-01',
				'news_artical'=>'Blah blah blah'),
				array('news_id'=>'34',
				'subject'=>'test two',
				'news_date'=>'2010-12-02',
				'news_artical'=>'Bleh bleh bleh'),
				array('news_id'=>'56',
				'subject'=>'test three',
				'news_date'=>'2010-12-03',
				'news_artical'=>'Blih blih blih'),
				);
foreach ($db_arr as $row) {

It made some rather sketchy XML

<?xml version="1.0" encoding="ISO-8859-1"?><XML>
    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
    <cd>
    <title>12</title>
    <artist>test one</artist>
   <country>Dec 01, 2010</country>
   <company>Blah blah blah</company>
    </cd>
</catalog>
    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
    <cd>
    <title>34</title>
    <artist>test two</artist>
   <country>Dec 02, 2010</country>
   <company>Bleh bleh bleh</company>
    </cd>
</catalog>
    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
    <cd>
    <title>56</title>
    <artist>test three</artist>
   <country>Dec 03, 2010</country>
   <company>Blih blih blih</company>
    </cd>
</catalog></XML>

I don’t know if it would work like that, but it bothers me so I took out the extra stylesheet lines and moved the root node to after the first one.

Then it was a simple matter of prepending the root node to the for-each select

You will need to use XSLTProcessor to apply the transformation and actually return the resulting XML. Otherwise, the transformation may be applied but the XML output will be the XML without the transformation. The transformation will just take affect in the browser, but not the source. That is part of the problem, there may be others.

Hi thanks for your replies but unfotunaly still didnt solve the problems i am trying to research more about (XSLTProcessor) i would still apreciate any further help from anyone

It workerd OK for me (using the array instead of the database).

What problems are you having after making the changes? Any error messages?

no i havent used the array because the data has to come from the database so and it does produce the xml structure but it just doesnt pick the stylesheet

Hi guys i’m back was on holiday. and still having problems with xml to be styled
xml doesnt pick the stylesheet can someone help me oout please you can check out how the data looks on my site here

this the code


<?php

// PHP file that renders perfect Dynamic XML for MySQL Database result sets

// Script written by Adam Khoury @ www.developphp.com - April 05, 2010

// View the video that is tied to this script for maximum understanding

// -------------------------------------------------------------------

header("Content-Type: text/xml"); //set the content type to xml

// Initialize the xmlOutput variable

$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';

$xmlBody .= "<XML>";

// Connect to your MySQL database whatever way you like to here

mysql_connect("localhost","root","") or die (mysql_error());

mysql_select_db("admin") or die ("no database");

// Execute the Query on the database to select items(20 in this example)

$sql = mysql_query("SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20");

while($row = mysql_fetch_array($sql)){

    // Set DB variables into local variables for easier use

    $news_id = $row["news_id"];

    $subject = $row["subject"];  

    $news_date = strftime("%b %d, %Y", strtotime($row["news_date"]));

    $news_artical = $row["news_artical"];  

    // Start filling the $xmlBody variable with looping content here inside the while loop

    // It will loop through 20 items from the database and render into XML format

   

    $xmlBody .= '

    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

<catalog>

    <cd>



    <title>' . $news_id . '</title>

    <artist>' . $subject . '</artist>

   <country>' . $news_date . '</country>

   <company>' . $description . '</company>

   

    </cd>



</catalog>';

} // End while loop

mysql_close(); // close the mysql database connection

$xmlBody .= "</XML>";

echo $xmlBody; // output the gallery data as XML file for flash

?>


stylesheet

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited with XML Spy v4.2 -->

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>



<xsl:template match="/">

  <html>

  <body>

  <h2>My CD Collection</h2>

    <table border="1">

      <tr bgcolor="#9acd32">



        <th align="left">Title</th>

        <th align="left">Artist</th>

      </tr>

      <xsl:for-each select="catalog/cd">

      <tr>

        <td><xsl:value-of select="title"/></td>

        <td><xsl:value-of select="artist"/></td>

      </tr>



      </xsl:for-each>

    </table>

  </body>

  </html>

</xsl:template>

</xsl:styl

The root tag looks misplaced

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

do u mean replace

header("Content-Type: text/xml "); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<XML>";

with this

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

I’m geussing u meant like this the at the bottom

	
    $xmlBody .= '



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



<catalog>	
	<cd>
	

    <title>' . $news_id . '</title>
	<title>' . $subject . '</title>
	<title>' . $news_date . '</title>
	<title>' . $news_artical . '</title>
   
 
   
		</cd>

</catalog>	
';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
?>

gave the above error
XML Parsing Error: XML or text declaration not at start of entity
Location: http://localhost/old_xampp_work/c3232585/rssdatabase.php
Line Number 5, Column 1:<?xml version=“1.0” encoding=“ISO-8859-1”?><XML>
^

I meant the opening root node - <XML> - should come after the prolog - the <?xml declaration and <?xml-stylesheet declaration

this what i change to now but i get error higlighted on the 3 line i’m not sure if its cz i am missing an php open or or close tag

      $xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?> ';

      $xmlBody .= "<catalog> ";

$xmlBody .= "<?xml-stylesheet type="text/xsl" href="books06.xsl"?> ";

i’m not sure if its cz i am missing an php open or or close tagthis how evrything looking now

<?php


      header("Content-Type: text/xml"); //set the content type to xml

      // Initialize the xmlOutput variable

      $xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?> ';

      $xmlBody .= "<catalog> ";

$xmlBody .= "<?xml-stylesheet type="text/xsl" href="books06.xsl"?> ";
?>
      $xmlBody .= "
<?php // Connect to your MySQL database whatever way you like to here

mysql_connect("localhost","root","") or die (mysql_error());

      mysql_select_db("admin") or die ("no database");

      // Execute the Query on the database to select items(20 in this example)

      $sql = mysql_query("SELECT * FROM news ORDER BY news_date DESC LIMIT 0, 20");

      while($row = mysql_fetch_array($sql)){

     // Set DB variables into local variables for easier use

      $news_id = $row["news_id"];

      $subject = $row["subject"];

      $news_date = strftime("%b %d, %Y", strtotime($row["news_date"]));

      $news_artical = $row["news_artical"];
      // Start filling the $xmlBody variable with looping content here inside the while loop
      // It will loop through 20 items from the database and render into XML format

       

      $xmlBody .= '

      <cd>

       

      <title> ' . $news_id . '</title>

      <artist> ' . $subject . '</artist>

      <country> ' . $news_date . '</country>

      <company> ' . $description . '</company>

       

      </cd> ';

      } // End while loop

      mysql_close(); // close the mysql database connection

      $xmlBody .= "</catalog> ";

      echo $xmlBody; // output the gallery data as XML file for flash

      ?>

It’s not easy randomly changing things around in hopes that you’ll get it right and it will work. Though I suppose that method does work sometimes. :wink:

One thing you need to be comfortable with is correct XML structure

First is the “prolog” i.e.
the XML declaration - <?xml
then the stylesheet declaration - <?xml-stylesheet

Then the root node - <XML> or <catalog> *
followed by the first of the the “sets” i.e. <catalog> or <cd> * - with their nested nodes.

Ending with the closing root node - </XML> or </catalog> *

*I’m not sure what your node names are because the XSL file is named “books”.