Xalan Java Version

We’ll start with the Xalan Java version of the extension. The return type of the extension element is org.w3c.dom.Element. To start with, we’ll look at the XSLT stylesheet that controls everything:

<?xml version="1.0"?>
<!-- xalan-photo-album.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xpa="xalan://com.oreilly.xslt.xalan.XalanPhotoAlbumExtension"
  extension-element-prefixes="xpa">
  
  <xsl:output method="html"/>

  <xsl:template match="photo-album">
    <html>
      <head>
        <title>Photo album extension element test</title>
      </head>
      <body style="font-family: sans-serif;">
        <xsl:choose>
          <xsl:when test="element-available('xpa:XalanPhotoAlbum')">
            <h1>Xalan photo album extension element test</h1>
            <xpa:XalanPhotoAlbum/>
          </xsl:when>
          <xsl:otherwise>
            <p>
              <i>[Sorry, the photo album function is not available.]</i>
            </p>
          </xsl:otherwise>
        </xsl:choose>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Here’s the code for the extension element:

/*
 * XalanPhotoAlbumExtension.java
 * Created on Nov 28, 2006 by Doug Tidwell
 */

package com.oreilly.xslt.xalan;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xalan.extensions.XSLProcessorContext;
import org.apache.xalan.templates.ElemExtensionCall;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

//This class creates a photo album with all of the images in a ...

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.