Jump to content

PHP, XML, XSL


denoteone

Recommended Posts

Trying to get everything in one file.  I have an external data source that has 3 different XML files located on it. I am writing a  php script that sits on my server and based on the GET variable I want to read a specific XML file and add a specific XSL style sheet to it then echo it to the browser.  What I cant have is 3 different .XSL files siting on my server or the data source. So if there is some way I can save the XSL info as a variable and somehow apply all from my one PHP script that would be great. So far this is what I have:

 

<?PHP

$view = $_GET['view'];

$xml_data = "http://externalserver.com/" . $view .".xml"; 
$handle = fopen($xml_data, 'r'); 
$Data = fread($handle, 512); 
fclose($handle); 

$old ='<?xml version="1.0" encoding="utf-8"?>';

//Right here is my biggest challange I dont wont the style sheets on the same server or any others for that matter since the server I am running this script on can not get out to the internet since it is behind a firewall. I was thinking of creating the XSL files on the fly and then saving them next to this script. But I would like to keep everything in just 1 file if at all possible.

$new ='<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="style.xsl" type="text/xsl"?>';

$final_xml = str_replace($old, $new, $Data);

echo $final_xml;

?>

 

the url would be http://www.myserver.com/getxml.php?view=weather

Link to comment
Share on other sites

Ok so I am calling the same page to set the .XSL style sheet but am getting the dreaded "Error loading stylesheet: Parsing an XSLT stylesheet failed." so the url calls the script that gets the xml file based on the GET variable and it adds the style sheet by calling the same page with a new GET variable.

 

below is my script is this possible?

<?PHP
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);
header('Content-type: text/xml');

$view = $_GET['view'];

if(isset($_GET['xsl_style']))
{
$style = '<?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>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>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>';

echo $style;

}else{

$xml_data = "http://www.website.com/STRATA/" . $view .".xml";
$handle = fopen($xml_data, 'r');
$Data = fread($handle, 10000);
fclose($handle);

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

$new ='<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="http://www.website.com/STRATA/get_view.php?xsl_style='. $view .'" type="text/xsl"?>';

$final_xml = str_replace($old, $new, $Data);

echo $final_xml;

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.