51
10/06/04 1 XSLT: crash course or Programming Language Design Principle http://www-sato.cc.u-tokyo.ac.jp/s chuko/ XSLT-intro.ppt 10, Jun, 2004

10/06/041 XSLT: crash course or Programming Language Design Principle XSLT-intro.ppt 10, Jun, 2004

Embed Size (px)

Citation preview

Page 1: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 2: 10/06/041 XSLT: crash course or Programming Language Design Principle  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)

Page 3: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 3

Page 4: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 4

References

• XSLT Programmer’s ReferenceMichael Kay, WROX Press, 2001

• XSLTDoug Tidwell, O’Reilly, 2001

• XSLT CookbookSal Mangano, O’Reilly, 2002

Page 5: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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:

Page 6: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 6

Page 7: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 7

Page 8: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 8

Page 9: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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, …)

Page 10: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 11: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 11

How XSLT Works

• A transformation expressed in XSLT describes rules for transforming a source tree into a result tree.

XMLXML

Page 12: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 12

How XSLT Works? (2)

• The transformation is achieved by associating patterns with templates.

Patterns in Templates:

Page 13: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 13

Template

Match

Page 14: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 15: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 15

Page 16: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 16

Page 17: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 18: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 19: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 20: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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)

Page 21: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 21

Page 22: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 23: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 24: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 24

Page 25: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 26: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 26

Page 27: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 28: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 28

Templates, Expressions, Datatypes

• So much for Templates Templates = Functions in Traditional

Sense.

• Remaining Part of Programming Languages Expressions and Datatypes

Page 29: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 29

Variable and Binding

• Form:<xsl:variable name=… select=…/>

• The Same as Traditional Variables

name = (value of select)

Page 30: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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>

Page 31: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 31

• <xsl:param …/>Another Mechanism for Binding a Value to a Variable.

-- Parameters of Template

Page 32: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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>

Page 33: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 34: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 34

Page 35: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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>

Page 36: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 36

What is VALUE?

• Factorial isn’t Everything.

• Expression and Datatype to Represent XML Tree Structure.

Boolean, Number, String (Ordinary)+

NodeSet

Page 37: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 37

XPath

• Defines Expressions and their Expressive Power.

• Ordinary Boolean, Number, String

• Variable Reference, Function Call

• Path Expressions Specifying Node-Sets.

Page 38: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 38

Page 39: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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]

Page 40: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 40

Self

DescendantFollowing

Preceding

Ancestor

Page 41: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 41

Page 42: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 42

XPATH Syntax (2)

• Abbreviations:(frequently used axis)

// -- /decendant-or-self::@ -- /attribute::

Page 43: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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 | …

Page 44: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 44

Examples of LocationPATH

//figure

@title

book/author/first-name

para[3]

Page 45: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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>

Page 46: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 46

Control Structure of XSLT (2)

• Note XSLT has NO ITERATION.

• ITERATION must be written in Recursion.

Page 47: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

10/06/04 47

Page 48: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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 …

Page 49: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.

Page 50: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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

Page 51: 10/06/041 XSLT: crash course or Programming Language Design Principle  XSLT-intro.ppt 10, Jun, 2004

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.