The XSL Transformation - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

The XSL Transformation

Description:

To View XML documents, we need to format or style them. The styling is directly related to and derived from the structure of the document ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 37
Provided by: ccNct
Category:

less

Transcript and Presenter's Notes

Title: The XSL Transformation


1
The XSL Transformation
2
Introduction
3
Why Styling?
  • XML
  • Concentrate on structure
  • Not concentrate on appearance
  • To View XML documents, we need to format or style
    them
  • The styling is directly related to and derived
    from the structure of the document
  • Two W3C styling sheets
  • CSS Cascading Styling Sheet
  • XSL XML Style-sheet Language

4
Cascading Style Sheet (CSS)
  • Originally for HTML
  • A set of rules that tell the browser which font,
    style, and margin to use to display the text
  • section.title
  • font-family Garamond, Time News Roman,
    seriffont-size 10pt
  • margin5px
  • displayblock
  • font-style italic

5
XML Stylesheet Language (XSL)
  • Support transforming the document before display
  • A transformation lang.
  • A formatting lang.
  • ltPgtltBgt Table of Content lt/Bgtlt/Pgt
  • ltULgt
  • ltxslfor-each select article/section/titlegtltLI
    gtltAgt
  • ltxslvalue-of select./gt lt/Agtlt/LIgt
  • lt/xslfor-eachgt
  • lt/ULgt

6
XSL
  • XSL is organized into two parts
  • XSLT XSL Transformation
  • Move data from one XML rep. to another
  • Electronic commerce, Electronic data exchange,
    Metadata change
  • Standardize Nov. 16, 1999
  • XSLFO XSL Formatting Objects
  • XSLFO and CSS are very similar in scope
  • In practice
  • XSLT is useful
  • CSS is preferred to XSLFO

7
XSL Processors
  • Microsoft
  • LotusXSL
  • www.alphaworks.ibm.com
  • James Clark
  • www.jclark.com

8
Concepts of XSLT (I)
Source Document
Resulting Document
XSLProcessor
XSLT Style Sheet
9
Concept of XSLT (II)
  • XSLT A language to specify transformation of XML
    documents
  • Add elements specifically for viewing
  • Add the logo or address of the company
  • Create new content from an existing one
  • Create table of content
  • Create views for different users
  • Concert between different DTDs
  • Transform XML documents into HTML

10
Concept of XSLT (III)
  • XSLT transforms one XML tree into another
  • select particular nodes from the input tree
  • reorder/process nodes
  • output nodes
  • Tree nodes
  • The root
  • Elements
  • Text
  • Attributes
  • Namespaces
  • Processing instructions
  • Comments

11
(No Transcript)
12
Basic XSLT
13
Example Scenario
  • A monthly e-zine, Pineapplesoft Link
  • E-mail to subscribers
  • Post a copy on Web
  • Use one document structure
  • XML
  • Two different presentations
  • XSLT

14
Using LotusXSL
  • e-zine Example xml_example/chap05
  • e-zine.xml
  • e-zine.xsl
  • Set up the LotusXSL Environment
  • set pathc\jdk1.1.8\binPATH
  • set classpathXSL\lotusxsl.jarCLASSPATH
  • set classpathXSL\xalan.jarXSL\xerces.jarCLASSP
    ATH
  • cd XSL
  • java org.apache.xalan.xslt.Process IN ltxmlgt -XSL
    ltXSLTgt -OUT ltOUTgt -ltFORMATgt

15
Stylesheet Element
  • XSL is a well-formed XML document
  • The tree of the source document
  • The tree of the resulting document
  • How to transform one into the other
  • Top element of XSLT
  • ltxslstylesheet version"1.0 xmlnsxslhttp//ww
    w.w3.org/1999/XSL/Transform xmlnshttp//www.w3.o
    rg/TR/REC-html40gt

16
Template Elements
  • Example
  • ltxsltemplate match"section/title"gt
    ltPgtltIgtltxslapply-templates/gtlt/Igtlt/Pgtlt/xsltemplat
    egt
  • A template has two parts
  • The match attribute a path to the element in the
    source tree to which the template applies
  • The content list the elements to insert in the
    resulting tree

17
Path
  • XML path is similar to file paths
  • Start from the root of an XML document
  • Elements are separated by /
  • The root is a node before the top-level element
  • Path Tips
  • / immediate children of a node
  • /article/title
  • // all the descendant from a node
  • /article//title
  • wildcard character (/article/)
  • or character (title p)

18
Path Example
root
/
/article
article
title
date
abstract
keyword
section
/article/title
19
Compute the Value of a Node
  • xslvalue-of takes the value of something and
    copies it into the output document
  • ltxsltemplate matchATOMgt ltxslvalue-of
    selectNAME /gtlt/xsltemplategt
  • select is relative to the current node
  • The value of a node is always a string
  • Value of an element node concatenation of all
    the parsed character data (but not markup)
    between its start and end tag

20
Matching on Attributes
  • Syntax
  • matchelement_at_attributevalue
  • matchurl_at_protocolmailto
  • lturl protocolmailtogt claven_at_cc.nctu lt/urlgt
  • lturl protocolhttpgt http//. lt/urlgt
  • matchelement_at_attribute
  • matchurl_at_protocol
  • lturl protocolhttpgt http//. lt/urlgt
  • lturlgthttp//www.w3.org lt/urlgt

21
Matching Text and Functions (I)
  • Functions restrict paths to specific elements
  • /article/sectionposition()2/title/text()
  • /article/section2/title/text()
  • count(//title)

22
Matching Text and Functions (II) -- Common XSL
functions
  • position()
  • the position of the current node in the node set
  • text()
  • The text (content) of an element
  • last()
  • the position of the last node in the current node
    set
  • count()
  • the no of nodes in the current node set
  • not()
  • negates the argument
  • contains()
  • return true if the first argument contains the
    second argument
  • starts-with()
  • return true if the first argument starts with the
    second argument

23
Actions of XSL Processor
  • Load the style sheet
  • Load the source document
  • Walk through the source document from root to
    leaf
  • Match the current node against a template
  • If match, generates the node in the resulting
    tree
  • Encounter ltxslapply-templatesgt
  • Move to the children of the current node and
    repeat the process

Recursive call to the Stylesheet
24
Built-in Template
  • ltxsltemplate match / gtltxslapply-template
    s /gtlt/xsltemplategt
  • ltxsltemplate match"text()_at_"gt ltxslvalue-of
    select"."/gtlt/xsltemplategt
  • ltxsltemplate match"processing-instruction()comm
    ent()"/gt

25
Creating Nodes in the Resulting Tree (I)
  • xslelement
  • creates element with a computed name
  • xslattribute
  • creates attribute with a computed value
  • xslattribute-set
  • Conveniently combines several xslattribute
  • xsltext
  • Create a text node
  • xslprocessing-instruction
  • Create a processing instruction

26
Creating Nodes in the Resulting Tree (II)
  • xslcomment
  • create a comment
  • xslcopy
  • copies the current node
  • xslvalue-of
  • computes text by extracting from the source tree
    or inserting a variable
  • xslif
  • instantiates its content if the expression if
    true
  • xslchoose
  • select elements to instantiate among possible
    alternatives
  • xslnumber
  • creates formatted number

27
Priority
  • Templates with more specific paths take
    precedence over less specific templates
  • If conflict between two templates of equivalent
    priority
  • Report an error
  • Choose the template that appears last in the
    style sheet

28
Customized View
XSL Processor
29
Where to Apply the Stylesheet
30
XSL and IE 5.0
  • Associate a style sheet with the current document
  • lt? xml-stylesheet href.. typetext/xsl ?gt
  • Changes to the stylesheet
  • Namespace Change
  • ltxslstylesheet xmlnsxslhttp//www.w3.org/TR/W
    D-xsl xmlnshttp//www.w3.org/TR/REC-html40gt
  • IE has no built-in templates
  • Must declared explicitly

31
Advanced XSLT
32
Declaring HTML Entities in a Style Sheet
  • lt!DOCTYPE xslstylesheet lt!ENTITY copy
    0169gt gt

33
Reorganizing the Source Tree
  • ltMETA NAME"keywords"gt ltxslattribute
    name"CONTENT"gt ltxslvalue-of
    select"article/keywords"/gt, lt/xslattributegtlt/
    METAgt
  • ltMETA NAMEkeywords contentXML, XSL, style
    sheet, publishing, Webgt

34
Calling a Template
  • Template define with a name
  • ltxsltemplate name"title"gt ltxslvalue-of
    select"/article/title"/gt (
  • ltxslvalue-of select"/article/date"/gt )
  • lt/xsltemplategt
  • Call a template
  • ltxslcall-template nametitle /gt

35
Include a Style Sheet
  • ltxslinclude hrefcore.xsl /gt
  • xslinclude must be a direct child of
    xslstylesheet

36
Repetitions
  • ltxslfor-each select"article/section/title"gtltLIgt
    ltAgtltxslattribute name"HREF"gt ltxslvalue-of
    select " generate-id()"/gtlt/xslattributegtltxslv
    alue-of select"."/gtlt/Agtlt/LIgtlt/xslfor-eachgt
  • xslfor-each loop over elements
  • generate-id() Return a unique identifier for the
    current node
Write a Comment
User Comments (0)
About PowerShow.com