- Understanding XML Core Tags
- Transforming XML with XSLT
- Using the <x:transform> Tag
- Using Parameters
- Conclusions
Using the <x:transform> Tag
The <x:transform> tag is transform XML data based on an XSLT script. There are two forms of the <x:transform> tag, shown as follows.
Syntax 1: XML document specified via a String or Reader object
<x:transform {xmlUrl="URLforXMLDocument"|xmlText="XMLDocument"} {xsltUrl="URLforXSLTStylesheet"|xsltText="XSLTStylesheet"} [{var="varName"|result="resultObject"}] [scope="{page|request|session|application}"] />
Syntax 2: XML document specified via the body content
<x:transform {xsltUrl="URLforXSLTStylesheet"|xsltText="XSLTStylesheet"} [{var="varName"|result="resultObject"}] [scope="{page|request|session|application}"]> XML Document to parse </x:parse>
XML must be provided to the <x:transform> tag. The two syntaxes give you different ways of providing the XML data. In the first syntax, the XML data is provided through the attributes of the <x:transform> tag. The second syntax specifies the input XML through the body data contained in the <x:transform> tag.
The xmlUrl attribute allows you to specify a URL that the XML data will come from. Likewise, the xsltUrl attribute allows you to specify a URL that the XSLT data will come from.
You can also specify that the XML and XSLT data will come from internal sources. The xmlText attribute allows you to specify a String that holds the XML data that is to be transformed. The xsltText attribute allows you to specify a String that holds the XSLT data that is to specify the transformation.
By default the results of the transformation will be written to the page. If a var attribute is specified, the contents of the transformation will be stored to that variable. (The type of this variable will be org.w3c.dom.Document.) The attribute scope allows you to set the scope for the variable specified by the var attribute.
You also can specify a result object using the result attribute, which determines how the transformation will take place. This object is of type result javax.xml.transform.Result. (For more information, refer to the Java API documentation for the javax.xml.transform.Result class.)
You should also note that if the XSLT stylesheet is specified via the xsltUrl attribute, the <x:transform> action may transparently cache transformer objects to improve performance. The <x:transform> tag is not required to fetch the contents of the xsltUrl attribute each time the <x:transform> tag is used.
You will now be shown an example of how the <x:transform> tag is used in a JSP page. This example is very short because nearly all the work is done in the XSLT file.
<c:import var="xml" url="students.xml" /> <c:import var="xslt" url="transform.xsl" /> <x:transform xml="${xml}" xslt="${xslt}" />
As you can see from the above example, both the XML and XSL files are loaded into scoped variables. The <x:transform> tag is then used to format the XML using the XSL. The end result is that the student list is written as a nicely formatted HTML page.
This brings up an important point. We have seen that there are two ways the XML file can be formatted. The end result is the same. On one hand, we can use the <x:forEach> tag and use JSTL to display the XML. On the other hand, we can use XSLT to transform the XML into the same HTML output. Which method should be used?
The answer is that it depends; there are no set rules. Often, it comes down to programmer preferences. However, there are a few things that you should keep in mind. Formatting with XSL does not allow you to interact with other JSTL components such as databases. If the XML to be displayed easily fits within a XSL template, then XSL is probably the solution of choice. Conversely, if the display of your XML requires complex cross-referencing or other complex processing, using the other JSTL tags might be a better solution.