Sun Sep 19 21:17:37 2004

XSP

Introduction

XSP stands for eXtensible Server Pages (not XML Server Pages, although that might apply equally well). This means that XSP is a server pages technology a similar idea to ASP or PHP or JSP - a file on your web server that executes some code to produce results.

XSP is however better than those other solutions because the output of an XSP page is structured XML, meaning you can then pipe it through XSLT (or any of the other AxKit language modules) to produce HTML or any other kind of output format depending on the client making the request. So you get all the structured data benefits, and the separation of content from presentation that XML and AxKit bring you, while still having easy to write dynamic code.

In diagramatic terms the pipeline might look like this:

Request --> [XSP] -- (XML) --> [XSLT] -- (HTML) --> Browser

[] - Engine component
() - Output format

The above is the most common pipeline pattern when using XSP, however you are free to choose your own pipeline pattern as you see fit.

So what does an XSP page look like?

Again, let's start off simply. First here's how we might configure AxKit to deliver XSP pages as above:

AxAddRootProcessor application/x-xsp NULL \
   {http://apache.org/xsp/core/v1}page
AxAddRootProcessor text/xsl output2html.xsl \
   {http://apache.org/xsp/core/v1}page

Now here's hello-world.xsp:

<xsp:page xmlns:xsp="http://apache.org/xsp/core/v1">
  <output>
    Hello World. The time of request is:
    <xsp:expr>scalar localtime</xsp:expr>
  </output>
</xsp:page>

And the output from that would be something like:

<output>
  Hello World. The time of request is: Wed Feb  5 10:46:11 2003
</output>

Finally we could design our output2html.xsl to convert that to nicely formatted HTML.

Now we can break the page down into components:

  • The <xsp:page> tag.

    This tag just starts the page. You don't actually need it (you can skip straight to the <output> tag if you want), but there is a use for having the tag which you'll discover as you read more :-)

  • The <output> tag.

    This tag is what we call the "User Root" tag. It is the root tag in the output from running the XSP (i.e. the XSLT stage in the diagram above will not see the <xsp:page> tag).

  • The plain text "Hello World..."

    Like other Server Pages technologies, by default text just goes straight to the output.

  • The <xsp:expr> tag.

    This tag indicates an expression (in Perl) that should get run at request time. XSP is intelligent about what to do with the results of <xsp:expr> tags and in this case it attaches the resulting string to the output.

Taglibs to Make Life Easier

Anyone familiar with Server Pages technologies knows that it very quickly becomes unmanagable to embed code in your pages. You end up with a bigger mess than just coding in plain CGI scripts very easily.

In order to combat this problem XSP allows you to very easily create what are called Tag Libraries. These are basically libraries of tags that do useful things. For example the AxKit Wiki-> is basically a tag library held together with an XSP page for glue.

Please see the separate page for more details.

Namespaces Everywhere

Another important thing about XSP is its use of XML Namespaces->. It does this so that you can cleanly delineate taglibs. Consider it to be like perl namespaces where you have IO::File->new() in the IO::File namespace so that it doesn't trample on things in other namespaces.

An XML namespace is very simple - it is simply a URI, though the URI does not have to map to anything important - it doesn't even need to really exist (a simple way to work if this confuses you is to make your namespace URIs URNs, which is a private definition that looks something like "urn:mytaglibnamespace").

Finally if you don't like all the extra work that this involves (mapping namespaces to modules, loading modules and getting everything right in your XSP page) you can specify the namespace as a module by setting the config variable:

PerlSetVar XSPResNamespaces 1

And in your XSP page you can now use:

<xsp:page xmlns:wiki="res:perl/AxKit/XSP/Wiki">

Which will use the taglib defined in the module AxKit::XSP::Wiki.

Where to now?

From here check out how to use taglibs, or see how to write your own taglib, and finally there's the full blown XSP Guide which documents the core of XSP in much more detail.


Edit This Page / Show Page History /