Java Extension Element

<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 via a Java class and method with the following signature:

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

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

Whatever the function returns is put into the result tree, so use void if you do not want this effect. See Recipe 12.15 for further details on the XSLProcessorContext and ElemExtensionCall classes.

Get XSLT Cookbook 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.