Example E-3. paramtaglib.xsp
<xsp:page
xmlns:xsp="https://apache.org/xsp/core/v1"
xmlns:param="https://axkit.org/NS/xsp/param/v1"
language="Perl"
>
<page>
<xsp:logic>
if (<param:name/>) {
<xsp:content>
Your name is: <param:name/>
</xsp:content>
}
else {
<xsp:content>
<form>
Enter your name: <input type="text" name="name" />
<input type="submit"/>
</form>
</xsp:content>
}
</xsp:logic>
</page>
</xsp:page>
The most significant thing about this example is how we freely mix
XML tags with our Perl code, and the XSP processor figures out the
right thing to do depending on the context. The only requirement is
that the XSP page itself must be valid XML. That is, the following
would generate an error:
<xsp:logic>
my $page = <param:page/>;
if ($page < 3) { # ERROR: less-than is a reserved character in XML
...
}
</xsp:logic>
We need to convert this to valid XML before XSP can handle it. There
are a number of ways to do so. The simplest is just to reverse the
expression to if (3 > $page), because the
greater-than sign is valid within an XML text section. Another way is
to encode the less-than sign as <, which
will be familiar to HTML authors.
The other thing to notice is the <xsp:logic>
and <xsp:content> tags. The former defines a
section of Perl code, while the latter allows you to go back to
processing the contents as XML output. Also note that the
<xsp:content> tag is not always needed.
Because the XSP engine inherently understands XML, you can omit the
<xsp:content> tag when the immediate child
would be an element, rather than text. For example, the following
example requires the <xsp:content> tag:
<xsp:logic>
if (<param:name/>) {
# xsp:content needed
<xsp:content>
Your name is: <param:name/>
</xsp:content>
}
</xsp:logic>
But if you rewrote it like this, it wouldn't,
because of the surrounding non-XSP tag:
<xsp:logic>
if (<param:name/>) {
# no xsp:content tag needed
<p>Your name is: <param:name/></p>
}
</xsp:logic>
Note that the initial example, when processed by only the XSP engine,
will output the following XML:
<page>
<form>
Enter your name: <input type="text" name="name" />
<input type="submit"/>
</form>
</page>
This needs to be processed with XSLT or XPathScript to be reasonably
viewable in a browser. However, the point is that you can reuse the
above page as either HTML or WML just by applying different
stylesheets.