.NET Version

The .NET version of the photo album is slightly different. We’ll implement the extension as an extension function, not as an extension element. We’ll do this to show an alternate technique for creating nodes in the output tree.

Our extension function creates a DOM tree that contains all of the HTML elements needed for the output tree. The Xalan and Saxon versions of this extension element replace the extension element in the stylesheet with the appropriate generated nodes. In the .NET environment, our extension function returns a DOM tree that is added to the result tree.

Here’s the code for the extension function. It follows the same basic procedure as the other versions of the extension: use system functions to get the list of files and then create the DOM elements for the HTML table:

/* 
 * PhotoAlbum.cs
 * Created on Nov 28, 2006 by Doug Tidwell
 */

using System;
using System.IO;
using System.Text;
using System.Xml;

// This class creates a photo album with all of the images in a 
// given directory.
namespace com.oreilly.xslt
{
  class PhotoAlbum
  {
    // getPhotoListing is the name of the extension function.  Notice 
    // that it returns an XMLNode; that node is inserted into the 
    // output tree. 
    public XmlNode getPhotoListing(String dirName,
                                   String imgPerRow,
                                   String incFilenames)
    {
      // For parameters, we get the name of the directory, the number 
      // of images per row, and whether the names of the files should 
      // be displayed. int imagesPerRow = int.Parse(imgPerRow); ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.