XSLT Continued - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT Continued

Description:

Stylesheet Document or Program. XML document rooted in stylesheet element ... Body of template specifies contribution of source elements to result tree ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 15
Provided by: carll8
Category:
Tags: xslt | continued | rooted

less

Transcript and Presenter's Notes

Title: XSLT Continued


1
XSLT Continued
  • CS 431 February 28, 2005
  • Carl Lagoze Cornell University

2
Stylesheet Document or Program
  • XML document rooted in ltstylesheetgt element
  • Body is set of templates or rules
  • match attribute specifies xpath of elements in
    source tree
  • Body of template specifies contribution of source
    elements to result tree

3
Associating an XML document with a transform
4
XSL Execution Model
  • Templates represent a set of rules
  • Rule matching is done within current tree context
  • Rules are not executed in order
  • Default behavior is depth-first walk of tree,
    outputting element values
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/base.xml
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/null.xsl

5
Template Form
  • Elements from xsl namespace are transform
    instructions
  • match attribute value is xpath expression setting
    rule for execution of body
  • Sequential execution within template
  • Non-xsl namespace elements are literals.
  • ltxslapply-templatesgt
  • set context to next tree step (default
    depth-first)
  • re-evaluate rules

6
Result Tree Creation
  • Literals any element not in xsl namespace is
    inserted into result tree
  • ltxsltextgt - send content directly to output
    (retain whitespaces)
  • ltxslvalue-ofgt - extract element values (anywhere
    in the tree)

7
Result Tree Creation (cont)
  • ltxslcopyofgt - Copy selected nodes into result
    tree
  • ltxslelementgt - instantiate an element
  • ltxslattributegt - instantiate an attribute

8
A simple example
  • XML base file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/simple.xml
  • XSLT file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/simple.xsl

9
Modifying rule set and context
  • Mode setting
  • ltxslapply-templates modethisgt
  • ltxsltemplate matchfoo modethisgt
  • ltxsltemplate matchfoo modethatgt
  • Context setting
  • ltxslapply-templates select//bargt
  • Modifies default depth-first behavior
  • Conflict resolution rules
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/elements.xsl
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/elements2.xsl

10
Namespaces in XSLT
  • The XSL document MUST know about the namespaces
    of elements that it references (via XPATH
    expressions) in the instance document
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/baseNS.xml
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/elementsNS.xsl
  • Watch out for the default namespace!!
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/baseNoNS.xml
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/elementsNoNS.xsl

11
XSLT Procedural Programming
  • Sequential programming style
  • Basics
  • for-each loop through a set of elements
  • call-template like a standard procedure call

12
For-each programming example
  • XML base file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/foreach.xml
  • XSLT file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/foreach.xsl

13
Call-template programming example
  • XML base file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/call.xml
  • XSLT file
  • http//www.cs.cornell.edu/lagoze/courses/CS431/200
    5sp/Examples/Lecture10/call.xsl

14
Various other programming constructs
  • Conditionals
  • Variables (declaration and use)
  • Once set, cant be reset
  • Functional programming style
  • Use recursion
  • Some type conversion
  • Parameters
  • Sorting
Write a Comment
User Comments (0)
About PowerShow.com