note 13572 deleted from function.xslt-run by nlopess
| From: | nlopess@php.net | Date: | Sat, 26 Jun 2004 19:07:40 +0000 |
| Subject: | note 13572 deleted from function.xslt-run by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71803@lists.php.net to get a copy of this message | ||
Note Submitter: Sandro_dot_Zic@oc4-science_dot_org
----
This shows how to use xslt_run to add dynamic content to the result tree, i.e. how to hand over
parameters to the XSL stylesheet and write to Sablotron's document buffer.
First, assign the value of a parameter to an array:
$xslt_params["test"] = "Run-time parameter.";
The parameter is envoced in XSL by
<xsl:value-of select="$test" />
Don't forget to define the parameter at the beginning of your stylesheet:
<xsl:param name="test"/>
How can we add dynamic content from a database to the XSLT output? In Sablotron, this is done using
the document buffer function.
Retrieve a table's content, store it in a XML structure assigned to an array:
$xslt_args["buffer1"] =
"
<TestBuffer>
<Message>
<Content>
This is message 1 of buffer 1.
</Content>
</Message>
<Message>
<Content>
This is message 2 of buffer 1.
</Content>
</Message>
</TestBuffer>
";
This is how you add the buffer to the result tree in your XSL document:
<xsl:for-each select="document('arg:/buffer1')/TestBuffer/Message">
<xsl:value-of select="Content" />
</xsl:for-each>
Now, use xslt_run to start processing an XML file, the parameter, and the document buffer:
$processor = xslt_create();
xslt_run ($processor, "file://".$DOCUMENT_ROOT.$xsl_file,
"file://".$DOCUMENT_ROOT.$xml_file, "arg:/_result", $xslt_params, $xslt_args);
$result = xslt_fetch_result ($processor);
echo $result;
The complete XSL file looks like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" version="1.0" standalone="yes"
indent="yes" />
<!-- define parameter -->
<xsl:param name="test"/>
<xsl:template match="/">
<!-- run-time parameter -->
<xsl:value-of select="$test" />
<!-- dynamic content -->
<xsl:for-each select="document('arg:/buffer1')/TestBuffer/Message">
<xsl:value-of select="Content" />
</xsl:for-each>
<!-- content from XML file -->
...
</xsl:template>
</xsl:stylesheet>