Title: Using Extensible Stylesheets XSLT
1Using Extensible Stylesheets (XSLT)
- Dan Gunter
- DIDC Group
- 11 June 2001
2Talk Outline
- Motivation
- XML background information
- High-level view of XSLT
- Example of use of XSLT for a real Web page
- Links to tools and more information
3Motivation
- We need to share information about our work, and
the Web is the medium of choice - But updating and maintaining HTML pages every
time the underlying data changes is tedious - Changing the datas format is also tedious
- We need to separate content from presentation so
that changing each is easier
4XML Overview
- XML uses nested elements that look like HTML,
i.e. ltelementgtdatalt/elementgt - But in XML, you, the document creator, can invent
your own elements - Sample XML fragment
- ltaddressgt
- ltnamegtDan Gunterlt/namegt
- ltstreetgt13 Foobar Avelt/streetgt
- ltcitygtSan Franciscolt/citygt
- ltstategtCAlt/stategt
- lt/addressgt
5Well-formed vs. Valid XML
- If start-tags match end-tags and the special
characters (like lt and gt) are used properly,
a document is well-formed. - If the document is well-formed and it also
conforms to its schema or document type
definition (DTD), it is called valid.
well-formed, not valid address
valid address
ltaddressgt ltnamegtJoelt/namegt
lthairgtbrownlt/hairgt lteyesgtgreenlt/eyesgt
ltheightgt70lt/heightgt lt/addressgt
ltaddressgt ltnamegtJoelt/namegt ltstreetgt1 Odd
Stlt/streetgt ltcitygtTrentonlt/citygt
ltstategtNJlt/stategt lt/addressgt
6XSLT
- Stands for Extensible Stylesheet Language
Transformation - It is a script language specifically designed for
transforming XML documents into other documents
(HTML, text, PDF, etc.) - The XSLT language is expressed in XML, and its
syntax is specified by an XML schema - Thus, each XSLT script is a valid XML document
7XSLT Hello, world!
- ltxslstylesheet version1.0
- xmlnsxslhttp//www.w3.org/1999/XSL/Transformgt
- ltxsloutput methodhtml/gt
- ltxsltemplate match/gt
- lthtmlgt
- ltbodygt
- Hello, world!
- lt/bodygt
- lt/htmlgt
- lt/xsltemplategt
- lt/xslstylesheetgt
8Transformation Pipeline
well-formed XML document
XSLT
XML
valid XSLT document
processors exist in many languages, and can be
used from the command-line or with an API
XSLT processor
the format of the output depends on the
stylesheet used
output
9Transformation Concepts
Think of the input XML document as a tree of
elements. XSLT can select elements and then
extract their values
person
name
age
gender
M
first
middle
last
30
Lazy
Programmer
Q.
into the output document.
10Example
- Example task
- Applying XSLT to the task
11Example task
- Data A list of documents for a Grid Forum
working group, each with an author, title,
document, version, date, status, notes, and an
associated PDF file named by the document - Output Sorted table(s) of the documents on the
groups website, with hyperlinks to the actual
files. Like this.
12Applying XSLT to the task
- Put the data into XML document(s).
documents.xml - Write stylesheet(s) documents.xsl
- Write a small script to automate the process of
feeding the document(s) to an XSLT processor
xslt.html - Run the script
13When not to use XSLT
- XSLT is probably not worth it for small, one-shot
jobs, i.e. when - Number of data items is small
- Formatting is unlikely to change very often
- Only one output format will be needed in the
foreseeable future
14When to use XSLT
- XSLT is great for frequently updated data
because - Easy to change formatting for whole document
(e.g. change order of TABLE columns) - Easy to create alternate views of the same data
- Data more readable (not buried in HTML)
- Easier to automate related tasks (e.g. inactive
document email script)
15Links and Tools
- Xalan, from the Xerces project at Apache
(http//xml.apache.org) - 4XSLT from 4Suite (http//4suite.org)
- Google search for xslt, click on
XSLImplementations category - XML Bible page on XSLT (http//www.ibiblio.org/xml
/books/bible/updates/14.html)