XPD: XML Pipeline Document Generation for Websites By Joshua Tauberer As with most of my programming pet projects, I was frustrated by something and decided to program-away the problem. In the case of XPD, the problem was constructing websites efficiently, with separation of concerns and no repeated markup. For a better explanation of my thinking, see this article I wrote. XPD works under Mono's mod_mono to provide a dynamic mechanism for transforming XML source documents into HTML output. When a web page is requested, XPD runs the source page through a pipeline of transformations (usually XSLT), and sends the final output to the browsing user. I developed XPD to power GovTrack.us, but now it also powers my personal website (including this page). How is XPD useful? Rather than repeating the layout and formatting of my website in each page, I can separate the master page layout from each page's content. And I can separate the programming from the rest through extension functions called by XSL transformations. Setting up XPDXPD runs under Mono's mod_mono (it should work with ASP.NET too, but I haven't tried). Mod_mono is an Apache module for serving ASP.NET pages through Mono (primarily useful in Linux). You'll need that installed. To instruct Apache to invoke mod_mono for pages ending in .xpd, you need to add these directives to Apache's httpd.conf and put a webapp file at the place specified in httpd.conf. At the root of your www directory, place this web.config file, which instructs mod_mono to invoke XPD for .xpd files. Create a bin directory, and put xpd.dll in it. (The download is below.) Then place your master page layout and index page in your www directory. If you try these files, you'll need to update the xml-stylesheet directive in index.xpd: it should read "master.xsl" not "../../master.xsl" if the two files are in the same location. You should then be good to go. Be sure to chop off the .txt extensions in these links. They're there just to make sure you see the files as raw text. More PowerXPD is a lot more poweful than what you can see in these example files. For one, XSLT templates can call into other .NET libraries through extension functions. To do this, place your extension function library in your bin directory. Extension functions must be methods of public classes in the library. Then, add an XML namespace declaration in your stylesheet as follows: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version=1.0" xmlns:my-functions="assembly://MyAssembly/MyNamespace.MyClassName"> Your assembly should be named MyAssembly.dll and be placed in the bin directory. Your extension function class should look something like:
namespace MyNamespace {
public class MyClassName {
public string MyFunction(string x) {
return "The string was " + x.Length + " characters long.";
}
}
}
And then within your stylesheet you can call your function like this: <xsl:value-of select="my-functions:MyFunction(data/@value)"/> Extension function arguments and return types can be strings, numbers, XPathNavigators, and XPathNodeIterators. Return values can also be ArrayLists or Hashtables, which are converted to XPathNavigators. There is even more you can do with XSLT templates. See XSLT template functionality and output control. DownloadLast Updated: Jan. 22, 2005 xpd.dll: Place this in your bin directory. xpd.tgz: Source code. Just compile all of the .cs files together. |