XML technologies / XSLT / XSLT Kochbuch / Extending and embedding XSLT / Java extension element

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" xmlns:MyExt="xalan://com.AcmeX.MyExtensionElement" extension-element-prefixes="MyExt">
  <xalan:component prefix="MyExt" elements="superExtension">
    <xalan:script lang="javasclass" src=" xalan:// com.AcmeX.MyExtensionElement"/>
  </xalan:component>
  <xsl:template match="*">
    <myExt:superExtension attr1="val1" attr2="val2">
      <!-- ... -->
    <myExt:superExtension>
  </xsl:template>
</xsl:stylesheet>

The implementation must be a Java class with a method having the following signature:

public class com.AcmeX.MyExtensionElement
{

public SomeType superExtension(
          org.apache.xalan.extensions.XSLProcessorContext ctx,
          org.apache.xalan.templates.ElemExtensionCall extensionElement)
 {
     //...
 }

}

Here, SomeType specifies the return type, ctx is an instance of the processing context, and extensionElement is the node corresponding to the stylesheet’s extension element. You may also use the superclasses of the specified types in the method signature. The base class com.AcmeX.MyExtensionElement can be anything you like, including none, as shown here.

Whatever the function returns is inserted into the result tree, an effect you can prevent with void. The recipe Adding extension elements with Java provides further details on the classes XSLProcessorContext and ElemExtensionCall.

  

 

 

 

back to top