XSL - PowerPoint PPT Presentation

About This Presentation
Title:

XSL

Description:

The most important bit of XSL ... firstName Selma /firstName lastName Blair /lastName studentNumber 200133333 /lastname ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 24
Provided by: SarahM100
Category:
Tags: xsl | blair | selma

less

Transcript and Presenter's Notes

Title: XSL


1
XSL
  • Unit 6
  • November 2

2
XSL
  • XSL
  • eXtensible Stylesheet Language
  • Basically a stylesheet for XML documents
  • XSL has three parts
  • XSLT
  • XPath
  • XSL-FO

3
XSLT
  • The most important bit of XSL
  • XSLT allows us to transform an XML document into
    another XML document
  • For instance, transform an XML document into an
    XHTML document
  • XSLT relies on using XPath
  • Were going to use it, but not cover it
  • Dont have to go into detail in order to create
    simple, effective XSL documents

4
XSLT Basics
  • The whole idea behind XSLT is template matching
  • Whats a template?
  • Lets say we have a list of students like what
    weve done in class
  • ltstudentgt
  • ltfirstNamegtMarkylt/firstNamegt
  • ltlastNamegtMarklt/lastNamegt
  • ltstudentNumbergt2001-22222lt/studentNumbergt
  • lt/studentgt

5
Templates, cont.
  • We know what one student looks like
  • All students follow the same format
  • Student
  • first name
  • last name
  • student number
  • Every single one of the students in our XML
    document are described in exactly the same manner
  • We can use a template to identify and use the
    parts of each student

6
Templates, cont.
  • ltstudentListgt
  • ltstudentgt
  • ltfirstNamegtMarky lt/firstNamegt
  • ltlastNamegtMark lt/lastNamegt
  • ltstudentNumbergt200122222 lt/lastnamegt
  • lt/studentgt
  • ltstudentgt
  • ltfirstNamegtSelma lt/firstNamegt
  • ltlastNamegtBlair lt/lastNamegt
  • ltstudentNumbergt200133333 lt/lastnamegt
  • lt/studentgt
  • lt/studentListgt

7
Creating a New XSL Stylesheet
  • Created with a text editor
  • Saved with file extension .xsl
  • Just like with HTML, we have to put some bits
    at the top of our XSL file
  • Again, you do not have to have these memorized
    completely
  • I will provide a blank version online when/if
    needed

8
Creating an XSL Stylesheet, cont.
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • Or
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxsltransform version"1.0" xmlnsxsl"http//www
    .w3.org/1999/XSL/Transform"gt
  • You can use either xlsstylesheet or
    xlstransform, they are identical

9
Linking the XSL file to the XML file
  • The top of our XML file should read something
    like this
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • lt?xml-stylesheet type"text/xsl"
    hrefstudentlist.xsl"?gt
  • This is nearly identical to how we link HTML
    pages to CSS files
  • Again, an empty file will be provided for you if
    needed
  • But! You do need to know how to link to an XSL
    file

10
xsltemplate
  • xsltemplate is an XSL element
  • We are going to use it to apply rules when the
    template match is found
  • ltxsltemplate match /gt
  • match / means that well apply the template
    to the entire XML document
  • This is the next line of our XSL file

11
XSL File, so far
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match /gt
  • .
  • lt/xsltemplategt
  • lt/xslstylesheetgt

12
Beginning the Transformation to HTML
  • What do we need for an html file?
  • lthtmlgt
  • ltbodygt
  • Maybe a ltheadgt tag?
  • This is what well add next to our XSL file

13
Beginning HTML
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match /gt
  • lthtmlgt
  • ltheadgtlttitlegtXML Examplelt/titlegtlt/headgt
  • ltbodygt
  • .
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

14
In-Class Example
  • Adding some HTML

15
How do we add data?
  • ltxslvalue-ofgt gives us the value of a certain
    bit of our xml file
  • Which bit?
  • Well use the select property and the information
    about our tag
  • ltxslvalue-of select studentList/student/first_n
    ame/gt
  • This long value for select is based on the
    structure of our XML document
  • ltstudentListgt is our root element
  • ltstudentgt is contained within ltstudentListgt
  • ltfirst_namegt is the data were interested in
  • Just like paths for URLs

16
In class Example
  • Adding some data to our html
  • Using more html tags

17
More than Student?
  • We can get the data for a single student, but
    what about our list of 3 students?
  • Or 300 for that matter?
  • We dont want to cut and paste for every student
  • What if the number of students changes?
  • We can use the ltxslfor-eachgt element to loop
    through each of our students
  • Can use it to look through any sets of data
    nodes (think items with () marks)

18
Using ltxslfor-eachgt
  • ltxslfor-eachgt requires the use of the select
    attribute
  • ltxslfor-each select some expressiongt
  • But what to use?
  • Depends on the repeat of the data
  • In the case of having a list of students
  • select studentList/student
  • Basically you can read this as for each student,
    apply these rules below
  • ltxslfor-each selectstudentList/studentgt

19
In Class Example
  • Adding for-each to loop through the students in
    the XML document
  • Reversing last and first name
  • Changing the format of the data

20
Sorting the Data
  • We can sort the data in our XML files
  • For instance, we can alphabetize our student list
  • Even if its not alphabetical in the XML file,
    the HTML output will have the items in
    alphabetical order
  • We are going to use the element
  • ltxslsortgt

21
Using ltxslsortgt
  • We can specify what we are sorting on
  • First name?
  • Last name?
  • ltxslsort selectlast_namegt
  • Default is to sort ascending (A, BZ)
  • But we can reverse it by using the order
    property
  • ltxslsort selectlast_name orderdescending/gt

22
In Class Example
  • Sorting the student list ascending and descending
  • Using both first and last name
  • Another XML file with products

23
Questions?
Write a Comment
User Comments (0)
About PowerShow.com