Re: [xsl] Streamline xslt

Subject: Re: [xsl] Streamline xslt
From: Bill Humphries <bill@xxxxxxxxx>
Date: Wed, 25 Sep 2002 19:24:21 -0700
On Wednesday, September 25, 2002, at 10:26 AM, Geoff wrote:

The first thought I had was use includes. Php includes won't work
because the first thing the Sablotron processor sees is <?php //php code
?>. I assume the same problem occurs with Apache includes.
XSL includes must be well formed xml documents so that wouldn't work
either.

You might want to look at Sablotron's ablity to fetch named buffers into a transform using the Xpath document () function.


So you set up your transform like this:

$xh = xslt_create ();

$header = /* PHP code which generates well-formed XML */
$footer = /* PHP code which generates well-formed XML */

/* Now place these items in an array we'll pass to xslt_process */
$args = array(
"/header" => $header,
"/footer" => $footer
);

/* And any parameters your defining in your XSLT */
$params = array(
"foo"	=> $untaintedFoo,
"bar"	=> $untaintedBar
);

$source = "/path/to/xml/file.xml"; // If this is user input, you did untaint it, didn't you?
$xslt = "/path/to/xslt.xsl";


$result = xslt_process ($xh,$source,$xslt,NULL,$args,$params);

if ($result)
{
	echo $result;
}
else
{
	/* error handling */
}

Inside your XSLT, you'll do something like

<xsl:template match="/">
	<xsl:apply-templates select="document('arg:/header')" />
	<xsl:apply-templates />
	<xsl:apply-templates select="document('arg:/footer')" />
</xsl:template>

This is also described in the user-comments for the xlst_process function on the PHP.net site.

Things to remember are that you should probably have an assertion on $header and $footer to insure they are well-formed. Or use a trusted library/class to generate the XML strings. Catting together the XML is dicey unless you can near certain that you won't end up with a malformed XML string.

In the case where I implemented this, $header and $footer referred to files in the file system, and the path to the file was contigent on the value of $_SERVER['REQUEST_URI'], so we had to assert that the file existed, and was well formed.

----
Bill Humphries <bill@xxxxxxxxx>
http://www.whump.com/moreLikeThis/


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread