-
Notifications
You must be signed in to change notification settings - Fork 0
topics.scriptlets.xml
Instead of writing the content directly to the response, the
scriptlet.xml.XMLScriptlet
can be used, to build a XML tree which
is transformed using a XSL file to generate the HTML output.
The scriptlet.xml.XMLScriptlet
class is derived from the scriptlet.HttpScriptlet
class and provides additional functionality for processing HTTP requests.
Basically the behaviour is the same. The difference between the two classes is,
that the XMLScriptlet
is working with a XML tree and a XSL file, which is
used to do the transformation (XSLT).
A simple implementation looks like this:
<?php
uses('scriptlet.xml.XMLScriptlet');
class MyScriptlet extends XMLScriptlet {
public function doGet($request, $response) {
$response->addFormResult(new Node('class', $this->getClassName());
$response->addFormResult(new Node('name', $request->getParam('name'));
return parent::doGet($request, $response);
}
public function _setStylesheet($request, $response) {
$response->setStylesheet('welcome.xsl');
}
}
?>
Additionally to this you need to create a XSL file (here welcome.xsl
) to do
the transformation with the XML tree (build by using the response object and
XMLScriptletResponse::addFormResult()
):
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://exslt.org/functions"
>
<xsl:template match="/">
<html>
<head><title>Hello World scriptlet (<xsl:value-of select="/formresult/title"/>)</title></head>
<body>
<h1>Hello <xsl:value-of select="/formresult/name"/></h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The doGet()
function is adding some nodes to the response XML tree, which
results in a XML tree like this:
<formresult>
<title>package.MyScriptlet</title>
<name>Foo Bar</name>
</formresult>
It adds a node "title" which contains the FQCN of the scriptlet class, and a node "name" which contains the name passed as GET parameter "name".