XSLT II - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT II

Description:

XSLT II Robin Burke ECT 360 – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 47
Provided by: Jama82
Category:

less

Transcript and Presenter's Notes

Title: XSLT II


1
XSLT II
  • Robin Burke
  • ECT 360

2
Outline
  • Conditionals
  • Numbering
  • Functions and operators
  • Variables and parameters
  • Named and recursive templates

3
Conditional nodes
  • XSLT supports two kinds of conditional elements
  • ltxslifgt
  • ltxslchoosegt
  • If is only if-then (no else)
  • Choose is more general

4
Example
  • if
  • choose

5
Comparison operators
6
Predicates
  • XPath expressions
  • test for a condition
  • result set of nodes that fulfill that condition

7
Example
  • from last week

8
Numbering
  • Number nodes using
  • ltxslnumbergt
  • position()
  • Using position()
  • Nodes are numbered by position in result document
  • Can be used in conditional statements

9
Number element
  • Nodes are numbered according to position in
    source document
  • Attributes
  • valueexpression any XPath expression that
    evaluates to a number (i.e. position())
  • countpattern specifies which nodes to count
  • leveltype tree level for nodes to count can be
    any, single, or multiple

10
USING ltXSLNUMBERgt
  • Attributes
  • frompattern pattern indicates where numbering
    should restart
  • formatpattern pattern indicates number format
  • grouping-size, grouping-separator indicate how
    digits are grouped and separator character

11
Example
  • numbering

12
XPath numerical functions
13
XPath string functions
14
XPath operators
15
Example
  • Calculating the average

16
Number format
  • XPath function format-number
  • Syntax format-number(value, format)
  • Example format-number(56823.847, ',0.00')
    displays 56,823.85

17
Number format syntax
18
Decimal format
  • XSL element for defining number formats
  • Named decimal format passed as argument to
    format-number

19
Variables
  • Not variable!
  • User-defined name that stores a particular value
    or object
  • Types
  • number
  • text string
  • node set
  • boolean
  • result tree fragment

20
Using variables
  • Syntax ltxslvariable namename
    selectvalue/gt
  • Example ltxslvariable nameMonths select12
    /gt
  • Names are case-sensitive
  • Value only set once upon declaration
  • Enclose text strings in single-quotes

21
Using variables
  • Value can be XPath expression
  • Value can be XML fragment
  • Syntax
  • ltxslvariable nameLogogt
  • ltimg srclogo.gif width300 height100
    /gt
  • lt/xslvariablegt

22
Variable reference
  • Syntax variable-name
  • Example Months
  • Referencing tree fragments
  • Do not use variable-name
  • Use ltxslcopy-ofgt to reference value

23
COPYING
24
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 ltxslstylesheetgt
    element
  • Must have a unique variable name
  • Evaluated only once when stylesheet is invoked

25
Local variables
  • Referenced only within template
  • Can share name with other local or global
    variable
  • Reevaluated when template is used

26
Examples
  • average
  • numbering

27
Parameters
  • Similar to variables, but
  • Value can be changed after it is declared
  • Can be set outside of scope
  • Syntax ltxslparam namename selectvalue/gt
  • Example ltxslparam nameFilter select'C103'
    /gt
  • To reference param-name

28
External parameters
  • Depends on XSLT processor
  • Some work by appending parameter value to url
  • Command line processors allow external parameter
    setting
  • MSXML
  • Saxon

29
Template parameters
  • Local in scope
  • Created inside ltxsltemplategt element
  • Used to pass parameters to template

30
Idea
  • Called template
  • contains param definitions
  • Calling template
  • place ltxslwith-paramgt element in
    ltxslapply-templatesgt element
  • or ltxslcall-templategt element
  • Syntax ltxslwith-param namename
    selectvalue/gt
  • No error if calling param name does not match
    template param name

31
Named template
  • Template not associated with node set
  • Collection of commands that are accessed from
    other templates in the style sheet
  • Syntax
  • ltxsltemplate name"name"gt
  • XSLT elements
  • lt/xsltemplategt

32
Calling named templates
  • Syntax
  • ltxslcall-template namenamegt
  • ltxslwith-param /gt
  • ltxslwith-param /gt
  • ...
  • lt/xslcall-templategt

33
Example
34
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
Templates as functions
  • A template supplies a result fragment
  • incorporated into the final document
  • think of this as substitution
  • function call replaced by result

36
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
  • recursive templates

37
Recursion
  • Recursion
  • base case
  • when do I stop?
  • recursive case
  • how do I bite off one piece
  • call template again with the rest

38
Example
  • Template star(rating)
  • desired output stars in rating
  • Base case
  • rating 0
  • output nothing
  • Recursive case
  • rating gt 0
  • output plus result of star (rating-1)

39
Example, cont'd
  • star(3)
  • replaced by star(2)
  • replaced by star(1)
  • replaced by star(0)

40
Pseudocode
  • stars (n)
  • if n 0 return ''
  • else
  • return '' stars(n-1)

41
Recursive pattern
  • Syntax with ltxslifgt
  • ltxsltemplate nametemplate_namegt
  • ltxslparam nameparam_name
    selectdefault_value /gt
  • ...
  • ltxslif teststopping_conditiongt
  • ...
  • ltxslcall-template nametemplate_namegt
  • ltxslwith-param nameparam_name
    selectnew_value /gt
  • lt/xslcall-templategt
  • ...
  • lt/xslifgt
  • lt/xsltemplategt

42
Example
43
Another example
  • "best rated" jeep supplier
  • Finding maximum
  • built into XSLT 2.0

44
Recursive statement?
45
What about the name?
46
Homework 5
  • Case 1 from Chapter 7
  • Plus doing it "correctly" with CSS
Write a Comment
User Comments (0)
About PowerShow.com