Forum Moderators: open
I have really bitten off way more than I can chew on this one. A site Im developing requires a simple cms system. It will never have more than 20-30 pages or large amounts of traffic. I am awhere of the pros and cons in creating one in xml rather than php which would be my typical option. However I think it will be fine for this particular website.
My problem is this, I have managed to get verious pieces of code but have one big problem. In regards to images....
The basic function of the cms is up and working fine but Im at an utter loss as to how to use images in it. It creates xml files which are loaded by a single php page. Which then echos the data in the places necessary. And as the server is only running php4 the only option I can see is to style the xml file in xsl. But this still creates a problem as the php page doesnt include the xml file directly it just reads it and the values it needs so at this point this wouldnt work. Would this be a suitable way to display the pages? Im I right in the theory that I cant just use a echo to display an image url like how I can a text data entry ideally I would like to use something like <?php echo $image; ?> ?
Heres part of the php page Im using to load the xml
<?php
// incase the server moves to php5
if (version_compare(PHP_VERSION,'5','>=')&&extension_loaded('xsl'))
require_once('include/xslt-php4-to-php5.php');
session_start();
function extractText($array){
if(count($array) <= 1){
//we only have one tag to process!
for ($i = 0; $i<count($array); $i++){
$node = $array[$i];
$value = $node->get_content();
}
return $value;
}
}
//pull in the XML file
if ($file == ""){
echo "<h2>You didn't choose a file to edit!</h2>";
echo "<a href=\"index.php\">Go back to index and choose a file</a>";
exit;
} else {
$open = "./xml/" . $file;
$xml = domxml_open_file($open);
$root = $xml->root();
$id = $root->get_attribute("id");
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
$stat_array = $root->get_elements_by_tagname("status");
$status = extractText($stat_array);
$a_array = $root->get_elements_by_tagname("author");
$author = extractText($a_array);
$e_array = $root->get_elements_by_tagname("email");
$email = extractText($e_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
$kl_array = $root->get_elements_by_tagname("keywords");
$keywords = extractText($kl_array);
?>
If it is possible to just include the xml file how could I do that without directly calling its filename? How could I use the code I already have to include a dynamic xml file?
Is this the best way to get image nodes working, how would you go about this?
And finally Im hvaing trouble getting the xml file to display the images. What is the correct format to do this in xslt and php4?
Thank you in advance.
Instead of building a complex system of PHP, XML and XSLT, what if you were to just make the site out of HTML files? Would the site be less maintainable? Does your proposed CMS accomplish one or all of the criteria above?
OK but that didn't answer your question
Since you're considering XSLT, you could use it to generate the entire page. Forget about picking through the XML with PHP and displaying fragments using print(). The only PHP code you'll need is the routine that does your XSLT transformation.
<?php
$xslt_processor = xslt_create();
// Perform the transformation
$result = xslt_process($xslt_processor, 'example.xml', 'example.xsl', NULL);
if ($result) {
echo $result;
}else {
echo xslt_error($xslt_processor) . " ¦ " . xslt_errno($xslt_processor);
}
xslt_free($xslt_processor);
?>
your XSLT template can contain the entire page layout, including the <html>, <head>, <body>, etc. and where you were previously picking through the XML with PHP, now you will insert the XML data using <xsl:value-of select="xpath_expression_here" />
Will this be your first time using XSLT?
If so, I've got to ask - does this project have a deadline?
[edited by: httpwebwitch at 9:06 pm (utc) on May 13, 2008]
Yes the cms is to accomplish all of your questions raised, I have to impliment them in one way or another for many of the sites we design and produce. I had also considered a simple html/txt include methord but while simplistic this project requires more than that methord could provide.
The normal option would be to develop a php/mySql system, but being that I would like a better understand xml I thought I would take that path for this project. It does have a reasonably tight deadline and while it is my first time with XSLT I have already had a small experiment with it (despite the fact I cant get images to display from an xml file).
Because I am working from a basic cms xml template/example already most of the hard work has been done and as it stands it is more of less fully functional, the problem lies in the fact that it doesn't support image placement.
I suspected I would need to use XSLT to generate the entire page but Im not to sure how to get it to function in this case. Currently the article.php file (posted above) is the display template and the xml files are dynamically loaded into it so your url ends up looking something like this...
.com/Article.php?file=test.xml
.com/Article.php?file=test2.xml
etc...
So as the xml file is not being displayed directly (essentially its still a php file) the XSLT wont style the page.
My second problem is that because the xml file is being dynamically loaded it means that I cant hard code any links or references to XSLT except into each xml file them self.
I could write an include for the XSLT file into the xml as these are editable, but then how can it become styled when its displayed through a php page like the examples above. Thats why I was thinking perhaps as a php include? Would it then use the XSLT style its assigned?
With my little skill, plus the complicated nature of the project I hope that made some sense. Because of the time frame I would appreciate all feed back, suggestions and help.
.com/Article.php?file=test.xml
.com/Article.php?file=test2.xml
that's less than ideal...
why not
articles/test1.php
articles/test2.php
It's easy to load an xml file which has the same name as the current page.
I was also imagining that all (or a majority of) your pages could use a common XSLT template. Is this not so?
I cant hard code any links or references to XSLT
I could write an include for the XSLT file into the xml
$result = xslt_process($xslt_processor, 'example.xml', 'example.xsl', NULL);
Think of your page as a martini. XML is your gin, and XSLT is the vermouth.
Everything of worth in the drink is in the gin, but without the vermouth it's just gin, not a martini. PHP is the shaker that mixes the two ingredients together, and the result is something which is neither gin nor vermouth.
XML -> as storage. anything that is unique on a page is stored in an XML file. use it to store data only, not markup or visual layout elements.
XSLT -> a template you use to transform the XML into an HTML document. This is where markup and layout elements belong.
PHP -> the mixer that combines the two ingredients on the server, then delivers the result.
as for trouble with images, try something like this in your XSLT:
<img>
<xsl:attribute name="src">
<xsl:value-of select="xpath_to_the_image_URL" />
</xsl:attribute>
</img>
good luck
If you want, you can use a standard html img tag, and then use this for an xslt transformation:
<xsl:template match="img">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="@src" />
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="@alt" />
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@title" />
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="@width" />
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="@height" />
</xsl:attribute>
</xsl:element>
</xsl:template> [edited by: Blue_Jeans at 2:46 am (utc) on May 15, 2008]