Title: 5.2 Transforming Data: Using Apache Xalan to apply XSLT transformations
15.2 Transforming DataUsing Apache Xalan to
apply XSLT transformations
- Marc Dumontier
- Blueprint Initiative
- Samuel Lunenfeld Research Institute
- Toronto, ON
2Outline
- Introduction to Apache Xalan.
- Use the Xalan interactive processor from the
command line. - Use the Xalan Java API to apply an XSLT
transformation. - Use the JSP Standard Tag Library to apply
transformations
3Apache Xalan
- Fully compliant XSLT 1.0 processor
- Fully supports XPath 1.0
- Includes an Interpretive processor for debugging.
- Can be configured to work with any W3C compliant
XML parser. Optimized for Xerces. - Can process stream, SAX, or DOM input.
- Can output to a stream, SAX, or DOM
4Apache Xalan
- Transformations may be chained.
- Includes an applet wrapper.
- May be used in a servlet
- May be used from JavaServer Pages.
- Can be extended.
5Outline
- Introduction to Apache Xalan.
- Use the Xalan interactive processor from the
command line. - Use the Xalan Java API to apply an XSLT
transformation. - Use the JSP Standard Tag Library to apply
transformations
6Applying transformation from the command line
- Use the interpretive processor for simple command
line based transformations. - Advantages
- Easier to debug by isolating transformation from
a larger more complex system. - Can see output of ltxslmessagegt to console.
ltxslmessagegt is used for debugging. Similar to
System.out.println(). - Quick to execute
- Simple to use
7Applying transformations from the command line
8Outline
- Introduction to Apache Xalan.
- Use the Xalan interactive processor from the
command line. - Use the Xalan Java API to apply an XSLT
transformation. - Use the JSP Standard Tag Library to apply
transformations
9Using the Java API
- Prerequisites
- Must include necessary JAR files in your
classpath such as xalan.jar, and an XML parser
such as Apache Xerces. - An XML document to transform, either from a file,
W3C DOM structure, or string. - A valid XSLT Stylesheet which describes the rules
of transformation.
10Using the Java API - Example
- This program takes in an XML filename, and an
XSLT filename, and writes the result of the
transformation to standard output. - The first step is to get the argument values and
convert them to URIs (Uniform Resource
Identifiers). - The second step involves creating a
TransformerFactory object. This object employs
the factory pattern. - The third step is to create a Transformer object
for the XSLT stylesheet. - The fourth step is to transform the XML document.
11Using the Java API - Example
12Using the Java API - Example
13Outline
- Introduction to Apache Xalan.
- Use the Xalan interactive processor from the
command line. - Use the Xalan Java API to apply an XSLT
transformation. - Use the JSP Standard Tag Library to apply
transformations
14Using Xalan from JavaServer Pages
- The common pattern for doing XSLT transformation
in Java Server Pages (JSP) is to use the JSP
Standard Template Library. - It is also possible to import the Xalan classes
and embed Java code using the API. -
15Introduction to JSTL
- JavaServer Pages Standard Tag Library (JSTL)
encapsulates common JSP functionality into Tags. - JSTL has support for common, structural tasks
such as iteration and conditionals, tags for
manipulating XML documents, internationalization
tags, and tags for accessing databases using SQL.
It also introduces the concept of an expression
language to simplify page development.
16JSTL XML
17Core Tags
- Provides the basic functionality for accessing
and parsing XML data. - The parse tag parses an XML file and stores the
result in a type of variable. - The expr and set tags are for running XPath
expressions. set can store the results into a
scoped JSP attribute.
18Flow Control Tags
- Similar to XSLT.
- Allows for evaluating XPath expressions and
creating control statements. - for-each allows for iterating over a node-set.
19Transformation Tags
- Perform XSLT transformations!
- Optionally pass parameters to the stylesheet such
as external information only known as runtime.
20Using Xalan from JavaServer Pages
- Theres some setup required!
- There are multiple ways of doing this, use what
works. This technology is very new. - You must import the tag library definitions.
- The files must then be mapped in web.xml
- The supporting JAR files must be in your web
applications classpath (usually WEB-INF/lib).
21Using Xalan from JavaServer Pages
22Using Xalan from JavaServer Pages
23Parsing an XML document
- Must read in the XML as a string using the core
tag import. - Use the parse tag on this string variable to
parse into a DOM structure.
24Parsing an XML document
- lt_at_ taglib uri"/tags/jstl-c" prefix"c" gt
- lt_at_ taglib uri"/tags/jstl-x" prefix"x" gt
- lthtmlgt
- ltheadgt
- lttitlegtParse XMLlt/titlegt
- lt/headgt
- ltbody bgcolor"FFFFFF"gt
- lth3gtParse XML demolt/h3gt
- lt!-- import and parse XML document --gt
- ltcimport varReader"xmlString"
url"smallgame.xml"gt - ltxparse var"document" xml"xmlString"/gt
- lt/cimportgt
- lt/bodygt
- lt/htmlgt
25Using XPath in JSTL
- lt_at_ taglib uri"/tags/jstl-c" prefix"c" gt
- lt_at_ taglib uri"/tags/jstl-x" prefix"x" gt
- lthtmlgt
- ltheadgt
- lttitlegtXPath Demolt/titlegt
- lt/headgt
- ltbody bgcolor"FFFFFF"gt
- lth3gtXPath demolt/h3gt
- lt!-- import and parse XML document --gt
- ltcimport varReader"xmlString"
url"smallgame.xml"gt - ltxparse var"document" xml"xmlString"/gt
- lt/cimportgt
- lt!-- execute XPath statement --gt
- ltxout select"document/game/seq/name"/gt
26Using XPath in JSTL
27Running a Transformation from JSTL
- Similar to the previous example, reading the XML
file from disk is done with the import tag. - The transform tag is used to transform the XML
using the XSLT that must also be read in and
parsed
28Running a Transformation from JSTL
- lt_at_ taglib uri"/tags/jstl-c" prefix"c" gt
- lt_at_ taglib uri"/tags/jstl-x" prefix"x" gt
- lt!-- import stylesheet --gt
- ltcimport var"stylesheet" url"xsl1.xsl"/gt
- lt!-- import and parse XML document --gt
- ltcimport varReader"xmlString"
url"smallgame.xml"gt - ltxparse var"document" xml"xmlString"/gt
- lt/cimportgt
- lt!-- transform XML document using stylesheet --gt
- ltxtransform xslt"stylesheet"
xml"document" /gt
29Running a Transformation from JSTL
30Expression Language (EL)
- JSP 2.0 introduced Expression Language (EL).
- EL is used extensively in JSTL.
- EL expressions are found in the special holder
. - Attributes can be accessed through EL.
- Example pageContext.request.method could be
equal to GET or POST
31LAB Section
- The purpose of this lab is to get a working
knowledge of using xalan in the three methods
described. - Command Line
- Java API
- JSP/JSTL
- All Code and examples have been provided
32The Data
- The Data chosen for this lab is the XML output
from apollo the sequence annotation tool. - Apollo is a collaboration between the Drosophila
Genome Project, the Sanger Institute and the EBI. - GMOD has adopted Apollo for sequence annotation.
- The XML output generated is GameXML.
33Apollo
34The Source
- Use CVS to checkout the gamexml module. It
includes the following - A build.xml file for ant
- 2 GameXML files (game.xml, smallgame.xml)
- 2 XSLT stylesheets (game_transcript.xsl,
game_sequence.xsl) - game.dtd (no XML Schema available)
- MyTansformer
35Exercises
- Explore the project
- Run MyTransformer with an XML file and a XSLT
Stylesheet. - Install the Web Application into tomcat.
- Run all the JSPs and see how they work.
- Write your own JSP with JSTL tags for processing
XML.