XML/XSLT
Custom Tags with XMLSubsMatch
Before XML, there was the need to make HTML markup smarter.
Apache::ASP gives you the ability to have a perl
subroutine handle the execution of any predefined tag,
taking the tag descriptors, and the text contained between,
as arguments of the subroutine. This custom tag
technology can be used to extend a web developer's abilities
to add dynamic pieces without having to visibly use
<% %> style code entries.
So, lets say that you have a table that
you want to insert for an employee with contact
info and the like, you could set up a tag like:
<my:new-employee name="Jane" last="Doe" phone="555-2222">
Jane Doe has been here since 1998.
</my:new-employee>
To render it with a custom tag, you would tell
the Apache::ASP parser to render the tag with a
subroutine:
PerlSetVar XMLSubsMatch my:new-employee
Any colons, ':', in the XML custom tag will turn
into '::', a perl package separator, so the my:employee
tag would translate to the my::employee subroutine, or the
employee subroutine in the my package. Any dash "-" will
also be translated to an underscore "_", as dash is not valid
in the names of perl subroutines.
Then you would create the my::employee subroutine in the my
perl package or whereever like so:
package my;
sub new_employee {
my($attributes, $body) = @_;
$main::Response->Include('new_employee.inc', $attributes, $body);
}
1;
<!-- # new_employee.inc file somewhere else, maybe in Global directory -->
<% my($attributes, $body) = @_; %>
<% for('name', 'last', 'phone') { %>
<%=ucfirst $_ %>: |
<%= $attributes->{$_} %> |
<% } %>
<%= $body %> |
<!-- # end new_employee.inc file -->
The $main::Response->Include() would then delegate the rendering
of the new-employee to the new_employee.inc ASP script include.
Though XML purists would not like this custom tag technology
to be related to XML, the reality is that a careful
site engineer could render full XML documents with this
technology, applying all the correct styles that one might
otherwise do with XSLT.
Custom tags defined in this way can be used as XML tags
are defined with both a body and without as it
<my:new-employee>...</my:new-employee>
and just
<my:new-employee />
These tags are very powerful in that they can also
enclose normal ASP logic, like:
<my:new-employee>
<!-- normal ASP logic -->
<% my $birthday = &HTTP::Date::time2str(time - 25 * 86400 * 365); %>
<!-- ASP inserts -->
This employee has been online for <%= int(rand()*600)+1 %>
seconds, and was born near <%= $birthday %>.
</my:new-employee>
For an example of this custom XML tagging in action, please check
out the ./site/eg/xml_subs.asp script.
XSLT Tranformations
XML is good stuff, but what can you use it for? The principle is
that by having data and style separated in XML and XSL files, you
can reformat your data more easily in the future, and you
can render your data in multiple formats, just as easily
as for your web site, so you might render your site to
a PDA, or a cell phone just as easily as to a browser, and all
you have to do is set up the right XSL stylesheets to do the
transformation (XSLT).
With native XML/XSLT support, Apache::ASP scripts may be the
source of XML data that the XSL file transforms, and the XSL file
itself will be first executed as an ASP script also. The XSLT
transformation is handled by XML::XSLT or XML::Sablotron and you can
see an example of it in action at the ./site/eg/xslt.xml XML script.
To specify a XSL stylesheet, use the setting:
PerlSetVar XSLT template.xsl
where template.xsl could be any file. By default this will
XSLT transform all ASP scripts so configured, but you can separate xml
scripts from the rest with the setting:
PerlSetVar XSLTMatch xml$
where all files with the ending xml would undergo a XSLT transformation.
Note that XSLT depends on the installation of XML::XSLT,
which in turn depends on XML::DOM, and XML::Parser.
As of version 2.11, XML::Sablotron may also be used by
setting:
PerlSetVar XSLTParser XML::Sablotron
and XML::LibXSLT may be used, as of 2.29, by setting
PerlSetVar XSLTParser XML::LibXSLT
If you would like to install XML::Sablotron or XML::LibXSLT,
you will first have to install the libraries that these perl
modules use, which you can get at:
libxslt - The XSLT C Library for Gnome
Sablotron - Ginger Alliance
For more on XML::XSLT, the default XSLT engine that Apache::ASP
will use, please see:
XML::XSLT
XML:XSLT was the first supported XSLT engine as has the benefit
of being written in pure perl so that though while it is slower
than the other solutions, it is easier to port.
If you would like to cache XSLT tranformations, which
is highly recommended, just set:
PerlSetVar XSLTCache 1
Please see the Cache settings in the CONFIG section for
more about how to configure the XSLTCache.
References
For more information about XSLT, please see the standard at:
http://www.w3.org/TR/xslt
For their huge ground breaking XML efforts, these other XML OSS
projects need mention:
Cocoon - XML-based web publishing, in Java
AxKit - XML web publishing with Apache & mod_perl