Information Encoding in Biological Molecules

Download Report

Transcript Information Encoding in Biological Molecules

5.2 Transforming Data:
Using Apache Xalan to apply XSLT
transformations
Marc Dumontier
Blueprint Initiative
Samuel Lunenfeld Research Institute
Toronto, ON
Lecture 5.2
1
Outline
• 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
Lecture 5.2
2
Apache 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
Lecture 5.2
3
Apache Xalan
•
•
•
•
•
Transformations may be chained.
Includes an applet wrapper.
May be used in a servlet
May be used from JavaServer Pages.
Can be extended.
Lecture 5.2
4
Outline
• 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
Lecture 5.2
5
Applying 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 <xsl:message> to console.
<xsl:message> is used for debugging. Similar to
System.out.println().
– Quick to execute
– Simple to use
Lecture 5.2
6
Applying transformations from the
command line
Lecture 5.2
7
Outline
• 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
Lecture 5.2
8
Using 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.
Lecture 5.2
9
Using 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.
Lecture 5.2
10
Using the Java API - Example
Lecture 5.2
11
Using the Java API - Example
Lecture 5.2
12
Outline
• 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
Lecture 5.2
13
Using 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.
Lecture 5.2
14
Introduction 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.
Lecture 5.2
15
JSTL & XML
Lecture 5.2
16
Core 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.
Lecture 5.2
17
Flow Control Tags
• Similar to XSLT.
• Allows for evaluating XPath expressions and
creating control statements.
• for-each allows for iterating over a node-set.
Lecture 5.2
18
Transformation Tags
• Perform XSLT transformations!
• Optionally pass parameters to the stylesheet
such as external information only known as
runtime.
Lecture 5.2
19
Using Xalan from JavaServer Pages
• There’s 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
application’s classpath (usually WEB-INF/lib).
Lecture 5.2
20
Using Xalan from JavaServer Pages
Lecture 5.2
21
Using Xalan from JavaServer Pages
Lecture 5.2
22
Parsing 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.
Lecture 5.2
23
Parsing an XML document
<%@ taglib uri="/tags/jstl-c" prefix="c" %>
<%@ taglib uri="/tags/jstl-x" prefix="x" %>
<html>
<head>
<title>Parse XML</title>
</head>
<body bgcolor="#FFFFFF">
<h3>Parse XML demo</h3>
<!-- import and parse XML document -->
<c:import varReader="xmlString" url="smallgame.xml">
<x:parse var="document" xml="${xmlString}"/>
</c:import>
</body>
</html>
Lecture 5.2
24
Using XPath in JSTL
<%@ taglib uri="/tags/jstl-c" prefix="c" %>
<%@ taglib uri="/tags/jstl-x" prefix="x" %>
<html>
<head>
<title>XPath Demo</title>
</head>
<body bgcolor="#FFFFFF">
<h3>XPath demo</h3>
<!-- import and parse XML document -->
<c:import varReader="xmlString" url="smallgame.xml">
<x:parse var="document" xml="${xmlString}"/>
</c:import>
<!-- execute XPath statement -->
<x:out select="$document/game/seq/name"/>
</body>
</html>
Lecture 5.2
25
Using XPath in JSTL
Lecture 5.2
26
Running 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
Lecture 5.2
27
Running a Transformation from JSTL
<%@ taglib uri="/tags/jstl-c" prefix="c" %>
<%@ taglib uri="/tags/jstl-x" prefix="x" %>
<!-- import stylesheet -->
<c:import var="stylesheet" url="xsl1.xsl"/>
<!-- import and parse XML document -->
<c:import varReader="xmlString" url="smallgame.xml">
<x:parse var="document" xml="${xmlString}"/>
</c:import>
<!-- transform XML document using stylesheet -->
<x:transform xslt="${stylesheet}" xml="${document}" />
Lecture 5.2
28
Running a Transformation from JSTL
Lecture 5.2
29
Expression 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’
Lecture 5.2
30
LAB 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
Lecture 5.2
31
The 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.
Lecture 5.2
32
Apollo
Lecture 5.2
33
The 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
Lecture 5.2
34
Exercises
1. Explore the project
2. Run MyTransformer with an XML file and a
XSLT Stylesheet.
3. Install the Web Application into tomcat.
4. Run all the JSPs and see how they work.
5. Write your own JSP with JSTL tags for
processing XML.
Lecture 5.2
35