2. XSL
• Extensible
Stylesheet
Language
(XSL)
– family
of
transforma-on
languages
• To
format
and
/
or
transform
XML
–
documents
• XSL
Family
consists
of
three
languages
– XSL
TransformaHons
(XSLT)
– XSL
FormaIng
Objects
(XSL-‐FO)
– XML
Path
Language
(XPath)
• All
languages
are
W3C
recommendaHons
3. RelaHonships
with
the
Languages
Extensible
Stylesheet
Language
XSL
TransformaHons
XSL
FormaIng
Objects
uses
XML
Path
Language
4. Languages
1. XSL
Transforma0ons
(XSLT)
– XML
language
for
transforming
XML
documents
2. XSL
Forma8ng
Objects
– XML
language
for
specifying
visual
forma8ng
3. XML
Path
Language
– A
non-‐XML
language
used
by
XSLT,
addressing
the
parts
of
an
XML
document.
Also
available
for
use
in
non-‐XSLT
contexts
5. Main
Idea
of
XSLT
XSLT
Processor
The
XSLT
Processor
can
be
client-‐
side
(browser)
or
server-‐side
(php,
java..)
7. With
XSLT
-‐
link
Now
the
XML
document
is
linked
to
an
external
xslt
document
Browser
is
used
as
an
XSLT
processor.
(client-‐
side)
8. XSLT
• XSLT
is
an
transforma0on
language.
• With
XSLT
you
can
make
transformaHons:
– XML
-‐>
XML
in
General:
• XML
-‐>
XHTML
• XML
-‐>
SVG
• ...
– XML
-‐>
HTML
– XML
-‐>
TEXT
• With
XSLT
you
can
transform
a
XML
document
to
other
text
format
(can
be
any).
9. XSL-‐FO
• XSL-‐FO
is
a
W3C
Specified
XML
language
for
defining
visual
presentaHon.
• Unified
presentaHon
language
• No
semanHc
markup
10. Example
of
an
XSL-‐FO
Document
<?xml version="1.0"?>!
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">!
<fo:layout-master-set>!
<fo:simple-page-master master-name="only">!
<fo:region-body/>!
</fo:simple-page-master>!
</fo:layout-master-set>!
<fo:page-sequence master-reference="only">!
<fo:flow flow-name="xsl-region-body">!
<fo:block font-size="20pt" font-family="serif" line-
height="30pt">!
Jussi Pohjolainen!
</fo:block>!
<fo:block color="red" font-size="30pt" font-family="serif">!
jussi.pohjolainen(at)tamk.fi!
</fo:block>!
</fo:flow>!
</fo:page-sequence>!
</fo:root>!
!
11. Transforming
XSL-‐FO
-‐
document
• Once
XSL-‐FO
document
is
generated,
it
is
passed
to
a
FO
processor.
• FO
Processor
converts
the
document
into
something
that
is
readible,
printable
or
both.
– Most
common
output
is
PDF,
PS,
RTF.
• FO
Processors?
– XEP,
XSLForma_er,
XINC
and
FOP
12. Example:
XSL-‐FO
and
Processor
XS
Processor
The
possible
output
formats
depends
of
the
features
of
the
XS
processor.
For
example,
Apache
FOP
supports
only
PDF
13. GeneraHng
FO
documents
• XSL-‐FO
Documents
are
not
meant
to
be
implemented
by
the
programmer.
• XSL-‐FO
Documents
are
generated
• Well
Who
/
What
generates
XSL
FO
Documents?
– It's
result
of
an
XSLT
–
transformaHon
14. Main
Idea
of
XSLT:
XML
-‐>
XHTML
XSLT
Processor
The
XSLT
Processor
can
be
client-‐
side
(browser)
or
server-‐side
(php,
java..)
15. Main
Idea
of
XSLT:
XML
-‐>
FO
XSLT
Processor
Now
the
books.xml
is
transformed
into
XSL
FO
language...
16. Example:
XSL-‐FO
and
Processor
XS
Processor
And
if
you
have
an
fo-‐document,
you
can
transform
it
to
pdf
(for
example)
18. Example
• ConverHng
XML
to
PDF
using
PHP
and
Apache
FOP
• Apache
FOP
– h_p://xmlgraphics.apache.org/fop/
• Must
be
installed
in
your
server
– It
is
NOT
installed
in
TAMK
Environment
22. RelaHonships
with
the
Languages
Extensible
Stylesheet
Language
XSL
TransformaHons
XSL
FormaIng
Objects
uses
XML
Path
Language
23. XPath
• Before
learning
the
basics
of
XSLT,
let's
look
at
XPath
– XPath
is
a
non-‐XML
language
that
is
used
with
XSLT.
– XPath
is
used
also
in
other
languages,
like
XPointer
• XPath
is
a
language
for
selec0ng
elements,
or
nodes,
from
an
XML
–
document
25. SelecHng
Nodes
(W3schools)
Expression
Descrip0on
nodename
Selects
all
child
nodes
of
the
named
node
/
Selects
from
the
root
node
//
Selects
nodes
in
the
document
from
the
current
node
that
match
the
selecHon
no
ma_er
where
they
are
.
Selects
the
current
node
..
Selects
the
parent
of
the
current
node
@
Selects
a_ributes
26. Examples
(W3schools)
Expression
Descrip0on
/bookstore
Selects
the
root
element
bookstore
bookstore/book
Selects
all
book
elements
that
are
children
of
bookstore
//book
Selects
all
book
elements
no
ma_er
where
they
are
in
the
document
bookstore//book
Selects
all
book
elements
that
are
descendant
of
the
bookstore
element,
no
ma_er
where
they
are
under
the
bookstore
element
//@lang
Selects
all
a_ributes
that
are
named
lang
28. Example
of
an
XSLT-‐transformaHon:
XML-‐file
<?xml version="1.0" encoding="utf-8"?>!
<?xml-stylesheet type="text/xsl"
href="transform.xslt"?>!
<book>!
<title>Programming with Java</title>!
</book>!
29. Example
of
an
XSLT-‐transformaHon:
XSLT-‐file
<?xml version="1.0"?>!
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/
XSL/Transform">!
!
<xsl:output method="text" indent="yes" encoding="utf-8"/>!
!
<xsl:template match="/">!
Title: <xsl:value-of select="/book/title"/>!
</xsl:template>!
!
</xsl:stylesheet> !
30. Result:
Simple
text
–
file..
Title: Programming with Java!
31. Making
the
TransformaHon
1. Implement
a
xml-‐document
2. Implement
a
xslt-‐document
3. Link
these
two
together
4. Output
the
result
with
some
processor
– Browser,
XML-‐editor,
PHP,
Java..
32. XSLT-‐transformaHon
with
PHP
5
<?php!
// Load the transformation file!
$xslt = new domDocument();!
$xslt->load("transformation.xslt");!
!
// Load the source xml file!
$myXML = new DomDocument();!
$myXML->load("original.xml");!
!
// Add the transformation file to XSLTProcessor!
$proc = new XSLTProcessor();!
$proc->importStylesheet($xslt);!
!
// Transform the xml and output the result!
print $proc->transformToXml($myXML);!
?>!
37. <xsl:value-‐of
select="XPath"/>
• With
<xsl:value-‐of>
you
can
select
an
element
from
the
source
file
• The
value
of
the
select
a_ribute
is
XPath
expression
– <xsl:value-‐of
select="books/book/name"/>