XML Transformation Tags — Functional Description

<x:transform>
<x:param>

<x:transformer>


1. <transform>

The <transform> tag applies a transformation to an XML document (attribute "source"), given a specific XSLT stylesheet (attribute "xslt"). If the source attribute is not specified, the input XML document is read from the tag's body content.

In the example below, the XML document to be transformed is retrieved from an http request, while the XSLT stylesheet for the transformation is a webapp resource. The result of the transformation is written to the page.

  <c:import url="http://acme.com/customers?country=usa" var="xml"/>
  <c:import url="/WEB-INF/xslt/customerList.xsl" var="xslt"/>
  <x:transform source="$xml" xslt="$xslt"/>

The source and xslt attributes are objects of any of the following types: java.lang.String, java.io.Reader, or javax.xml.transform.Source.

As seen in the example above, the result of the transformation is written to the page by default. It is also possible to capture the result of the transformation in two other ways:

"var" and "result" are mutually exclusive. What's interesting with the "var" attribute is that the exported document can be used directly with the other xml tags. For example:

<x:transform source="..." xslt=".." var="doc/>
<x:expr select="$doc/..."/>

The <transform> action also supports attribute "transformer" described in section 3 (<transformer>) below.

2. <param>

The <param> subtag can be used along with <transform> to set transformation parameters. Attributes 'name' and 'value' are used to specify the parameter. The value attribute is optional. If it is not specified the value is retrieved from the tag's body.

For example:

  <x:transform source="$xml" xslt="$xslt">
<x:param name="foo" value="foo-value"/>
</x:transform>

3. <transformer>

It is sometimes the case that the same stylesheet transformation needs to be applied multiple times to different source XML documents. A more efficient approach is to process the transformation stylesheet once, and then save this "transformer" object for successive transformations.

As shown in the example below, the <transformer> tag can be used in collaboration with the transformer attribute of <transform> to efficiently reuse a transformation stylesheet.

  <c:import url="/xslt/style.xsl" var="xslt"/>
<x:transformer xslt="$xslt" var="transformer"/>
<c:forEach items="$xmlDocs" var="xml">
...
<x:transform source="$xml" transformer="$transformer" />
...
</c:forEach>
The attribute xslt is optional and can be any of the following types: java.lang.String, java.io.Reader, or javax.xml.transform.Source. If not specified, the stylesheet is read from the tag's body content.

4. Summary

XML Transformation Tags
Element Sample usage

<transform>
source xslt transformer result var

Applies a transformation to an XML document given a specific XSLT stylesheet or a transformer object.

<c:import url="/xml/employees.xml" var="xml"/>
<c:import url="/xslt/customerList.xsl" var="xslt"/>
<x:xslt source="$xml" xslt="$xslt"/>

<param>
name value

Sub-element to specify request parameters for the enclosing <transform>.


<x:transform source="$xml" xslt="$xslt">
<x:param name="foo" value="foo-value"/>
</x:transform>

 

<transformer>
var xslt

Creates a "transformer" object for more efficient transformations that use the same XSLT stylesheet.

<c:import url="/xslt/style.xsl" var="xslt"/>
<x:transformer xslt="$xslt" var="transformer"/>
...
<x:transform source="$xml" transformer="$transformer" /> ... <x:transform source="$xml2" transformer="$transformer" />