Transcript Document

XP
XSLT II
Robin Burke
ECT 360
1
Homework #4

Forgot we hadn't covered "choose"


2
becomes extra credit
My books.xml didn't include
descriptions
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
XP
Comparison operators
6
Predicates
XPath
expressions
test for a condition
 result = set of nodes that fulfill that
condition

Format

7
elementName[condition]
XP
Example

8
from last week
XP
Numbering

XP
Issues
what is counted?
 what is the format of the numbering?
 how are numbers at different places in
the tree related?

9
Numbering

XP
Number nodes using:
<xsl:number>
 position()


Using position():
Nodes are numbered by position in
result document
 Can be used in conditional statements

10
Using <xsl:number>

Attributes:
from=pattern: pattern indicates where
numbering should restart
 format=pattern: pattern indicates
number format
 grouping-size, grouping-separator:
indicate how digits are grouped and
separator character

11
XP
Number element
Nodes are numbered according to
position in source document
 More flexibility

12
XP
Example
numbering with position
 numbering with number element

13
XP
Number element

14
XP
Attributes:
value=expression: any XPath
expression that evaluates to a number
(i.e. position())
count=pattern: specifies which nodes
to count
level=type: tree level for nodes to
count; can be any, single, or multiple
Numbering, cont’d

count attribute
indicates what is being counted
 default = count elements of the same
name
 otherwise = specify an XPath pattern

• all matching elements included
15
XP
Numbering relationships
how does the number of one element
depend on other elements?
 Possibilities


only depends on immediate siblings
• single

depends on “parent”
• multiple

depends on “cousins”
• any
16
XP
Formatting

format

type
• 1, a, A, i, I

17
other punctuation
XP
Example

18
table of contents
XP
Internal computations

Often performed using XPath


Can perform calculations of nodes
selected by an XPath expression

19
sort of like SQL
XPath functions
XP
XPath numerical functions
20
XP
XPath string functions
21
XP
XPath operators
22
XP
Example
Calculating the average
 Problem


23
redundant calculation
XP
Variables



Not variable!
User-defined name that stores a particular
value or object
Types:





24
number
text string
node set
boolean
result tree fragment
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

25
XP
Using variables, cont'd
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>
26
XP
Variable reference
Syntax: $variable-name
 Example: $Months
 Except


Referencing tree fragments:
• Do not use $variable-name
• Use <xsl:copy-of> to reference value
27
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

28
XP
Local variables
Referenced only within template
 Can share name with other local or
global variable
 Reevaluated when template is used

29
XP
Examples

30
average with variables
XP
Number format
XPath function format-number
 Syntax: format-number(value, format)
 Example: format-number(56823.847,
'#,##0.00') displays 56,823.85

31
XP
Number format syntax
32
XP
Decimal format
XSL element for defining number
formats
 Named decimal format passed as
argument to format-number

33
XP
Example

34
average with format
XP
Parameters

Similar to variables, but:
Value can be changed after it is
declared
 Can be set outside of scope


Syntax: <xsl:param name=“name”
select=“value”/>

Example: <xsl:param name=”Filter”
select=”'C103'” />

35
To reference: $param-name
XP
External parameters
Depends on XSLT processor
 Some work by appending parameter
value to url
 Command line processors allow
external parameter setting:

MSXML
 Saxon

36
XP
Template parameters
Local in scope
 Created inside <xsl:template>
element
 Used to pass parameters to template

37
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”/>

38
No error if calling param name does not
match template param name
XP
Named template
XP
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>
39
Calling named templates

40
Syntax:
<xsl:call-template name=”name”>
<xsl:with-param />
<xsl:with-param />
...
</xsl:call-template>
XP
Example

41
average with named template
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
42
XP
Templates as functions

A template supplies a result fragment
incorporated into the final document
 think of this as substitution
 function call replaced by result

43
XP
Iteration

How to handle iteration with
templates?


stars for ratings
Possibilities

convert numbers into node sets
• weird since node sets are supposed to
come from the document

44
recursive templates
XP
Recursion

Recursion

base case
• when do I stop?

recursive case
• how do I bite off one piece
• call template again with the rest
45
XP
Example

Template: star(rating)


desired output: # stars = # in rating
Base case:
rating = 0
 output: nothing


Recursive case
rating > 0
 output: * plus result of star (rating-1)

46
XP
Example, cont'd
star(3)
 replaced by * star(2)
 replaced by ** star(1)
 replaced by *** star(0)
 ***

47
XP
Pseudocode

stars (n)
if n = 0 return ''
 else

• return '*' + stars(n-1)
48
XP
Recursive pattern

XP
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>
49
Example

50
ratings with stars
XP
Another example
"best rated" jeep supplier
 Finding maximum


51
built into XSLT 2.0
XP
Recursive statement?
52
XP
What about the name?
53
XP
Homework #5
Case 1 from Chapter 7
 Plus doing it "correctly" with CSS
 Also 10/27


54
Schema milestone for final project
XP