46
1 XP XSLT II Robin Burke ECT 360

XSLT II

Embed Size (px)

DESCRIPTION

XSLT II. Robin Burke ECT 360. Outline. Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates. Conditional nodes. XSLT supports two kinds of conditional elements: If is only if-then (no else) - PowerPoint PPT Presentation

Citation preview

1

XP

XSLT II

Robin Burke

ECT 360

2

XP

Outline

Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates

3

XP

Conditional nodes

XSLT supports two kinds of conditional elements:

<xsl:if> <xsl:choose>

If is only if-then (no else)Choose is more general

4

XP

Example

if choose

5

XP

Comparison operators

6

XP

Predicates

XPath expressionstest for a conditionresult = set of nodes that fulfill that

condition

7

XP

Example

from last week

8

XP

Numbering

Number nodes using:<xsl:number>position()

Using position():Nodes are numbered by position in

result documentCan be used in conditional statements

9

XP

Number element

Nodes are numbered according to position in source document

Attributes:value=expression: any XPath expression

that evaluates to a number (i.e. position())count=pattern: specifies which nodes to

countlevel=type: tree level for nodes to count;

can be any, single, or multiple

10

XP

USING <XSL:NUMBER>

Attributes:from=pattern: pattern indicates where

numbering should restartformat=pattern: pattern indicates

number formatgrouping-size, grouping-separator:

indicate how digits are grouped and separator character

11

XP

Example

numbering

12

XP

XPath numerical functions

13

XP

XPath string functions

14

XP

XPath operators

15

XP

Example

Calculating the average

16

XP

Number format

XPath function format-number Syntax: format-number(value, format) Example: format-number(56823.847,

'#,##0.00') displays 56,823.85

17

XP

Number format syntax

18

XP

Decimal format

XSL element for defining number formats

Named decimal format passed as argument to format-number

19

XP

Variables

Not variable! User-defined name that stores a particular

value or object Types:

numbertext stringnode setbooleanresult tree fragment

20

XP

Using variables

Syntax: <xsl:variable name=“name” select=“value”/>

Example: <xsl:variable name=”Months” select=”12” />

Names are case-sensitive Value only set once upon declaration Enclose text strings in single-quotes

21

XP

Using variables

Value can be XPath expression Value can be XML fragment

Syntax:

<xsl:variable name=”Logo”>

<img src=”logo.gif” width=”300” height=”100” />

</xsl:variable>

22

XP

Variable reference

Syntax: $variable-name Example: $Months Referencing tree fragments:

Do not use $variable-nameUse <xsl:copy-of> to reference value

23

XP

COPYING

24

XP

Global variables

Can be referenced from anywhere within the style sheet

Must be declared at the top level of the style sheet, as a direct child of the <xsl:stylesheet> element

Must have a unique variable name Evaluated only once when stylesheet

is invoked

25

XP

Local variables

Referenced only within template Can share name with other local or

global variable Reevaluated when template is used

26

XP

Examples

average numbering

27

XP

Parameters

Similar to variables, but:Value can be changed after it is

declaredCan be set outside of scope

Syntax: <xsl:param name=“name” select=“value”/>

Example: <xsl:param name=”Filter” select=”'C103'” />

To reference: $param-name

28

XP

External parameters

Depends on XSLT processor Some work by appending parameter

value to url Command line processors allow

external parameter setting:MSXMLSaxon

29

XP

Template parameters

Local in scope Created inside <xsl:template>

element Used to pass parameters to template

30

XP

Idea

Called template contains param definitions

Calling template place <xsl:with-param> element in

<xsl:apply-templates> element or <xsl:call-template> element Syntax: <xsl:with-param name=“name”

select=“value”/> No error if calling param name does not

match template param name

31

XP

Named template

Template not associated with node set Collection of commands that are

accessed from other templates in the style sheet

Syntax: <xsl:template name="name">

XSLT elements

</xsl:template>

32

XP

Calling named templates

Syntax:

<xsl:call-template name=”name”>

<xsl:with-param />

<xsl:with-param />

...

</xsl:call-template>

33

XP

Example

34

XP

Functional programming

Functional programming language:Relies on the evaluation of functions

and expressions, not sequential execution of commands

Different from C family of languages• LISP, Scheme, ML

35

XP

Templates as functions

A template supplies a result fragmentincorporated into the final documentthink of this as substitutionfunction call replaced by result

36

XP

Iteration

How to handle iteration with templates?stars for ratings

Possibilitiesconvert numbers into node sets

• weird since node sets are supposed to come from the document

recursive templates

37

XP

Recursion

Recursionbase case

• when do I stop?

recursive case• how do I bite off one piece• call template again with the rest

38

XP

Example

Template: star(rating)desired output: # stars = # in rating

Base case:rating = 0output: nothing

Recursive caserating > 0output: * plus result of star (rating-1)

39

XP

Example, cont'd

star(3) replaced by * star(2) replaced by ** star(1) replaced by *** star(0) ***

40

XP

Pseudocode

stars (n)if n = 0 return ''else

• return '*' + stars(n-1)

41

XP

Recursive pattern

Syntax with <xsl:if>:<xsl:template name=”template_name”>

<xsl:param name=”param_name” select=”default_value” />

...

<xsl:if test=”stopping_condition”>

...

<xsl:call-template name=”template_name”>

<xsl:with-param name=”param_name” select=”new_value” />

</xsl:call-template>

...

</xsl:if>

</xsl:template>

42

XP

Example

43

XP

Another example

"best rated" jeep supplier Finding maximum

built into XSLT 2.0

44

XP

Recursive statement?

45

XP

What about the name?

46

XP

Homework #5

Case 1 from Chapter 7 Plus doing it "correctly" with CSS