morphis logo morphis | wax   
morphis manual
manual
 

Processing
   Dispatching
   Requesting
   Translating

Installation
   Requirements
   Instructions
   Config Properties

Dispatching
   Configuration
   Pre/Post Filters

Requestors
   URL Requesting
   Servlet Chaining
   Run-time Instructs
   Custom Requestors

Translators
   Non-Translator
   Custom Translators

SAX Translators
   XSLT Translation
   Custom SAX Filters

SAX Translators: XSLT Translation using the XSLTAdapter Filter

XSLT translation is used to translate one flavor of XML (or, SAX-ified versions of HTML) into either XML, or HTML. Morphis accomplishes this by wrapping Apache's Xalan product with the XSLTAdapter SAX Filter. This functionality treats XSLT processing just like any other SAX filter. In fact, you can chain all of these filters together to come up with a custom processing pipeline. Since the pipeline is SAX based, processing is fast, and requires less overhead than a traditional DOM based implementation.

Many different scenarios are possible with this model. The following scenario performs XSLT processing on a document, then runs the result through two custom filters. The second filter is responsible for serializing the SAX events:

In-Stream -> Parser -> XSLT -> Filter1 -> Filter2 -> Out-Stream

The followgin scenario performs two back-to-back XSLT transformations. The first XSLT processor generates XML, and the second XSLT processor generates an XML or HTML output stream:

In-Stream -> Parser -> XSLT1 -> XSLT2 -> Out-Stream

Since the XSL stylesheet defines the rules for processing an XML document using XSLT, Morphis must be told which stylesheet to use, and where to find it. There are three different places this can happen:

  • Filter parameters in dispatch.xml: Processing may always be instructed inside dispatch.xml. The following snippet of dispatch.xml shows how this can be done. The filter parameter name "stylesheet" is being set to foo.xsl. Therefore, the document obtained by this context's requestor will be run through an XSLT transformation with the "foo.xsl" stylesheet.

    <context ... >
        ...
       
    <translator class="org.morphis.translator.SAXTranslator">
            <filter type="translate"
               class="org.morphis.translate.XSLTAdapter">
                <parameters>
                    <param name="stylesheet" value="foo.xsl"/>
                </parameters>
            </filter>


  • Special processing instructions in the xml document itself: The source document may include the <?xml-stylesheet href="..."?> processing instruction. The following example shows an XML document with a processing instruction telling Morphis to use the foo.xsl stylesheet. This method will always tell Morphis to perform an XSLT transformation as the last SAX filter. However, you may specify multiple <?xml-stylesheet href="..."?> instructions to have Morphis perform multiple transformations back-to-back.

    <?xml version="1.0"?>
    <?xml-stylesheet href="foo.xsl"?>
    <my><document/><here/></my>

  • Morphis processing instructions in the xml document: Run-time processing instructions may be inserted into the Requestor's header, or in the document itself. The following XML document shows an alternate way of specifiying XSLT transformation. Performing translation in this manner allows for greater control over multiple SAX filters, and in what order the filtering occurs, but still leaves the document in complete control.

    <?xml version="1.0"?>
    <?morphis-filter type="translate" class="org.morphis.translator.XSLTAdapter" name="stylesheet" value="foo.xsl"?>
    <my><document/><here/></my>

Explaining XSL, and providing help building stylesheets is beyond the scope of this document. Fortunately, there are many websites which specialize in explaining XSL:

Java Extensions to XSLT

One of the most powerful features of using Apache's Xalan to perform XSL translations is its Xalan-Java Extensions. Using these extensions you are able to embed scripting code directly into your XSL stylesheets. This functionality is similar to Java Server Pages "Tag Libraries", where your source HTML (or XML) document declares a tag (say, "<customer-table>") which is replaced by HTML (or XML) which is created by complex logic, database calls, Java beans, or any of the supported scripting languages.

This logic can be written in your XSL stylesheet without having to compile code. Alternatively, you can write extension elements in Java which will invoke class methods whenever a defined element appears in your source document. This is very similar to writing custom Morphis SAX filters.

To find out more information about writing custom extensions to XSL, see Apache's Xalan website at http://xml.apache.org/xalan/extensions.html.

Currently, the list of supported scripting languages includes:

  • Mozilla Rhino (Javascript)
  • NetRexx
  • BML
  • JPython
  • Win32 ActiveScript, JScript, VBScript
  • PerlScript

Explain: <xsl:output media-type="text/html"/>

 

morphis SourceForge Logo