TUTORIAL 6
WORKING WITH XSLT
New Perspectives on XML
1
OBJECTIVES
In this chapter, you will learn:
What is XSL To create an XSLT style sheet The syntax of the XPath language To transform an XML document into HTML To sort an XML document To create elements and attributes in HTML
New Perspectives on XML 2
WORKING WITH XSL
XSL = eXtensible Stylesheet Language Allows you to make up your own styles
We will create a new style right at the end of the chapter
New Perspectives on XML
3
INTRODUCING XSLT AND XPATH
XSLT is used to transform XML content into another presentation format
Read data from an XML document and output it into another document
Turns an XML document into an HTML document on the fly
Add HTML tags
New Perspectives on XML
4
INTRODUCING XSLT AND XPATH
XPath enables a XSLT style sheet to reference elements in a XML document
XML document is a tree structure – each element is a node
References can be absolute or relative
Similar to traversing the file system in DOS with the CD command
New Perspectives on XML
5
INTRODUCING XSLT AND XPATH
Portfolio Date Stock Name Description Category Five_day Day Open Close High Low
New Perspectives on XML 6
Nodes
NODES HAVE RELATIONSHIPS
Date/time is child of Portfolio Portfolio is ancestor of Name
Portfolio Date Stock Name Description Category Five_day Day Open
Portfolio is parent of date/time
Name is descendent of Portfolio
Close
High Low
New Perspectives on XML 7
REFERENCING NODES
Portfolio Date Stock Name Description Category
Absolute Paths
Five_day
Day Open Close High Low
/ (root) /Portfolio /Portfolio/Date /Portfolio/Stock/Name /Portfolio/Stock/Five_day/ Day //Description (all occurrences of this node)
New Perspectives on XML
8
REFERENCING NODES
Portfolio Date Stock Name Description Category
Relative Paths
/ (root) child child/child .. (parent)
Five_day
Day Open Close High Low
New Perspectives on XML 9
REFERENCING ATTRIBUTES
Portfolio Date Stock Name Description Category Five_day
Symbol
/Portfolio/Stock/Name/@Symbol
Day
Open Close
High
Low New Perspectives on XML 10
XSLT STYLE SHEETS
An XSLT style sheet document is itself an XML document An XSLT style sheet document has an extension .xsl
New Perspectives on XML
11
CREATING AN XSLT STYLE SHEET
• XSLT style sheet general structure:
Content of the style sheet
xsl: is the XSL namespace. It identifies the element as an xsl command.
New Perspectives on XML
12
CREATING A TEMPLATE
Template = defines how a section of the source document should be transformed
To create a template, the syntax is:
XSLT and Literal Result Elements where node is a specific element name from the source document
New Perspectives on XML 13
CREATING A TEMPLATE
A template contains two types of content
XSLT elements send commands to the XSLT processor A literal result element is text sent to the result document, but not acted upon by the XSLT processor
New Perspectives on XML
14
CREATING THE ROOT TEMPLATE
The root template sets up the initial code for the result document
To create a root template, the syntax is:
XSLT and Literal Result Elements
New Perspectives on XML 15
CREATING THE ROOT TEMPLATE EXAMPLE
New Perspectives on XML
16
SPECIFYING THE OUTPUT METHOD
By default, the XSLT processor will render the result document as an XML file To control how the processor formats the source document, you can specify the output method using the
element
New Perspectives on XML
17
ATTRIBUTS OF THE
ELEMENT
method – output format: xml, html, text version – version of the output
New Perspectives on XML
18
TRANSFORMING A DOCUMENT
A browser with a built-in XSLT processor allows you to view the result document Alternatively, most XSLT processors provide the capability to create the result document as a separate file
New Perspectives on XML
19
VIEWING THE RESULT DOCUMENT IN A BROWSER
Internet Explorer 6.0 contains built-in XSLT processor You can view the results of the transformation by opening the XML document in the browser
New Perspectives on XML
20
INSERTING A NODE VALUE
To insert a node’s value into the result document:
where XPath identifies the node from the source document’s node tree If the node contains child elements in addition to text content, the text in those child nodes appears as well
New Perspectives on XML 21
INSERTING A NODE VALUE EXAMPLE
New Perspectives on XML
22
WORKING WITH TEMPLATES
We have already seen how to create a template:
To apply a template in the result document:
where XPath indicates the node in the document for which a template has been created. New Perspectives on XML
23
TEMPLATE EXAMPLE
New Perspectives on XML
24
Template Example
means “display the value of whatever node has been selected”. So if the XPath in xsl:apply-templates points to “name”, select=“.” will cause the data in the name element to be displayed.
New Perspectives on XML
25
BUILT-IN TEMPLATES
There is a default template for each node. The built-in template for text nodes causes their values to appear in the result document To force the built-in templates to display all data in a document
New Perspectives on XML 26
CREATING TEMPLATES
See pages 6.28-6.38
New Perspectives on XML
27
SORTING NODES
By default, nodes are processed in document order, by their appearance in the document To specify a different order:
New Perspectives on XML
28
SORTING NODES
Attributes of :
data-type indicates the type of data
order indicates the direction of the sorting (ascending or descending)
goes “inside” the xsl:applytemplates tag:
New Perspectives on XML
29
CREATING ELEMENTS AND ATTRIBUTES
To create an element (tag) in the output file, use the tag To create an attribute of an element (tag) in the output file, use the tag
New Perspectives on XML
30
CREATING ELEMENTS AND ATTRIBUTES
For example, an element in the XML document should be displayed as a hyperlink in the output HTML document
In the XML document:
www.yahoo.com Yahoo
Transform to Yahoo
New Perspectives on XML
31
CREATING ELEMENTS AND ATTRIBUTES
New Perspectives on XML
32
SUMMARY
Extensible Style sheet Language,or XSL, includes: XSLT and XPath XPath is used to reference a node Templates format sections of the XML document and transform XML data into a variety of formats
New Perspectives on XML
33
SUMMARY
Nodes can be sorted in either alphabetical or numerical order
You can insert new elements and attributes in the transformed document
New Perspectives on XML
34