Transcript ppt
Overview of XSL
1
Outline
• We will use Roger Costello’s tutorial
• The purpose of this presentation is
To give a quick overview of XSL
To describe what is available in Costello’s
tutorial
2
Introduction and Motivation
3
Web Services
• A Web service is written using servlets,
JSP or a similar technology
• The Web service interacts with
databases using JDBC
• The Web service interacts with clients
using a Web server
• The Web service may interact with other
applications
4
The Traditional Approach
• The Web service generates HTML pages in
order to interact with clients
• Changing the HTML pages necessitates a
change in the Java code
• Different clients (e.g., pc, palm-pilot, cell
phone) may require different HTML pages
Different versions of the Java code should be
maintained
• HTML is not suitable for information
exchange, e.g., interacting with automated
agents (knowbots)
5
The XML Approach:
Separating Content from Style
• The Web service generates XML
documents
• Automated agents get XML documents
in response to requests
• Other clients get HTML pages that are
generated by adding style to the XML
documents
It is easy to implement different styles for
different clients
6
Where Should the Business
Rules be Implemented?
• An example of a business rule
All products are now on sale at a discount
of 10%
• Business rules may change frequently
• Where should they be implemented?
Database application
Servlet or JSP
XML transformation (by means of XSLT)
7
XML Transformations
• CSS can add style to XML, but it cannot
transform an XML document
How can you change the price of a product
in an XML document describing that
product?
How can you add (to the XML document)
the information that the product is on sale?
• XSLT can do just that
8
Even Just Adding Style May
Require a Transformation
• Consider an XML document that
describes several products and their
prices
• How can you create a table with two
columns – one for products and and one
for prices?
• Cannot be done in CSS, but can be
done in XSL
9
Different Types of
Transformations
• Transform an XML document into
An HTML page
Another XML document
A text file
• Roger Costello’s tutorial covers the
three types (and more)
10
How to Transform?
• Write an XSL style sheet
• Use an XSL processor to transform the
XML document according the XSL style
sheet, or
• Add a Stylesheet PI (Processing
Instruction) to the XML document and
display the XML document using a
browser (recent versions only, e.g., IE 6
or Netscape 7)
11
Roger Costello’s Tutorial
• It describes the XSL language
• It gives many examples
There are actual files of the examples
Each example is in a separate folder and it
includes
• An XML document
• An XSL style sheet
• The result of the transformation as produced by
an XSL processor
12
A Few Things About XSL
• XSL is a high-level, functional language
• The syntax is a bit peculiar and possibly
confusing
• An XSL style sheet is a valid XML
document
Valid with respect to the XSL namespace
• Therefore, commands in XSL are XSL
elements
13
How to Use the Examples
• Since XSL is a high-level language,
XSL style sheets are relatively short and
easy to understand
• It is easy to modify an existing style
sheet into another style sheet with a
similar functionality
The many examples of Costello are very
useful as a starting point for writing your
own style sheets
14
The Main Elements of XSL
• <xsl:value-of select=“xpath-expression“/>
This element is for extracting values from the given
XML document
• <xsl:for-each select=“xpath-expression“/>
This element is for looping
• <xsl:if test=“xpath-expression/>,
<xsl:if test=“xpath-expression=value“/>, etc.
This element is for conditional processing
15
Examples of
Xpath Expressions
• /university/department/course
This Xpath expression matches any path that
starts at the root, which is a university element,
passes through a department element and ends
in a course element
• ./department/course[@year=2002]
This Xpath expression matches any path that
starts at the current element, continues to a child
which is a department element and ends at a
course element with a year attribute that is equal
to 2002
16
How to Write a Style Sheet for
Transforming XML to HTML
• Write a template in XHTML
• Add some necessary elements and PI
(Processing Instructions)
• Inside the XHTML template, use XSL
elements in order to extract values from
the given XML document
17