Sunday, August 30, 2009

PL/XSLT

Let me introduce the procedural language that you didn't yet know you needed: PL/XSLT.

PL/XSLT isn't actually that new. I faintly recall that I wrote it around the time of the Anniversary Summit in 2006, but never published it. Today I found the source code again, cleaned it up a bit, and put it up on GitHub: http://github.com/petere/plxslt/

Here is roughly how this would look:
CREATE FUNCTION foo(xml) RETURNS xml AS $$
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xsl="http://www.w3.org/1999/XSL/Transform">
...
</xsl:stylesheet>
$$ LANGUAGE xslt;

There are some conditions on the function definition:

  • The first parameter must be of type xml. It will receive the input document. Every function must have at least this one parameter.

  • The following function parameters will be supplied to the stylesheet as XSL parameters.

  • The return type must match the output method specified by the stylesheet. If it is xml, then the return type must be xml; if it is text or html, the return type must be text or varchar.

  • Triggers are not supported.


In the source code, I include an example function that converts the output of the table_to_xml etc. functions to HTML.

Of course, the question will arise whether this is sensible. I guess it is about as sensible as considering XSLT a programming language. Judge for yourself. :-)

In the words of GitHub: fork the project and add to it.

4 comments:

  1. There seems to be a typo in the Makefile.am
    It reads Libxslt_CLFAGS which presumably should be Libxslt_CFLAGS.

    You might consider making more clear what xslt processor the implementation is based on. That's what I was looking for when I saw the typo. (I didn't try to install, so I cannot say if it fails)

    I might have overlooked the obvious though.

    ReplyDelete
  2. Peter - This is very nice. Thanks for sharing it. Readme and Install are quite well-written. Also, I didn't know it was this "easy" for users to "add" a language to a running PostgreSQL. Interesting.

    ReplyDelete