XSLT - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

XSLT

Description:

The value of its 'match' attribute contains an XPath statement that is used as a ' ... The template xsl:template match='/' says to select the entire file ... – PowerPoint PPT presentation

Number of Views:227
Avg rating:3.0/5.0
Slides: 26
Provided by: david2771
Category:
Tags: xslt | match

less

Transcript and Presenter's Notes

Title: XSLT


1
XSLT
  • Reference
  • www.cis.upenn.edu/matuszek/ cit597-2002/Lectures/
    xslt.ppt

2
XSLT
  • XSLT stands for Extensible Stylesheet Language
    Transformations
  • XSLT is used to transform XML documents into
    other kinds of documents--usually, but not
    necessarily, XHTML
  • XSLT uses two input files
  • The XML document containing the actual data
  • The XSL document containing both the framework
    in which to insert the data, and XSLT commands to
    do so

3
Very simple example
  • File data.xml
  • type"text/xsl" href"render.xsl"?Howdy
    !
  • File render.xsl
  • version"1.0 xmlnsxsl"http//www.w3.or
    g/1999/XSL/Transform"
    match"/"



4
The .xsl file
  • An XSLT document has the .xsl extension
  • The XSLT document begins with
  • xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform"
  • Contains one or more templates, such as
  • ...
  • And ends with

5
The .xsl file
  • An XSLT style sheet contains "template rules"
    that define
  • the criteria for selecting elements from the
    source tree
  • providing instructions for restructuring the
    selected content to create the result tree
  • Template rules are the key building blocks of
    XSLT transformations.
  • There are two parts to a template rule a pattern
    and a template.
  • The "pattern" (usually an element name) is then
    "matched" against elements in the source tree to
    determine which nodes to select for processing.
  • The "template" provides instructions for
    processing the selected content.
  • Each template rule constructs a result tree
    fragment of markup and content. An XSLT
    Transformation's result tree is created when all
    of the result tree fragments are combined in the
    order specified to form a completed whole.

6
The .xsl file
  • The xsltemplate element is used to encapsulate
    template rules. The value of its "match"
    attribute contains an XPath statement that is
    used as a "pattern" to select source elements for
    processing.
  • A document's first template almost always has a
    "match" value of "/", which means "the document
    root."
  • The "apply-templates" element signals to an XSLT
    processor to look for other template rules and
    apply them at that point in the style sheet's
    structure. The results of each rule combine to
    form the "result tree" of the completed
    transformation.

7
Finding the message text
  • The template says to
    select the entire file
  • You can think of this as selecting the root node
    of the XML tree
  • Inside this template,
  • selects the
    message child
  • Alternative Xpath expressions that would also
    work
  • ./message
  • /message/text() (text() is an XPath function)
  • ./message/text()

8
Putting it together
  • The XSL was
    select"message"/
  • The chooses the root
  • The is written to the output
    file
  • The contents of message is written to the output
    file
  • The is written to the
    output file
  • The resultant file looks like
    Howdy!

9
How XSLT works
  • The XML text document is read in and stored as a
    tree of nodes
  • The template is used to
    select the entire tree
  • The rules within the template are applied to the
    matching nodes, thus changing the structure of
    the XML tree
  • If there are other templates, they must be called
    explicitly from the main template
  • Unmatched parts of the XML tree are not changed
  • After the template is applied, the tree is
    written out again as a text document

10
Where XSLT can be used
  • With an appropriate program, such as Xerces, XSLT
    can be used to read and write files
  • A server can use XSLT to change XML files into
    HTML files before sending them to the client
  • A modern browser can use XSLT to change XML into
    HTML on the client side
  • This is what we will mostly be doing in this
    class
  • Most users seldom update their browsers
  • If you want everyone to see your pages, do any
    XSL processing on the server side
  • Otherwise, think about what best fits your
    situation

11
Modern browsers
  • Internet Explorer 6 best supports XML
  • Netscape 6 supports some of XML
  • Internet Explorer 5.x supports an obsolete
    version of XML
  • IE5 is not good enough for this course

12
xslvalue-of

  • selects the contents of an element and adds it to
    the output stream
  • The select attribute is required
  • Notice that xslvalue-of is not a container,
    hence it needs to end with a slash
  • Example (from an earlier slide)

13
xslfor-each
  • xslfor-each is a kind of loop statement
  • The syntax is expression" Text to insert and rules to
    apply
  • Example to select every book (//book) and make
    an unordered list (
      ) of their titles (title),
      use
        select"//book"
      • select"title"/


    14
    Filtering output
    • You can filter (restrict) output by adding a
      criterion to the select attributes value

      • select"title../author'Terry Pratchett'"/
    • This will select book titles by Terry Pratchett

    15
    Filter details
    • Here is the filter we just used select"title../author'Terry Pratchett'/
    • author is a sibling of title, so from title we
      have to go up to its parent, book, then back down
      to author
    • This filter requires a quote within a quote, so
      we need both single quotes and double quotes
    • Legal filter operators are !
      lt gt
    • Numbers should be quoted, but apparently dont
      have to be

    16
    But it doesnt work right!
    • Heres what we did select"//book"

    • erry Pratchett'"/
    • This will output
    • and
    • for every book,
      so we will get empty bullets for authors other
      than Terry Pratchett
    • There is no obvious way to solve this with just
      xslvalue-of

    17
    xslif
    • xslif allows us to include content if a given
      condition (in the test attribute) is true
    • Example


    • This does work correctly!

    18
    xslchoose
    • The xslchoose ... xslwhen ... xslotherwise
      construct is XMLs equivalent of Javas switch
      ... case ... default statement
    • The syntax is test"some condition" ... some code
      ...
      ... some code ... choose

    xslchoose is often used within an
    xslfor-each loop

19
xslsort
  • You can place an xslsort inside an xslfor-each
  • The attribute of the sort tells what field to
    sort on
  • Example
      select"//book" select"author"/
    • select"title"/ by select"author"

  • This example creates a list of titles and
    authors, sorted by author

20
Book example



  • Table of Contents


    template
  • Etc.

21
xslapply-templates
  • The element applies a
    template rule to the current element or to the
    current elements child nodes
  • If we add a select attribute, it applies the
    template rule only to the child that matches
  • If we have multiple
    elements with select attributes, the child nodes
    are processed in the same order as the
    elements

22
When templates are ignored
  • Templates arent used unless they are applied
  • Exception Processing always starts with
    select"/"
  • If it didnt, nothing would ever happen
  • If your templates are ignored, you probably
    forgot to apply them
  • If you apply a template to an element that has
    child elements, templates are not automatically
    applied to those child elements

23
Applying templates to children
With this lineXML by Gregory Brill
  • XML
    Gregory Brill

  • / select"/book/author"/
    match"/book/author" by select"."/

Without this lineXML
24
An Example
25
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com