View Single Post
Old 12-30-2011, 03:44 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This will be interesting. XSL itself has the ability to modify the date format in 2.0, but PHP will be dependent on libxslt version.
Rather than rely on the version of XSL 2.0 being available, I'd say stick to a PHP route to do this by registering a PHP function instead. The XML guys would be able to verify if you can fetch just the text itself out of the node in XSLT, but I can't seem to get it to work. So I'll have to write a custom function instead to pull the node value out to format it (otherwise I could directly call date() and strtotime()).
I used this. I pulled out the current weather since it was not meant to be parsed with the rest, but shares some common elements to match. You'll need to write a specific template and a call for it yourself. This is what I used, should be compatible with 5.1+ with an xslt 1.0 processor:
PHP Code:
<?php

function dateToDayOfWeek($xmlDate$useFormat 'l')
{
    
$rd 'Unknown';
    if (
count($xmlDate) == 1)
    {
        
$element $xmlDate[0];
        if (
$element instanceof DOMElement)
        {
            
$rd date($useFormatstrtotime($element->nodeValue));
        }
    }
    return 
$rd;
}

date_default_timezone_set('UTC');

$xslDoc = new DOMDocument();
$xslDoc->load("weathertest.xsl");

$xmlDoc = new DOMDocument();
$xmlDoc->load("http://free.worldweatheronline.com/feed/weather.ashx?q=milan,italy&format=xml&num_of_days=5&key=af8b2fc417222733111712");

$proc = new XSLTProcessor();
$proc->registerPHPFunctions('dateToDayOfWeek');
$proc->importStylesheet($xslDoc);
echo 
$proc->transformToXML($xmlDoc);
?>
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="data">
<html>
	<head><title>Weather</title></head>
	<body>
		<table border="1">
			<tr>
				<xsl:apply-templates select="./weather" />
			</tr>
		</table>
	</body>
</html>
</xsl:template>

<xsl:template match="weather">
	<td>
		<p><xsl:value-of select="php:function('dateToDayOfWeek', date)" /></p>
		<p><xsl:apply-templates select="./weatherIconUrl" /></p>
	</td>
</xsl:template>

<xsl:template match="weatherIconUrl">
	<img>
		<xsl:attribute name="src">
		<xsl:value-of select="." />
		</xsl:attribute>
	</img>
</xsl:template>

</xsl:stylesheet>
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
JonesJ (12-30-2011)