How do I pass a variable from a URL to a XSL stylesheet?
January 8, 2004 5:19 PM   Subscribe

How do I pass a variable from a URL to a XSL stylesheet? I'm using PHP with Sablotron to parse the static XML and XSL files. [More inside]

I've been trying to find a way to get variables from the URL to the XSL style sheet so I can match a particular day in a week long weather report.

Say the url is:
http://riffola.com/weather/?location=11355&forecast&day=Friday

I can use PHP to get the right XML file using the location, but I dont know how to pass the day variable info to the XSL file.

Now I want to use that day variable from the URL to grab only Friday's weather report.

Here's a sample XML file of the weather report:
http://riffola.com/weather/cache/11355forecast.xml

As you can see <day d="2" t="Friday" dt="Jan 9"> has Friday in it, and I don't want to list any other day's details.

I want to use Sablotron to parse the XML and XSL. I can't find a way to do this. Is this even possible? Or should I just cave in and create 7 stylesheets for each day of the week?

I've had suggestions about creating a new XML from the cached file. I am not sure if that's going to be less effort than making 7 stylesheets for each day.
posted by riffola to Computers & Internet (4 answers total)
 
Best answer: Put

<xsl:param name="day">Some sensible value</xsl:param>

at the top inside <xsl:stylesheet> and then from PHP pass the parameters in using a named array, as in

$xslt_params['day'] = $something;
xslt_transform ($xsl_file, $xml_file, $result_file, $xslt_params);

posted by holloway at 5:29 PM on January 8, 2004


Best answer: I'm not sure how you're calling sablatron, but if you're using the command-line sabcmd, you can pass variables to the stylesheet by putting $VARNAME=value on the command-line after the xml file.

In the stylesheet, put <xsl:param name="VARNAME"/>. Then you can use that variable in the stylesheet.
posted by Emanuel at 5:31 PM on January 8, 2004


Argh... rather than 'Some sensible value' I meant 'Some sensible default'
posted by holloway at 5:39 PM on January 8, 2004


Response by poster: Thank you holloway and Emanuel!!!

I used the following to process it:

URL:
/?report=forecast&forday=Monday

I'll use .htaccess to make the URLs pretty.

Used the following to parse in PHP:
$result = @xslt_process($weatherparser, 'cache/'.$this->location.$GLOBALS['report'].'.xml', $this->report.'.xsl', NULL, NULL,$xslt_params);

Put this at the top of my style sheet:
<xsl:param name="forday" />

printed it in the template with:
<xsl:value-of select="$forday" />
posted by riffola at 6:27 PM on January 8, 2004


« Older A DVD codec for Win2K?   |   "Asshat" - origination and definition? Newer »
This thread is closed to new comments.