10/06/041 xslt: crash course or programming language design principle xslt-intro.ppt 10, jun, 2004
TRANSCRIPT
10/06/04 1
XSLT: crash courseor
Programming Language Design Principlehttp://www-sato.cc.u-tokyo.ac.jp/schuko/
XSLT-intro.ppt
10, Jun, 2004
10/06/04 2
Why XSLT ?
• Very Simple to Parse (just XML),a little Harder than Scheme
• A Typical Programming Language on XMLInformal Semantics, but well Described.
• XSLT Processors at Hand!No Need of Installation. (MSXML)
10/06/04 3
10/06/04 4
References
• XSLT Programmer’s ReferenceMichael Kay, WROX Press, 2001
• XSLTDoug Tidwell, O’Reilly, 2001
• XSLT CookbookSal Mangano, O’Reilly, 2002
10/06/04 5
What is XSLT?• eXtensible Stylesheet Language Transfo
rmation
A transformation expressed in XSLT describes rules for transforming a source tree into a result tree.
Show by Example:
10/06/04 6
10/06/04 7
10/06/04 8
10/06/04 9
Why XSLT Designed?
• Requirements of XMLo Separating Data from Presentationo Transmitting Data between Applications.
XML Fixed, andTransformer Designed XSLT(to HTML, PDF, …)
10/06/04 10
Systems for XML Handling
• XSLT
• XQuery XML as Semi-Structured DataBase
Rigid Formal Semantics is Given withType System.
• XPath Navigation in XML
10/06/04 11
How XSLT Works
• A transformation expressed in XSLT describes rules for transforming a source tree into a result tree.
XMLXML
10/06/04 12
How XSLT Works? (2)
• The transformation is achieved by associating patterns with templates.
Patterns in Templates:
10/06/04 13
Template
Match
10/06/04 14
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 15
10/06/04 16
10/06/04 17
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 18
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 19
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 20
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 21
10/06/04 22
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 23
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 24
10/06/04 25
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 26
10/06/04 27
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 28
Templates, Expressions, Datatypes
• So much for Templates Templates = Functions in Traditional
Sense.
• Remaining Part of Programming Languages Expressions and Datatypes
10/06/04 29
Variable and Binding
• Form:<xsl:variable name=… select=…/>
• The Same as Traditional Variables
name = (value of select)
10/06/04 30
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 31
• <xsl:param …/>Another Mechanism for Binding a Value to a Variable.
-- Parameters of Template
10/06/04 32
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 33
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 34
10/06/04 35
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 36
What is VALUE?
• Factorial isn’t Everything.
• Expression and Datatype to Represent XML Tree Structure.
Boolean, Number, String (Ordinary)+
NodeSet
10/06/04 37
XPath
• Defines Expressions and their Expressive Power.
• Ordinary Boolean, Number, String
• Variable Reference, Function Call
• Path Expressions Specifying Node-Sets.
10/06/04 38
10/06/04 39
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 40
Self
DescendantFollowing
Preceding
Ancestor
10/06/04 41
10/06/04 42
XPATH Syntax (2)
• Abbreviations:(frequently used axis)
// -- /decendant-or-self::@ -- /attribute::
10/06/04 43
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 44
Examples of LocationPATH
//figure
@title
book/author/first-name
para[3]
10/06/04 45
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 46
Control Structure of XSLT (2)
• Note XSLT has NO ITERATION.
• ITERATION must be written in Recursion.
10/06/04 47
10/06/04 48
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 49
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 50
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 51
XSLT Execution Model
• Tree Manipulation
Node Selection by XPATH ExpressionBasic Control Structure Given as
<xsl:if><xsl:choose> <xsl:when><xsl:apply-templates><xsl:call-template>
Tree Construction in XML way.