XSLT: crash course or Programming Language Design Principle

XSLT: crash course
or
Programming Language Design Principle
http://www-sato.cc.u-tokyo.ac.jp/schuko/
XSLT-intro.ppt
10, Jun, 2004
10/06/04
1
Why XSLT ?
• Very Simple to Parse (just XML),
a little Harder than Scheme
• A Typical Programming Language on XML
Informal Semantics, but well Described.
• XSLT Processors at Hand!
No Need of Installation. (MSXML)
10/06/04
2
10/06/04
3
References
• XSLT Programmer’s Reference
Michael Kay, WROX Press, 2001
• XSLT
Doug Tidwell, O’Reilly, 2001
• XSLT Cookbook
Sal Mangano, O’Reilly, 2002
10/06/04
4
What is XSLT?
• eXtensible Stylesheet Language
Transformation
A transformation expressed in XSLT
describes rules for transforming a source
tree into a result tree.
Show by Example:
10/06/04
5
10/06/04
6
10/06/04
7
10/06/04
8
Why XSLT Designed?
• Requirements of XML
o Separating Data from Presentation
o Transmitting Data between
Applications.
•  XML Fixed, and
Transformer Designed  XSLT
(to HTML, PDF, …)
10/06/04
9
Systems for XML Handling
• XSLT
• XQuery
 XML as Semi-Structured DataBase
Rigid Formal Semantics is Given with
Type System.
• XPath
 Navigation in XML
10/06/04
10
How XSLT Works
• A transformation expressed in XSLT
describes rules for transforming a source
tree into a result tree.
XML
10/06/04
XML
11
How XSLT Works? (2)
• The transformation is achieved by
associating patterns with templates.
Patterns in Templates:
10/06/04
12
Template
Match
10/06/04
13
How XSLT Works (3)
• A pattern is matched against elements in the
source tree.
• A template is instantiated to create part of
the result tree.
10/06/04
14
10/06/04
15
10/06/04
16
In Summary,
• A transformation expressed in XSLT is
called a stylesheet. This is because, in the
case when XSLT is transforming into the
XSL formatting vocabulary, the
transformation functions as a stylesheet.
10/06/04
17
StyleSheet – A Key Idea
• A stylesheet contains a set of
template rules.
A template rule has two parts:
(1) a pattern which is matched against nodes
in the source tree and
(2) a template which can be instantiated to
form part of the result tree.
10/06/04
18
Execution Model
• The result tree is constructed by
(1) finding the template rule for the
root node and
(2) instantiating its template.
10/06/04
19
Instantiating a Template
• When a template is instantiated, each
instruction is executed and replaced by the
result tree fragment that it creates.
contents of template =
Instructions (explained later)
Literal Result Element(Data)
10/06/04
20
10/06/04
21
Instantiating a Template (2)
• Instructions can select and process
descendant source elements.
• Processing a descendant element creates a
result tree fragment by finding the
applicable template rule and instantiating its
template.
10/06/04
22
Programming Style (Digression)
• Push Processing
-- Just call <xsl:apply-templates/>
Execution
(1) Select All Children,
(2) Push Them to the Stylesheet, and
(3) Let the Stylesheet Select Appropriate
Templates
10/06/04
23
10/06/04
24
Programming Style (2)
• Pull Processing
-- Select a Class of Specific Nodes as
<xsl:apply-templates select=“…”/>
Execution
(1) Select Specific Nodes,
(2) Pull a Specific Template for their
Processing
10/06/04
25
10/06/04
26
Key Ideas in Execution
• Execution by Template Instantiation
• Elements are only processed when they
have been selected by the execution of an
instruction.
 Selection Schema is Critical
10/06/04
27
Templates, Expressions, Datatypes
• So much for Templates
 Templates = Functions in Traditional
Sense.
• Remaining Part of Programming Languages
 Expressions and Datatypes
10/06/04
28
Variable and Binding
• Form:
<xsl:variable name=… select=…/>
• The Same as Traditional Variables
name = (value of select)
10/06/04
29
Example of XSL:VARIABLE
<xsl:variable name=
"para-font-size">12pt</xsl:variable>
<xsl:template match="para">
<fo:block font-size=
"{$para-font-size}">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
10/06/04
30
• <xsl:param …/>
Another Mechanism for Binding a Value to
a Variable.
-- Parameters of Template
10/06/04
31
Example of Parameters
<xsl:template name="numbered-block">
<xsl:param name="format">1.</xsl:param>
<fo:block>
<xsl:number format="{$format}"/>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="ol//ol/li">
<xsl:call-template name="numbered-block">
<xsl:with-param name="format">a.
</xsl:with-param>
</xsl:call-template>
</xsl:template>
10/06/04
32
Scope of Variables
• Ordinary Rules for Scope.
Does not Exceed the Extent of Template.
From the Binding Point to the Point that
Another Value is bound to the same name
Variable.
10/06/04
33
10/06/04
34
To See the Result…
• Call factorial with the XML file:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="fac.xsl"?>
<root>
100
</root>
10/06/04
35
What is VALUE?
• Factorial isn’t Everything.
• Expression and Datatype to Represent XML
Tree Structure.
Boolean, Number, String (Ordinary)
+
NodeSet
10/06/04
36
XPath
• Defines Expressions and their Expressive
Power.
• Ordinary Boolean, Number, String
• Variable Reference, Function Call
• Path Expressions Specifying Node-Sets.
10/06/04
37
10/06/04
38
Node-Set in XPATH
• Selecting a Subset of a Tree.
• Traversing a Tree, and Specifying
Conditions of the form STEP:
Axis:: NodeTest [Predicates]
10/06/04
39
Ancestor
Preceding
Self
Following
Descendant
10/06/04
40
10/06/04
41
XPATH Syntax (2)
• Abbreviations:
(frequently used axis)
// -- /decendant-or-self::
@ -- /attribute::
10/06/04
42
XPATH Syntax
• Basic Form
[/]Step/Step/Step/…/Step
Step ≡ Axis::Node-Set ([ Predicate ]?)
Axis ≡ self | child | descendant | parent
| ancestor | sibling |
following- sibling | following |
preceding-sibling | preceding |
attribute | …
10/06/04
43
Examples of LocationPATH
//figure
@title
book/author/first-name
para[3]
10/06/04
44
Control Structure of XSLT
• Conditional
<xsl:if test=…>
<xsl:choose>
<xsl:when test=…>
<xsl:otherwise>
• Jump
<xsl:apply-templates match=…>
<xsl:call-template name=…>
<xsl:for-each>
10/06/04
45
Control Structure of XSLT (2)
• Note XSLT has NO ITERATION.
• ITERATION must be written in
Recursion.
10/06/04
46
10/06/04
47
General Theory in Design
• Design of Programming Language
⇔ Model of Computing
o Machine (CPU and memory)
o Object Interaction
o Recursion Thoery
o λ-Calculus
o Term Rewriting
o…
10/06/04
48
Model of Computing
• Goal 1:
Representing Key Concepts of Computing
o functional language  function
o OO language  object
Objects for “Key Concepts” must be treated
as a FIRST CLASS OBJECT.
10/06/04
49
Model of Computing (2)
• Goal 2:
Representing Control
o Control Structure (Conditional)
o Subroutine Call
o Data Structure
They can Make Overlap.
Object, Type, Class
10/06/04
50
XSLT Execution Model
• Tree Manipulation
Node Selection by XPATH Expression
Basic Control Structure Given as
<xsl:if>
<xsl:choose> <xsl:when>
<xsl:apply-templates>
<xsl:call-template>
Tree Construction in XML way.
10/06/04
51