XSLT Stylesheets - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT Stylesheets

Description:

Using XSLT to create HTML. Write a stylesheet which creates an HTML document ... Each of these takes an element attribute which contains a list of element names. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 17
Provided by: amyi1
Category:

less

Transcript and Presenter's Notes

Title: XSLT Stylesheets


1
XSLT Stylesheets
  • Some more advanced examples (adapted from the
    Edinburgh LT site)

2
Overview
  • Introduction to Stylesheets
  • Using XSLT to create HTML
  • Using XSLT to do XML to XML conversion
  • Using XSLT to sort and count

3
Stylesheets
  • A stylesheet is a file which contains a
    declarative set of rules for converting an XML
    document into another document, which can be XML,
    HTML, plain text

4
XSLT Transformations
  • XSLT style sheet template rules
  • pattern which specifies which tree it applies to
  • pattern which specifies which tree it should
    output
  • XSLT processor
  • reads XML document and XSLT stylesheet
  • carries out the instructions in the stylesheet
  • outputs a new document

5
Stylesheet top-level elements
  • lt!DOCTYPE xslstylesheetgt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsloutput method"html"/gt
  • ltxsltemplate matchgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

6
Template Matching
  • XPath is a language for addressing parts of an
    XML document, and is used by XSLT in the match
    attribute of a template e.g. lttemplate
    matchsentencegt matches any sentence element.
  • The stylesheet processor goes through the XML
    document one element at a time, finds the first
    template which that element matches, and carries
    out the instructions in that template.

7
Template Matching (cont)
  • If the element does not match any template in the
    stylesheet, then the default behaviour is for the
    processing to pass through to the children of
    this element without carrying ot any
    instructions.
  • When the processing reaches an element which has
    only text children, the result of processing
    these children is to print out the text.

8
Some examples of patterns
  • matches any element
  • / matches the top-level element
  • sentence/word matches any word element with a
    sentence parent
  • word_at_posnoun matches any noun element whose
    pos attribute has the value noun
  • Many many other possibilities

9
Using XSLT to create HTML
  • Write a stylesheet which creates an HTML document
  • Load the document into a browser
  • (Some browsers will do the XSL processing
    internally e.g. IE5 but many others dont yet
    allow this )

10
displaysentence.xsl
  • ltxsltemplate match"/"gt
  • lthtmlgtltheadgtlttitlegtParagraphlt/titlegtlt/headgt
  • ltbodygt
  • ltxslapply-templates/gt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • ltxsltemplate match"word_at_pos'noun'"gt
  • ltfont color"red"gt
  • ltxslapply-templates/gt
  • lt/fontgt
  • lt/xsltemplategt

11
removemarkup.xsl
  • ltxsltemplate match"word"gt
  • ltxslcopygt
  • ltxslcopy-of select"_at_"/gt
  • ltxslapply-templates/gt
  • lt/xslcopygt
  • lt/xsltemplategt

12
Treatment of whitespace
  • You can specify what should happen to the
    whitespace in a document using the
    ltxslstrip-space/gt and ltxslpreserve-space/gt
    elements. Each of these takes an element
    attribute which contains a list of element names.
  • If you want to see how this works, try putting
  • ltxslstrip-space elementsparagraph/gt or
  • ltxslstrip-space elementsparagraph sentence/gt
  • below the
  • ltxsloutput methodxml/gt declaration.

13
Sorting
  • XSLT can be used to sort elements by alphabetical
    or numerical order, according to attribute
    values, text contents, in ascending or descending
    order, and more

14
sortwords.xsl
  • ltxsltemplate match"sentence"gt
  • ltxslapply-templates select"word"gt
  • ltxslsort select"."/gt
  • lt/xslapply-templatesgt
  • lt/xsltemplategt
  • ltxsltemplate match"word"gt
  • ltxslcopygt
  • ltxslcopy-of select"_at_"/gt
  • ltxslapply-templates/gt
  • lt/xslcopygt
  • lt/xsltemplategt

15
Counting
  • XSLT can also be used to count or number elements

16
countwords.xsl
  • ltxsltemplate match"sentence"gt
  • ltxslfor-each select"word"gt
  • ltpgt
  • ltxslnumber value"position()" format"1.
    "/gt
  • ltxslvalue-of select"."/gt
  • lt/pgt
  • lt/xslfor-eachgt
  • lt/xsltemplategt
Write a Comment
User Comments (0)
About PowerShow.com