Anda di halaman 1dari 113

Introduction to XSLT

Justin Tilton, Chief Executive Officer


instructional media + magic, inc.
at the
JA-SIG Conference
Destin, Florida
December 2, 2001
XSLT, XPath and uPortal The Abstract…
Looking for a methodology to quickly and effectively
create Transformations? Interested in the basics of
XSLT and Xpath, and a good way to get started? If
so, this workshop is for you! We will be discussing
the fundamental concepts of XSLT and Xpath, and
the methodologies that have emerged from months
of developing stylesheet transformations for the
uPortal 2.0 project.

We will discuss the design aspects related to


converting structured information in XML into
device-dependent markup languages such as HTML,
and WML, and the guidelines and best practices
evolving from this experience. No prior XSLT
experience is necessary.
XSLT, XPath and uPortal Overview

• Introduction
• uPortal
• Basic XPath
• Basic XSLT
• Markup: XHTML
• Cascading Style Sheets
• Tools
• The Creation Process
• Hands-on
Introduction
XSLT, XPath and uPortal Background

• Who: W3C
• What: XPath and XSLT Specs.
• When: 11/16/1999
• Why: A need arose for a
specification to define the
syntax and semantics for
transforming XML
documents.
XSLT, XPath and uPortal References

• “The” definitive
reference…
• Michael Kay
• Wrox Press Inc
• ISBN: 1861005067
XSLT, XPath and uPortal References

• Great practical
reference…
• Jeni Tennison
• Hungry Minds
• ISBN: 0764547763
XSLT, XPath and uPortal References

• Practical use of
transformations
in Java code
• Eric Burke
• O'Reilly & Assoc.
• ISBN: 0596001436
JA-SIG’s uPortal
XSLT, XPath and uPortal What’s new in 2.0?

• Abstraction of layout
• Structure/theme transformations
• Standard channel events
• Standard CSS classes
• More flexible publish/subscribe
• User profile management
• JNDI lookup service
• WebProxy channel
XSLT, XPath and uPortal Basic Architecture
XSLT, XPath and uPortal Basic Architecture

Permissions
iPlanet
authentication LDAP

User preferences

Channel registry

Other uPortal Data


Oracle
db2
mySQL
XSLT, XPath and uPortal Channel

• Elementary unit of presentation,


defined by the IChannel interface

User Interaction Channel Content


External Information (Presentation)

IChannel

Set Runtime Data () Render XML ()


XSLT, XPath and uPortal IChannel content must

• Be well-formed XML such as XHTML,


RSS, SVG, SMIL, or a SOAP message
(HTML is not well formed XML)
• Rendered by an XSL transformation
using an XSL stylesheet
XSLT, XPath and uPortal Framework Organization

User Interaction Presentation

uPortal Framework

Channel Channel Channel


XSLT, XPath and uPortal User Layout

• User Layout is an abstract structure


defining the overall content available
to the user
• userLayout is a tree structure
consisting of “folders” and
“channels”, the later always being
the leaf nodes
XSLT, XPath and uPortal User Layout

User Layout

Folder Folder Folder

Folder Channel Folder Channel Channel

Channel Channel Channel Folder Channel

Channel
XSLT, XPath and uPortal Structure Transformation
User Layout

Folder Folder Folder

Folder Channel Folder Channel Channel

Channel Channel Channel Folder Channel

Channel

User Layout

Tab Tab Tab

Column Channel Column Channel Channel

Channel Channel Channel Channel Channel


XSLT, XPath and uPortal Theme Transformation
User Layout

Tab Tab Tab


Jim Smith Financial Aid Library

Column Column Channel Channel Channel

Channel Channel Channel


Dictionary.com Bookmarks Cartoon
XSLT, XPath and uPortal Compiling the Presentation
userLayout Structure
transformation

XSLT
structuredLayout

setRuntimeData()

XSLT
Channels Theme
renderXML()
transformation

HTML, WML VoiceML...


XSLT, XPath and uPortal Content Transformation

XML

XHTML: Web Browser

XSLT
Processor

HTML: PDA

Stylesheet

WML: Cell Phone


XSLT, XPath and uPortal Flexible Layouts

• Structures
• Tab / column
• Tree / column
• Themes
• Multi-column
• Multi-row
• Skins
• Matrix, Java
XSLT, XPath and uPortal Multiple Target Devices
XSLT, XPath and uPortal Tab / Column Layout
XSLT, XPath and uPortal Tree / Column Layout
XSLT, XPath and uPortal Theme: java
XSLT, XPath and uPortal Theme: imm
XSLT, XPath and uPortal Theme: matrix
XSLT, XPath and uPortal User preferences

• Portal=rendering+customization
• User Preferences Channel
• Swappable layout and preference
management modules
• Profile management module
• Tab-column specific UP module
• Skin Selection
XSLT, XPath and uPortal User Preferences
XSLT, XPath and uPortal Publish/Subscribe

• Channel publishing document


• Channel parameters
• Default values
• Modification permissions
• Descriptions
• Publish/Subscribe steps
• Step sequence
• Instructions, help
• Flexible channel publishing
XSLT, XPath and uPortal Channel Types
XSLT, XPath and uPortal Channel Settings
XSLT, XPath and uPortal Channel Controls
XSLT, XPath and uPortal Channel Classification
Basic XPath
XSLT, XPath and uPortal Nodes and Node Trees

• When an application wants to operate


on an XML document (such as an
XSLT processor running an XSLT
stylesheet on an XML document), it
builds an internal model of what the
document looks like.
• This model is known as a document
object model or DOM.
• In XPath and XSLT, it's called a node
tree.
XSLT, XPath and uPortal Types of Nodes
• Root nodes
• The top of the node tree R
• Element nodes
• XML elements
• Attribute nodes E E E E
• XML attributes
• Text nodes A T
• Textual content in XML elements
• Comment nodes
• XML comments
• Processing instruction nodes
• XML processing instructions
• Namespace nodes
• The in-scope namespaces on an element
XSLT, XPath and uPortal Node Tree Example

<document>
<para type=“note”> content </para>
<para type=“warning”> content </para>
<para type=“warning”> content </para>
</document> document
document

para
para para
para para
para

note
note warning
warning warning
warning

content
content content
content content
content

-root-
document

-element- -element- -element-


para para para

-attribute- -text- -attribute- -text- -attribute- -text-


type:note content type:warning content type:warning content
XSLT, XPath and uPortal XPath Definition

• XPath is a language for addressing


parts of an XML document, designed
to be used by XSLT.
• Example XPath:

child::para[attribute::type='warning'][position()=2]

• In English:
• select the second para child of the
context node that has a type attribute
with a value of warning.
XSLT, XPath and uPortal Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]
Context node
document

para para para


Axis

type:note type:warning type:warning


XSLT, XPath and uPortal Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]
Context node
document

para para para


Axis

type:note type:warning type:warning

Filtered
XSLT, XPath and uPortal Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]
Context node
document

para para para


Axis

type:note type:warning type:warning

Filtered Filtered
XSLT, XPath and uPortal Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]
Context node
document

para para para


Axis

type:note type:warning type:warning

Filtered Filtered Selected


XSLT, XPath and uPortal Types of XPaths

• Expressions:
Return a value, which might be a node
set that is processed or a string that is
output.
<xsl:when test="@type=‘warning’">

• Patterns:
Either match a particular node or don't
match that node.

<xsl:template match="para">
XSLT, XPath and uPortal XPath Expressions

• Select Nodes C
<xsl:for-each select=“child::Z”>

Z Z

• Conditional C
<xsl:if test=“position()=2”>

1 2 3 4

• Calculation
<xsl:value-of select=“position()+4">
XSLT, XPath and uPortal Node Set Expressions

• The most common way that XSLT


uses XPaths is to select node sets.
These XPaths usually occur within a
select attribute, for example on
xsl:for–each or xsl:apply–templates,
and are known as location paths.

<xsl:for-each select=“child::para”>

<xsl:apply-templates select="paragraph"/>
XSLT, XPath and uPortal Location Paths

• The purpose of location paths is to select


node sets from a node tree.
• Location paths can be absolute or relative
• Absolute location paths
start from a known location R
such as the root node

<xsl:for-each select=“/R/N”> N X Y N

R
• Relative location paths
start from the context node.

<xsl:for-each select=“N”> N X Y C ontext


note: same as “child::N”
N Z
XSLT, XPath and uPortal Steps

• A location path is made up of a


number of steps.
• Each step takes you from a node to a
node set.
• Each step is separated from the one
before it with a “/”.
XSLT, XPath and uPortal Steps
• Every step is made up of an axis and a
node test.
• The axis specifies the
direction that the “ child :: para ”
step is taken in
• The node test specifies
the kinds of nodes that
should be collected in
that direction.
• Within a step, the axis
and the node test are
separated by a double colon “::”.
XSLT, XPath and uPortal Some Shorthand Notation
• If no axis is specified in a step, the default
axis is the child axis.
• Longhand select=“child::para/child::sentence”
• Shorthand select=“para/sentence”
• Another important axis is the attribute axis,
which takes you from the context node to
the attributes of that node.
• Longhand select=“attribute::type”
• Shorthand select=“@type”
• Another essential axis is the parent axis.
This takes you from the context node to the
parent of that node.
• Longhand select=“parent::node()/child::xyz”
• Shorthand select=“../xyz”
XSLT, XPath and uPortal Axes

• ancestor :: node()
Takes you up the tree to the ancestors of
the context node in reverse document
order.

1
XSLT, XPath and uPortal Axes

• ancestor-or-self :: node()
Takes you up the tree to the ancestors of
the context node in reverse document
order starting with the context node.

1
XSLT, XPath and uPortal Axes

• child :: node()
Takes you to the children of the context
node in document order. This is the
default axis.

1 2
XSLT, XPath and uPortal Axes

• descendant :: node()
Takes you to the descendants of the
context node. The resulting nodes are in
document order.

1 3

2 4 5 6
XSLT, XPath and uPortal Axes

• descendant-or-self :: node()
Takes you to the descendants of the
context node, starting with the context
node, in document order.

2 4

3 5 6 7
XSLT, XPath and uPortal Axes

• following :: node()
Takes you to the nodes that occur after
the context node in document order, but
that are not the context node’s
descendants.

1 4

2 3
XSLT, XPath and uPortal Axes

• following-sibling :: node()
Takes you to the siblings of the context
node that occur after the context node in
document order.

1 2
XSLT, XPath and uPortal Axes

• parent :: node()
Selects a single node – the parent of the
context node.

1
XSLT, XPath and uPortal Axes

• preceding :: node()
Takes you to the nodes that occur before
the context node in reverse document
order, excluding the context node’s
ancestors.

2 1
XSLT, XPath and uPortal Axes

• preceding-sibling :: node()
Takes you to the siblings (children of the
same parent) of the context node that
occur before it, in reverse document
order.

2 1
XSLT, XPath and uPortal Axes

• self :: node()
Takes you to the context node itself.

1
XSLT, XPath and uPortal Predicates (filters)

• Predicates are placed in square


brackets either:
• at the end of a step
select=“para[position()=3]/sentence”
• or at the end of a location path
select=“para/sentence[position()=3]”

• Predicates act as filters on node sets.


• When predicate expressions test
false, the node is filtered out.
XSLT, XPath and uPortal Predicates (filters)

• You can have any number of predicates


following each other.
select=“para[position()=3][@indent=.5]/sentence”

• The context node list for each predicate


contains the nodes that are still in the node
set after it's been filtered by the previous
predicates.
• Predicates can be used at any point in a
location path, but they only apply to the
immediately preceding step.
XSLT, XPath and uPortal Expressions - Union

• You can combine node sets by


creating a union using the “|”
operator.
• If there are any nodes that occur in
both node sets, the union only holds
one copy of them.
• You can use predicates on the result
of a union, just as you can on any
node set.
XSLT, XPath and uPortal Expressions - Operators

• Logical Operators
• and, or and not()
• Comparative Operators
• =, !=, <, <=, >, >=
• Remember to escape: “<“ to &lt;
• Mathematical Operators
• +, -, *, div, mod
XSLT, XPath and uPortal XPath Examples
select=“presidents/president/[position()=3]/name”

select=“//president[count(vicePresident)=0]/name”

select=“//president[term/@from &lt; 1800]/name”

select=“descendant::president/
[count(vicePresident)>1]/name”

select=“//president/name[child::first=‘John’]”

select=“presidents/president[(term/@from > 1800) and


(term/@from &lt; 1850)/name”
Basic XSLT
XSLT, XPath and uPortal Background

• XSLT is part of a larger initiative


within the World Wide Web
Consortium (W3C) to define a way of
presenting XML documents. This
initiative is known as XSL (Extensible
Stylesheet Language).
• XSLT is an XML vocabulary that's
used to define a transformation
between an XML document and a
result document, which might be in
XML, in HTML, or a text document.
XSLT, XPath and uPortal What is an XSLT Stylesheet?

• The XSLT processor uses a stylesheet


to transform an XML document.
• The stylesheet contains instructions
for generating a new document
based on information in the source
document.
• This can involve adding, removing, or
rearranging nodes, as well as
presenting the nodes in a new way.
XSLT, XPath and uPortal The XSL Processing sequence

source
source document
document
source tree
XML
XML parser
parser

XSL
XSL stylesheet
stylesheet

rules base

apply
apply
Result
Result file
file write
write result
result templates
templates
or stream
or stream to output
to output

result tree
XSLT, XPath and uPortal Differentiating Stylesheets

• XSLT stylesheets do not perform the


same function as Cascading Style
Sheets (CSS)
• CSS is used to apply style elements
to markup languages to affect
formatting in a single pass, top
down, fashion.
• XSLT produces a separate result tree
and can iterate and perform
conditional logic.
• XSLT and CSS are most powerful
when used together. More later…
XSLT, XPath and uPortal Example of a Stylesheet

• When you work with a stylesheet,


three documents are involved:
• Source document in XML
• Desired output: the result document,
which can be HTML, XML, or text
• XSLT stylesheet, which is also an XML
document
XSLT, XPath and uPortal “Hello World!”
XML Document: root
root
<?xml version="1.0"?>
<greeting>Hello, World!</greeting>
Desired Output:
<html> greeting
greeting
<head><title>Greeting</title></head>
<body><p>Hello, World!</p></body>
</html>
Hello
Hello World!
World!
XSLT Stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Greeting</title></head>
<body><p><xsl:value-of select="greeting"/></p></body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT, XPath and uPortal Dissecting the XSLT

• XSLT stylesheets are XML Documents


<?xml version="1.0"?>

• Standard XSLT heading/Namespace


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

• The template rule to be triggered


when a particular part of the source
document is being processed. “/” is
XPath for “root node”
<xsl:template match="/">
XSLT, XPath and uPortal Dissecting the XSLT

• Once the rule is triggered the body of the


template defines what output to generate.
<html>
<head><title>Greeting</title></head>
<body><p><xsl:value-of select="greeting"/></p></body>
</html>

• Most of the template is HTML except the


value-of element which is an XSL instruction.
• The XPath in the select root
root
statement is asking for
the contents of the child greeting
greeting
of the context node with
an element name of greeting.
Hello
Hello World!
World!
XSLT, XPath and uPortal Dissecting the XSLT

• All that’s left to do is close (finish)


the template and the stylesheet.
</xsl:template>
</xsl:stylesheet>
XSLT, XPath and uPortal Some XSL Top Level Elements

• <xsl:decimal-format>
• Declares a decimal format.
• <xsl:import>
• Imports another stylesheet into this stylesheet.
• <xsl:key>
• provides a way to work with documents that
contain an implicit cross-reference structure
• <xsl:output>
• This instruction specifies how you want the result
tree to be output
XSLT, XPath and uPortal Some XSL Top Level Elements
• <xsl:param>
• Declares a parameter for a stylesheet or
template, and specifies a default value for the
parameter
• <xsl:template>
• Specifies a template rule.
• <xsl:variable>
• Declares a variable and binds a value to that
variable.
• The difference between the xsl:param and
xsl:variable instructions is that xsl:param defines
a default value while xsl:variable defines a fixed
value.
• If used as a top level element the scope is global
otherwise the scope is local to a specific template
XSLT, XPath and uPortal What Is a Template?

• A template defines what the XSLT


processor should do when it processes a
particular node in the XML source
document.
• The XSLT processor populates the result
document by instantiating a sequence of
templates.
• Instantiation of a template means that the
XSLT processor
• Copies any literal data from the template to the
result document
• Executes the XSLT instructions in the template
XSLT, XPath and uPortal Contents of a Template

• To define a template, you specify the


xsl:template instruction.
• In the xsl:template tag, the value of
the match attribute is an XPath
pattern.
• This pattern matches (identifies)
nodes in the source XML document.
• The value of the match attribute is
the template rule.
XSLT, XPath and uPortal The Template Body

• The template body defines actions


you want the XSLT processor to
perform each time it instantiates the
template. It contains
• XSLT instructions you want the XSLT
processor to follow
• Elements that specify literal output you
want the XSLT processor to insert in the
result document. For example:

<table align="center" cellpadding="5">


XSLT, XPath and uPortal Determining Templates to Instantiate

• When the XSLT processor applies a


stylesheet to an XML document, it
begins processing with the root node
of the XML source document
• Every stylesheet includes default
templates
• Whether or not you explicitly define a
template rule that matches the root
node, the XSLT processor always
instantiates a template that matches
the root node.
XSLT, XPath and uPortal The Example Handout
XSLT, XPath and uPortal Dissected XSLT Stylesheet
XSLT, XPath and uPortal Dissecting a sample

• In the sample stylesheet, the


template rule in the first template
matches the root node:
<xsl:template match="/">

• The XSLT processor instantiates this


template to start generating the
result document.
• It copies the first few lines from the
template to the result document.
XSLT, XPath and uPortal Dissecting a sample

• Then the XSLT processor reaches the


following XSLT instruction:
<xsl:apply-templates select="/bookstore/book"/>

• When the XSLT processor reaches the select


attribute, it creates a list of all source
nodes that match the specified pattern.
• In this example, the list contains book
elements.
• The processor then processes each node in
the list in turn by instantiating its matching
template.
XSLT, XPath and uPortal Dissecting a sample

• First, the XSLT processor searches


for a template that matches the first
book element. The template rule in
the second template matches the
book element:
<xsl:template match="book">

• After instantiating this template for


the first book element, the XSLT
processor searches for a template
that matches the second book
element…
XSLT, XPath and uPortal Dissecting a sample

• The XSLT processor instantiates the


book template again, and then
repeats the process for the third
book element.
• After three instantiations of the book
template, the XSLT processor returns
to the first template (the template
that matches the root node) and
continues with the line after the
xsl:apply-templates instruction.
XSLT, XPath and uPortal Built-in Templates

• When the XSLT processor cannot find


a template that matches a selected
node, it uses built-in templates.
Every stylesheet includes built-in
templates whether or not you
explicitly define them.
XSLT, XPath and uPortal Built-in Templates
• The following template matches the root
node and element nodes and selects all
attributes and child nodes for further
processing:
<xsl:template match="*|/">
<xsl:apply-templates / >
</xsl:template>
• The following template matches text and
attribute nodes. This template copies the
value of the text or attribute node to the
result document:
<xsl:template match="@*|text()">
<xsl:value-of select="." / >
</xsl:template>
XSLT, XPath and uPortal Major XSL Instructions
• xsl:apply-imports • xsl:message
• xsl:apply-templates • xsl:namespace-alias
• xsl:attribute • xsl:number
• xsl:attribute-set • xsl:otherwise
• xsl:call-template • xsl:output
• xsl:choose • xsl:param
• xsl:comment • xsl:preserve-space
• xsl:copy • xsl:processing-instruction
• xsl:copy-of • xsl:sort
• xsl:decimal-format • xsl:strip-space
• xsl:element • xsl:stylesheet
• xsl:fallback • xsl:template
• xsl:for-each • xsl:text
• xsl:if • xsl:transform
• xsl:import • xsl:value-of
• xsl:include • xsl:variable
• xsl:key • xsl:when
• xsl:with-param
XHTML
XSLT, XPath and uPortal Why XHTML?

• XSLT Stylesheets are XML documents


• If you plan to output HTML, it will
reside in the template bodies of the
XSLT Stylesheet, and the markup will
be output as literal output.
• When the XSLT Stylesheet contains
Markup, it has to be well formed
XSLT, XPath and uPortal Differences with HTML 4
• Due to the fact that XHTML is an XML
application, certain practices that were
perfectly legal in HTML 4 must be changed.
• Documents must be well-formed
• Well-formedness is a new concept introduced by
[XML]. Essentially this means that all elements
must either have closing tags or be written in a
special form (as described below), and that all
the elements must nest.

• CORRECT: nested elements.


<p>here is an emphasized <em>paragraph</em>.</p>
• INCORRECT: overlapping elements
<p>here is an emphasized <em>paragraph.</p></em>
XSLT, XPath and uPortal Differences with HTML 4

• Element and attribute names must


be in lower case
• XHTML documents must use lower case
for all HTML element and attribute
names. This difference is necessary
because XML is case-sensitive e.g. <li>
and <LI> are different tags.
XSLT, XPath and uPortal Differences with HTML 4

• In HTML 4 certain elements were permitted


to omit the end tag; with the elements that
followed implying closure. This omission is
not permitted in XML-based XHTML. All
elements must have an end tag.

• CORRECT: terminated elements


<p>here is a paragraph.</p>
<p>here is another paragraph.</p>
• INCORRECT: unterminated elements
<p>here is a paragraph.
<p>here is another paragraph.
XSLT, XPath and uPortal Differences with HTML 4

• All attribute values must be quoted,


even those which appear to be
numeric.

• CORRECT: quoted attribute values


<table rows="3">
• INCORRECT: unquoted attribute values
<table rows=3>
XSLT, XPath and uPortal Differences with HTML 4

• XML does not support attribute


minimization. Attribute-value pairs
must be written in full. Attribute
names such as nowrap and checked
cannot occur in elements without
their value being specified.

• CORRECT: unminimized attributes


<td nowrap=“nowrap">
• INCORRECT: minimized attributes
<td nowrap>
XSLT, XPath and uPortal Differences with HTML 4

• Empty elements must either have an


end tag or the start tag must end
with />. For instance, <br/> or
<hr></hr>.

• CORRECT: terminated empty tags


<br/><hr/>
• INCORRECT: unterminated empty tags
<br><hr>
Cascading Style Sheets
XSLT, XPath and uPortal CSS uPortal and Skins

• uPortal uses Cascading Style Sheets


to “Skin” the portal and content.
• These CSS files are optimized for a
particular structure.
• For consistency channel developers
should become familiar with the CSS
classes that are defined for a
particular structure and apply them
to the markup language in their XSLT
transformations.
XSLT, XPath and uPortal The CSS Classes

• There is a sample “developer”


channel in uPortal that describes and
gives examples of the CSS classes for
the Tab/Column Theme.
XSLT, XPath and uPortal The CSS Channel
The Tools
XSLT, XPath and uPortal The Applications…
These are the applications I am familiar
with -- this is not an endorsement ☺ --
and no testing was done on animals…

XML Document Development IDE:


• Excelon Stylus Studio
• XML Spy
• Cooktop (open source)
HTML Markup IDE:
• Macromedia Dreamweaver
• Adobe GoLive
HTML to XHTML Conversion/Cleanup
• HTML Tidy (open source)
XSLT Development IDE
• Excelon Stylus Studio
• XML Spy
• Cooktop (open source)
The Creation Process
XSLT, XPath and uPortal Creating an XSLT Stylesheet
Step One – get or develop one of the following:
• A representative XML Document
• A DTD (a whole different seminar)
• A Schema (a whole different seminar)
Step two – Analyze the data
• Think about the best way to present and interact with
it. The data is your friend.
Step three – Develop sample markup
• Design mocks for each markup language & device that
you plan to support. Don’t try to paint the canvas
without a sketch.
Step four – Convert the markup to XML
• The stylesheet will only eat tasty markup. Poorly
formed markup will be regurgitated.
Step five – copy markup into XSLT editor
• Match on a root element and start cooking XSLT.
Hands - on
XSLT, XPath and uPortal Hands – on

• Convert RSS (Rich Site Summary)


“Syndicated Content” XML
Documents into HTML using an XSLT
Stylesheet.
Source XML XSLT Stylesheet

<?xml version="1.0" ?>


<?xml version="1.0" ?> <xsl:stylesheet version="1.0"
<bookstore> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<book> <xsl:output method="html" />
<author>W. Shakespeare</author>
<title>Hamlet</title> <xsl:template match="/">
<published>1997</published> <html><head><title>Stylesheet Example</title></head>
<price>2.95</price> <body>
</book> <table align="Center" cellpadding="5">
<book> <tr>
<author>W. Shakespeare</author> <th>Title</th>
<title>Macbeth</title> <th>Author</th>
<published>1989</published> <th>Price</th>
<price>9.95</price> </tr>
</book> <xsl:apply-templates select="/bookstore/book">
<book> <xsl:sort select="author" />
<author>D. Alighieri</author> </xsl:apply-templates>
<title>The Divine Comedy</title> </table>
<published>1321</published> </body>
<price>5.95</price> </html>
</book> </xsl:template>
</bookstore>
<xsl:template match="book">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="author" /></td>
<td align="right"><xsl:value-of select="price" /></td>
</tr>
</xsl:template>

</xsl:stylesheet>
Dissected XSLT Stylesheet
Standard XML Namespace declaration for W3C XSLT
declaration namespace.

<?xml version="1.0" ?>


xsl:stylesheet is an
XSLT instruction. <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
xsl:template is an xsl:output is an XSLT instruction. In this
XSLT instruction. It <xsl:template match="/"> stylesheet, it specifies that the result
contains literal data <html><head><title>Stylesheet Example</title></head> document will be in HTML format.
to be copied to the <body>
result document and <table align="Center" cellpadding="5">
XSLT instructions to <tr>
be followed by the <th>Title</th> xsl:apply-templates is an XSLT
XSLT processor. The <th>Author</th> instruction. For each node identified by
processor performs <th>Price</th> this instruction’s select attribute, the
these steps for the </tr> XSLT processor goes to another
source nodes <xsl:apply-templates select="/bookstore/book"> template in this stylesheet, and
identified by the <xsl:sort select="author" /> performs the actions defined in that
match attribute </xsl:apply-templates> template. When done, the processor
value. In this </table> returns here, and moves to the next line
template, the match in this template. In this template, the
</body>
attribute identifies select attribute identifies all book
</html>
the root node of the elements in the source document.
source document. </xsl:template>

<xsl:template match="book">
This template <tr> xsl:sort is an XSLT instruction. The XSLT
matches book <td><xsl:value-of select="title" /></td> processor processes the book nodes in
elements in the <td><xsl:value-of select="author" /></td> alphabetical order by author.
source document. In <td align="right">
this stylesheet, the <xsl:value-of select="price" />
XSLT processor </td>
performs the actions </tr>
in this template xsl:value-of is an XSLT instruction. The
</xsl:template>
three times, once for XSLT processor extracts the contents of
each book element the source node specified in the select
</xsl:stylesheet> attribute and copies it into the result
in the source
document. document.
HTML Output Browser Representation

<html>
<head>
<META http-equiv="Content-Type" content="text/html">
<title>Stylesheet Example</title>
</head>
<body>
<table align="Center" cellpadding="5">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
</tr>
<tr>
<td>The Divine Comedy</td>
<td>D. Alighieri</td>
<td align="right">5.95</td>
</tr>
<tr>
<td>Hamlet</td>
<td>W. Shakespeare</td>
<td align="right">2.95</td>
</tr>
<tr>
<td>Macbeth</td>
<td>W. Shakespeare</td>
<td align="right">9.95</td>
</tr>
</table>
</body>
</html>

Anda mungkin juga menyukai