Extensible Stylesheet Language XSL - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Extensible Stylesheet Language XSL

Description:

XSL is a markup language defined in XML that enables you to ... h3 font color='Red' Funny Stuff! /font /h3 /xsl:when xsl:when test='genre='Thriller' ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 27
Provided by: steveandm
Category:

less

Transcript and Presenter's Notes

Title: Extensible Stylesheet Language XSL


1
Extensible Stylesheet Language (XSL)
  • Special Edition
  • Using XSLT
  • Michael Floyd
  • Que Publishing
  • Chapters 1, 2, 4

2
What is XSL?
  • XSL is a markup language defined in XML that
    enables you to describe how XML data will be
    transformed into another XML vocabulary, HTML, or
    text.
  • Introduced in 1998
  • Split into three specifications
  • Extensible Stylesheet Language Formatting Objects
    (XSL-FO)
  • Strictly addresses formatting
  • XSL Transformation Language (XSLT)
  • Allows you to convert of transform XML documents
    to other forms
  • XML Path Language (XPATH)
  • Allows you to locate an and access parts of an
    XML document

3
Benefits of XSL
  • You can use XSLt with XPATH expressions to
    accomplish many of the same tasks that require
    use of the DOM. Those of you who had difficulty
    with the DOM might find XSL easier to work with.

4
XSL Features
  • Uses a style sheet that can be associated with an
    XML document by
  • Referencing it directly. The XML processor reads
    the stylesheet directive and loads the sheet and
    processes it.
  • Generating it dynamically using the DOM. You
    create the style sheet as a DOM object and call
    the DOMs transformNode method.

5
XSL Transformations
  • Provides a set of elements that allow you to
    locate parts of an XML document and transform
    them to another form
  • XML to HTML (We saw the need for this last week)
  • XML to XML (When you want a document that fits
    one schema to fit another)
  • XPATH assists in the searching process
  • The most common use is to take an XML document
    and convert it to HTML while including a CSS
    document to format the HTML nicely.

6
XSL Transformations Part Two
  • Provides a large set of elements and attributes
    that allow you to
  • Read the source tree and generate the result tree
  • Copy and edit elements, attributes, text, etc
    from the source tree (the tree loaded into
    memory) and add them to the result tree (the
    tree being created on the fly that contains the
    results of applying transformations to the source
    tree).
  • Test for conditions
  • Perform conditional processing
  • Iterate through a group of elements
  • Search and sort elements
  • Set variables
  • Invoke scripts

7
XPATH
  • General mechanism for traversing a document tree
    to locate and access node/s
  • Also uses with the XML Linking Specification
    (XLINK)
  • Used with XSLT to select and transform part or
    all of a document
  • You can use XPATH expressions as arguments to DOM
    method calls that return node/s such as
    SelectNodes()
  • Allow you to specify conditions
  • Can generate text that will be placed in the
    result tree.

8
XSL Formatting Objects
  • Similar in concept to CSS
  • Used with complex structures
  • Uses XML notation
  • Allows you to change the order in which the
    elements appear
  • Much newer technology than CSS
  • Generally renders documents as PDF files since
    absolute positioning is used

9
XSL Processing
  • The XML document loaded into memory is the source
    tree as it represents the source document
  • The goal is to create a second tree, the result
    tree.
  • Rules in the XSL Style sheet (called templates)
  • Walk through the source tree
  • Select components to process
  • Tranformation occurs

10
Creating the XSL Style Sheet
  • You only have to transform the desired portions
    of the source tree
  • The XSL style sheet contains an XML declaration
    indicating its an XML document
  • .w3.org/1999/XSL/Transform
  • When the XML processor sees the style sheet
    reference, it creates the document tree. Then the
    XSLT processor applies the stylesheet to the XML
    document creating the result tree.
  • The URI varies depending on the version of XSL
    youre using.
  • 1.0 is the current version
  • An official W3C recommendation as of 10/15/2001
  • 2.0 is a work in progress
  • is the root element
  • The XSL style sheet also contains an xsloutput
    element that indicates the result trees type
    Other values include
    text or xml

11
Templates and Template Rules
  • Templates are the individual statements used to
    create a transformation
  • A collection of templates is known as a template
    rule
  • sets a pointer to the
    root element in the source document
  • Known as the root template rule
  • All stylesheet contain this rule.
  • The match attribute specifies which element/s you
    are processing. The / character indicates the
    root element.
  • The / character is known as a pattern.
  • The template element is a top-level element
    because its a child of the xslStyleSheet root
    element.

12
Understanding Patterns
  • A pattern is the syntax used to navigate around
    the source tree.
  • Used in the match attribute of a template rule.
  • Basic pattern syntax is easy to learn.
  • Allows you to perform 80 of what you need to
    navigate the source tree.
  • Similar to DOS syntax used to navigate a PCs
    hard drive directories.
  • You can always move to the root directory by
    using the / pattern.

13
Characters Used to Construct Patterns
14
Creating Text
  • Anything in a template rule that is not a
    recognized stylesheet element will be placed in
    the result tree as a text node
  • Company entName /

15
Getting the Content of an Element
  • The element retrieves the content from
    an element or attribute.
  • Takes an optional Select attribute and
    pattern/location path that allows you to specify
    node/s from which to get content.
  • A beginning slash is not needed if the pointer is
    currently set at the root node via match/
  • tells the XSLT processor to do something.

16
Outputting the Results
  • The easiest way to attach your XSL Stylesheet to
    the XML document is by using the same method we
    used to attach a CSS stylesheet to an XML
    document.
  • However you set the mime type to text/xsl instead
    of text/css.
  • hrefinvoice.xsl?

17
Controlling the Output
  • You can only convert XML to HTML, text, or XML
    using the element.
  • It doesnt change the actual source code when you
    view the page source in the Web browser.

18
Retrieving Attributes
  • Preface the attribute name with an _at_ sign
  • This is MUCH easier than grabbing attributes
    using the DOM.

19
Creating an HTML File from an XML File
  • The following XSLT file will display the title
    and genre of just the FIRST movie in the xml
    file. It does not do any looping on its own.
  • w.w3.org/1999/XSL/Transform"
  • My Favorite Movies
  • Rating dvd/_at_numberstars" /
  • Title vd/title" /
  • Genre vd/genre" /

20
Explaining the XSL File
  • You have to move from the current location of the
    pointer to the level in the XML that contains the
    content you want to display. Thats why we had to
    list two parent elements before title and genre
  • The pointers current location was on the root
    element because the template elements match
    attribute was set to /
  • Even though numberstars is an attribute of the
    DVD element, a slash is used. The _at_ sign denotes
    an attribute.
  • Since only the first DVDs information was
    displayed, we need a way to loop through the
    entire source tree.
  • XPATH will show us the way!

21
Using For-Each to Iterate
  • Rating /
  • Title
  • Genre
  • The xslfor-each element allows you to use the
    select attribute to move to the child element you
    want the processor to point to.
  • Leave off the ending slash
  • Make sure you close the xslfor-each element tag
  • Anything between the opening and closing
    xslfor-each tag will be processed once for each
    instance of that element or attribute in the XML
    document.

22
Fine Tuning Your Iteration
  • You can use xslsort to sort by an element or
    attribute. You can also choose whether the sort
    should be in ascending or descending order
  • The sort element comes after the xslfor-each
    element
  • The select attribute value contains the
    element/attribute that will be used to sort.
  • The default value is . which sets the strng
    value of the urrent node to be used as the sort
    key.
  • Order can be set to ascending or descending.
    Ascending is the default.
  • If you have a list of values with mixed case and
    you want one case to take precedence over the
    other you can use the case-order attribute and
    set the value to either upper-first or
    lower-first.

23
Conditional Processing Using XSLif
  • XSLT provides two methods for processing based
    upon a set of conditions
  • operates like an if/then construct. You
    specify the condition for the if element using
    the test attribute
  • 1999"
  • Year Released select"released" /

24
Conditional Processing Using xslchoose
  • Slightly more complex than using xslif
  • Works like Select Case or Switch
  • Lets you choose from a series of choices
  • Choices are written using a sequence of xslwhen
    elements
  • Funny
    Stuff!
  • You'll be on the
    edge of your seats!

25
Adding Comments
  • You can create a comment node using
  • This allows you to add a comment to the processed
    document

26
Including and Importing Style Sheets
  • You can combine XSL Stylesheets using the
    xslimport element in the first style sheet.
  • /
  • The elements that are imported will precede those
    in the stylesheet containing the import element.
Write a Comment
User Comments (0)
About PowerShow.com