An Introduction to XML and Web Technologies Transforming XML Documents with XSLT
Anders Møller & Michael I. Schwartzbach © 2006 Addison-Wesley Objectives
How XML documents may be rendered in browsers How the XSLT language transforms XML dtdocuments How XPath is used in XSLT
An Introduction to XML and Web Technologies 2 Presenting a Business Card
An Introduction to XML and Web Technologies 3 Using CSS
card { background-color: #cccccc; border: none; width: 300;} name { display: block; font-size: 20pt; margin-left: 0; } title { display: block; margin-left: 20pt;} email { disppylay: block; font-familyyp: monospace; marg in-left: 20pt;} phone { display: block; margin-left: 20pt;}
the information cannot be rearranged information encoded in attributes cannot be exploited additional structure cannot be introduced
An Introduction to XML and Web Technologies 4 Using XSLT
xsl "?>
An Introduction to XML and Web Technologies 5 XSLT for Business Cards (1/2)
An Introduction to XML and Web Technologies 6 XSLT for Business Cards (2/2)
An Introduction to XML and Web Technologies 7 XSLXSL--FOFO
XSLT was originally design to target XSL-FO XSL-FO (Formatting Objects) in an XML language for describing physical layout of texts Widely used in the graphics industry Not supported by any browsers yet
An Introduction to XML and Web Technologies 8 XSLXSL--FOFO for Business Cards
An Introduction to XML and Web Technologies 9 Overview
IdiIntroduction Templates and pattern matching Sequence constructors Using XSLT
An Introduction to XML and Web Technologies 10 XSLT Stylesheets
XSLT is a domain-specific language for writing XML transformations (compare with e.g. JDOM)
An XSLT stylesheet contains template rules Processing starts at the root node of the input document
An Introduction to XML and Web Technologies 11 Template Rules
Find the template rules that match the context node Select the most specific one Evaluate the body (a sequence constructor)
An Introduction to XML and Web Technologies 12 Use of XPath in XSLT
Specifying patterns for template rules Selecting nodes for processing Computing boolean conditions Generating text contents for the output document
An Introduction to XML and Web Technologies 13 Evaluation Context
A context item (a node in the source tree or an atomic value) A context position and size A set of variable bindings (mapping variable names to values) A function library (including those from XPath) AtfA set of namespace dldeclara tions
An Introduction to XML and Web Technologies 14 The Initial Context
The context item is the document root The context position and size both have value 1 The set of variable bindings contains only global parameters The function library is the default one The namespace declarations are those defined in the root el ement of th e st yl esh eet
An Introduction to XML and Web Technologies 15 Patterns and Matching
A pattern is a restricted XPath expression • it is a union of path expressions • each path expression contains a number of steps separated by / or // • each step may only use the child or attribute axis A pattern matches a node if • starting from some node in the tree: • the given node is contained in the resulting sequence
rcp:recipe/rcp:ingredient//rcp:preparation
An Introduction to XML and Web Technologies 16 Names, Modes, Priorities
Templates may have other attributes
name: used to call templates like function mode: used to restrict the candidate templates priority: used to determine specificity
An Introduction to XML and Web Technologies 17 Overview
IdiIntroduction Templates and pattern matching Sequence constructors Using XSLT
An Introduction to XML and Web Technologies 18 Sequence Constructors
Element and attribute constructors Text constructors Copying nodes Recursive application Repetitions Conditionals Template invocation …
An Introduction to XML and Web Technologies 19 Literal Constructors
An Introduction to XML and Web Technologies 20 Explicit Constructors
An Introduction to XML and Web Technologies 21 Computed Attributes Values (1/2)
An Introduction to XML and Web Technologies 22 Computed Attribute Values (2/2)
An Introduction to XML and Web Technologies 23 Text Constructors
Literal text becomes character data in the output here is some chardata
Whitespace control requires a constructor:
The (atomized) value of an XPath expression:
An Introduction to XML and Web Technologies 24 Recursive Application
The apply-templates element • finds some nodes using the select attribute • applies the entire stylesheet to those nodes • concatenates the resulting sequences The default select value is child::node()
Processing is often (but not necessarily!) a simple recursive traversal down the input XML tree
An Introduction to XML and Web Technologies 25 Student Data
An Introduction to XML and Web Technologies 27 Using Modes, Desired Output
An Introduction to XML and Web Technologies 28 Using Modes (1/2)
An Introduction to XML and Web Technologies 29 Using Modes (2/2)
An Introduction to XML and Web Technologies 30 Repetitions
An Introduction to XML and Web Technologies 31 Conditionals (if)
An Introduction to XML and Web Technologies 32 Conditionals (choose)
An Introduction to XML and Web Technologies 33 Template Invocation (1/2)
An Introduction to XML and Web Technologies 35 BuiltBuilt--InIn Template Rules
What happens if no template matches a node? XSLT applies a default template rule • text is copied to the output • nodes appl y the st ylesheet rec ursi v ely to the children
A widely used default rule: for the document root node
An Introduction to XML and Web Technologies 36 Sorting
An Introduction to XML and Web Technologies 37 Copying Nodes
The copy-of element creates deep copies The copy element creates shallow copies
Give top-most HTML lists square bullets:
An Introduction to XML and Web Technologies 38 An Identity Transformation
An Introduction to XML and Web Technologies 39 Overview
IdiIntroduction Templates and pattern matching Sequence constructors Using XSLT
An Introduction to XML and Web Technologies 40 XSLT 1.0 Restrictions
Most browsers only support XSLT 1.0 Can only use XPath 1. 0 No sequence values, only result tree fragments …
An Introduction to XML and Web Technologies 41 XSLT for Recipes (1/6)
An Introduction to XML and Web Technologies 42 XSLT for Recipes (2/6)
An Introduction to XML and Web Technologies 43 XSLT for Recipes (3/6)
An Introduction to XML and Web Technologies 44 XSLT for Recipes (4/6)
An Introduction to XML and Web Technologies 45 XSLT for Recipes (5/6)
An Introduction to XML and Web Technologies 46 XSLT for Recipes (6/6)
An Introduction to XML and Web Technologies 47 The Output Calories Fat Carbohydrates Protein Alcohol
An Introduction to XML and Web Technologies 48 A Different View
An Introduction to XML and Web Technologies 49 The Output
An Introduction to XML and Web Technologies 50 A Further Stylesheet
Dish Calories Fat Carbohydrates Protein
An Introduction to XML and Web Technologies 51 The Final Output
An Introduction to XML and Web Technologies 52 Other Language Features
Variables and parameters Numbering Functions Sequence types Multiple input/output documents Dividing a stylesheet into several files Stylesheets that generate stylesheets as output
– see the book!
An Introduction to XML and Web Technologies 53 Essential Online Resources
http://www.w3.org/TR/xslt20/ http://saxon.sourceforge .net/ http://www.w3.org/TR/xsl/ http://xml.apache.org/fop/
An Introduction to XML and Web Technologies 54 Variables and Parameters
An Introduction to XML and Web Technologies 55 Grouping
An Introduction to XML and Web Technologies 56 The Output
An Introduction to XML and Web Technologies 57 Numbering
An Introduction to XML and Web Technologies 59 Multiple Input Documents
Jack Doe
An Introduction to XML and Web Technologies 63 The Second Output
- Math 101: C-
- Bi ol ogy 101 : C+
- Statistics 101: D
- Math 101: A
- XML 101: A-
- Physics 101: B+
- XML 102: A