note 15094 deleted from ref.xslt by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:31:11 +0000 |
| Subject: | note 15094 deleted from ref.xslt by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207333@lists.php.net to get a copy of this message | ||
Note Submitter: sandro_zic at web dot de
----
[Editor's note: moved this note here where it belongs - jmcastagnetto@php.net]
This shows how you can use the XSLT functions 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 we assign the value of the parameter to an array in PHP:
-----------------------------------------
$xslt_params["test"] = "Run-time parameter.";
-----------------------------------------
The parameter is envoced in XSL by
-----------------------------------------
<h1>Value of run-time parameter:</h1>
<b><xsl:value-of select="$test" /></b>
<hr/>
-----------------------------------------
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 in PHP:
-----------------------------------------
$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:
-----------------------------------------
<h1>Dynamic content from document buffer 1:</h1>
<xsl:for-each select="document('arg:/buffer1')/TestBuffer/Message">
<b><xsl:value-of select="Content" /></b><br/>
</xsl:for-each>
<hr/>
-----------------------------------------
To get the whole thing running, following functions have to be executed:
-----------------------------------------
$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 -->
<h1>Value of run-time parameter:</h1>
<b><xsl:value-of select="$test" /></b>
<hr/>
<!-- dynamic content -->
<h1>Dynamic content from document buffer 1:</h1>
<xsl:for-each select="document('arg:/buffer1')/TestBuffer/Message">
<b><xsl:value-of select="Content" /></b><br/>
</xsl:for-each>
<hr/>
</xsl:template>
</xsl:stylesheet>
-----------------------------------------