July 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Extending XSLT

  • February 1, 2001
  • By Piroz Mohseni
  • Send Email »
  • More Articles »

XSLT provides an effective and elegant solution to many typical transformation problems. Transformation of data is often thought of as something that a program must do. But as XSLT has demonstrated, that is not always true. An XSLT stylesheet looks like XML and so I have a hard time considering it a programming language like C or Java. Nevertheless, XSLT is capable of modifying an XML tree. It can act on input data and produce different outputs. There are cases, however, where limitations of XSLT is evident. Unlike a typical programming language, XSLT has limited number of data types. It also has a limited number of built-in functions. So as a design issue, one has to consider what types of transformations are natural for XSLT and what transformations require a traditional programming language. There is an alternative to his approach and that is to use XSLT extensions.

Extension elements and functions, allow you to mix XSLT with a programming language like Java or a scripting language like JavaScript. You now have a mixed model, which allows you to use both XSLT and the flexibility of a conventional programming language under one umbrella. The Xalan processor from Apache (http://xml.apache.org/xalan/index.html) provides a full implementation of such extensions.

Extensions come in two flavors. Extension elements are valid XML elements that perform specific actions. They are similar to how tag library elements work in JSP. The element looks like XML making it easy to use for XSLT developers, but its underlying implementation can be in Java or other languages. The Redirect extension is an example that ships with Xalan. It contains three extension elements: , , and . Using these extension elements you are able to redirect portions of your transformation output to different files. An extension element routine (the implementation) receives two objects. The first is org.apache.xalan.xslt.XSLProcessorContext, which includes access to the XSL processor, the current node, the source tree and the stylesheet tree. The second object is org.w3c.dom.Element, which provides information about the element itself such as any attributes it may have. Using extension elements, you can extend the capabilities of XSLT by using XML-like elements and hiding the actual implementation.

Another way of incorporating extensions into XSLT is through extension functions. XPath already supports a number of functions. Extension functions augment those by using a consistent syntax. Xalan uses the Bean Scripting Framework (BSF) and supports extensions in Java and JavaScript. Extensions will have their own namespace (lxslt). The component and script elements contain the implementation details. In case of Java, there is a src attribute that identifies the Java class. From the XSLT developer point of view, extension functions look like any other XPath function with the exception of a prefix.

If you have been skeptical about XSLT due to some of its limitations, you should seriously consider XSLT extensions. They provide a nice framework for using conventional programming tasks and incorporating business logic into transformations done by XSLT. Performance, most likely remain an issue and any time two disparate programming models are combined, careful attention should be give to design details to assure interoperability, reliability and ease of use.

Piroz Mohseni






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel