PDA

View Full Version : Passing External Values To Variables In An XSL Stylesheet



Streakfury
19 Jan 2006, 05:43 PM
Evening all,

Lately I've been trying to figure out how to pass external values into XSLT stylesheet variables. Is this even possible?

From what I've read about XSLT variables, they're not 'variables' in the traditional sense of the word, as once their value has been defined it can't be changed, but there must still be some use for them.

Basically, I need to pass the value of a drop-down menu on an HTML form into a variable in an XSLT stylesheet. I've been reading up on it for days and come up blank. Any thoughts?

Thanks for your help.

- Streak

:)

Streakfury
22 Jan 2006, 08:42 PM
Well I guess I didn't make a whole lot of sense there, but I think I've found the basic script that does the job. For anyone that's interested:



<?php

// XML string
$xml = '<?xml version="1.0"?>
<para>
change me
</para>';

// XSL string
$xsl = '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" indent="no"
omit-xml-declaration="yes" media-type="text/html"/>
<xsl:param name="myvar"/>
<xsl:param name="mynode"/>
<xsl:template match="/">
My PHP variable : <xsl:value-of select="$myvar"/><br />
My node set : <xsl:value-of select="$mynode"/>
</xsl:template>
</xsl:stylesheet>';


$xh = xslt_create();

// the second parameter will be interpreted as a string
$parameters = array (
'myvar' => 'test',
'mynode' => '<foo>bar</foo>'
);

$arguments = array (
'/_xml' => $xml,
'/_xsl' => $xsl
);

echo xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments, $parameters);

?>

:)