Unable to style xml

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
lisa007
Forum Newbie
Posts: 9
Joined: Thu Apr 08, 2010 3:00 pm

Unable to style xml

Post by lisa007 »

Hi i have a while loop which displaying data frm the database but i am trying to style but it doesnt seem to be workin help please


this the xml

Code: Select all

<?php
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ http://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
?>
this is the style sheet

Code: Select all

<?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>
Last edited by Weirdan on Mon Dec 06, 2010 2:56 pm, edited 1 time in total.
Reason: fixed syntax highlighting
lisa007
Forum Newbie
Posts: 9
Joined: Thu Apr 08, 2010 3:00 pm

Re: Unable to style xml

Post by lisa007 »

over 102 views noone can give a me a tip what am i doing wrong please desperate help??
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Unable to style xml

Post by Weirdan »

it doesnt seem to be workin help
...tells exactly nothing about the issue you're facing. Please provide more details. Posting the code was a good thing to do, but it's not enough.

How do you know it's not working? What did you do, what results you expected, what did you get instead?
lisa007
Forum Newbie
Posts: 9
Joined: Thu Apr 08, 2010 3:00 pm

Re: Unable to style xml

Post by lisa007 »

ok the problem i'm having is that xml doesnt pick up the style sheet so am thinking either i have set it up the code to pick up the style sheet wrong can anyone see if thats the first case scenario you can see how is current display herebut i want to display using style sheet another thing is i dnt know if maybe i set up the stylesheet wrong desperate help please thanks in advance
lisa007
Forum Newbie
Posts: 9
Joined: Thu Apr 08, 2010 3:00 pm

Re: Unable to style xml

Post by lisa007 »

I'm getting this error i cnt seem to find a solution for it help please...


XML Parsing Error: not well-formed
Location: http://localhost/old_xampp_work/c323258 ... tabase.php
Line Number 4, Column 1:$xmlBody .= "
^

Code: Select all

<?php
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ http://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 .= "<catalog>";

$xmlBody .= '<?xml-stylesheet type="text/xsl" href="cdcatalog.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
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Unable to style xml

Post by califdon »

This more recent listing of your script shows a fragment of a line:

$xmlBody .= "

That's what the error message is telling you. That wasn't present in the first listing you posted.
lisa007
Forum Newbie
Posts: 9
Joined: Thu Apr 08, 2010 3:00 pm

Re: Unable to style xml

Post by lisa007 »

no it wasnt cz i couldnt figure out what was wrong with it so i followed another tutorial on another script which once again no luck with it. the problem with this one is that i cant seem to be able to insert the link to stylesheet it keeps given me error when i try to insert this line

$xml_output = "<?xml-stylesheet type="text/xsl" href="class.xsl"?>\n";
buts the version line works fine

i wana be able to style it tru xsl

Code: Select all

<?php

header("Content-type: text/xml");

$host = "localhost";
$user = "root";
$pass = "";
$database = "admin";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM news";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";


$xml_output = "<?xml-stylesheet type="text/xsl" href="class.xsl"?>\n";     // error on this line without i'm unable to style the loop 


$xml_output .= "<entries>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "\t<entry>\n";
    $xml_output .= "\t\t<date>" . $row['news_id'] . "</date>\n";
        // Escaping illegal characters
        $row['text'] = str_replace("&", "&", $row['text']);
        $row['text'] = str_replace("<", "<", $row['text']);
        $row['text'] = str_replace(">", ">", $row['text']);
        $row['text'] = str_replace("\"", """, $row['text']);
    $xml_output .= "\t\t<text>" . $row['subject'] . "</text>\n";
	$xml_output .= "\t\t<mood>" . $row['news_artical'] . "</mood>\n"; 
    $xml_output .= "\t</entry>\n";
}

$xml_output .= "</entries>";

echo $xml_output;

?> 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Unable to style xml

Post by califdon »

lisa007 wrote:no it wasnt cz i couldnt figure out what was wrong with it so i followed another tutorial on another script which once again no luck with it.
Mixing scripts can be a bad way to try to get a script to work because the 2 scripts may approach the task differently and conflict with each other, unless you are really an experienced programmer. I am dodging your question because I don't use XML, so I was just reacting to the error message you presented, which was about the incomplete line I referred to.

Is your XML stylesheet named "class.xsl" and is it in the same directory as the script? That's the first thing to check. But if you're getting error messages, you have to fix those problems first.
Post Reply