CDcollection { ... } CD { ... }XSL uses template rules like
<xsl:template match="CDcollection"> ... </xsl:template> <xsl:template match="CD"> ... </xsl:template>
https://www.w3.org/1999/XSL/Transform
,
usually with prefix xsl:
publisher
soloist
<xsl:template match="CD">
...
</xsl:template>
match
attribute
(e.g., CD
)
is a pattern for matching part of the input documenttemplate
element
(e.g., ...
) is a sequence of instructions
for constructing part of the output document<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="..."> ... </xsl:template> <xsl:template match="..."> ... </xsl:template> ... </xsl:stylesheet>
<CD publisher="Deutsche Grammophon" length="PT1H13M37S" > <composer>Johannes Brahms</composer> <performance> <composition>Piano Concerto No. 2</composition> <soloist>Emil Gilels</soloist> <orchestra>Berlin Philharmonic</orchestra> <conductor>Eugen Jochum</conductor> </performance> <performance> <composition>Fantasias Op. 116</composition> <soloist>Emil Gilels</soloist> </performance> </CD>
CD
element now has two attributes
CD
in example)<rss> <channel> <title> ... </title> ... <item> <title> ... </title> <description> ... </description> <link> ... </link> <pubDate> ... </pubDate> </item> ... <item> <title> ... </title> <description> ... </description> <link> ... </link> <pubDate> ... </pubDate> </item> </channel> </rss>
<xsl:template match="channel"> <html> <xsl:apply-templates select="item"/> </html> </xsl:template>
channel
elementsmatch
attribute is an
XPath expression in general (see later)<html>
and
</html>
are
instructions to construct output element using
literals<xsl:apply-templates select="item"/>
is
an instruction to apply templates to all item
children of the context nodeselect
attribute value is also an XPath
expressionmatch
are a subset of expressions
allowed in select
<xsl:template match="item"> <p> <xsl:value-of select="title"/> </p> </xsl:template>
item
elements<p>
and
</p>
are literals
constructing a result element named p
xsl:value-of
element is an instruction to output
the value of what is selected by select
attribute
valuerss-fragment.xml
rss-headlines.xsl
comprising the two previous rules<html> <p>Policewoman shot during burglary</p> <p>Lebanon marks Hariri anniversary</p> <p>MPs to vote on full smoking ban</p> </html>(see
rss-fragment-headlines.html
)xt
in the labs, you can use the batch file xt.bat
in
n:\xmltools
n:\xmltools\xt rss-fragment.xml rss-headlines.xsl rss-fragment-headlines.html
rss-fragment.xml
and
rss-headlines.xsl
as inputrss-fragment-headlines.html
as outputSaxon
in the labs, you can use the batch file saxon.bat
in
n:\SaxonHE
n:\SaxonHE\saxon
rss-fragment.xml rss-headlines.xsl rss-fragment-headlines.html
<?xml-stylesheet href="rss-headlines.xsl" type="text/xsl" ?>where the value of
href
is a URI and the value of type
is a MIME typerss-fragment-headlines.xml
(view the source to see the stylesheet processing instruction)applying the stylesheet
rss.xsl
comprising
<xsl:template match="channel"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body> <table border="1"> <xsl:apply-templates select="item"/> </table> </body> </html> </xsl:template> <xsl:template match="item"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="description"/></td> </tr> </xsl:template>
to rss-fragment.xml
yields
(rss-fragment.html
)
rss-fragment-xsl.xml
as viewed in a browser with the correct stylesheet processing instruction
<xsl:template match="channel"> <html> <xsl:for-each select="item"> <p> <xsl:value-of select="title"/> </p> </xsl:for-each> </html> </xsl:template>
xsl:for-each
selects all item
children of channel
xsl:for-each
element are applied to each item
in turntitle
selects child elements of
item
named title
match
attribute of
xsl:template
elementselect
attribute of
xsl:apply-templates
,
xsl:value-of
and
xsl:for-each
elementschannel
channel/titleselects all
title
children of
channel
children of the current
context node..
.
/
//titleselects all
title
children of descendants
of the root (including itself)/
/
/item/title
<xsl:template match="item"> <xsl:value-of select="title"/> </xsl:template>context for
title
given by item
//
element-name predicates
@attribute-name predicates
element-name
means find all child elements
of each node in the context which have the given name@attribute-name
means
find the attribute node of
each node in the context which has the given namepredicates
(each enclosed in [
and ]
) filter out nodestitle
elements from RSS feed<xsl:template match="/"> <html> <body> <xsl:apply-templates select="//title"/> </body> </html> </xsl:template>
match="/"
)select
attribute causes templates to be applied
only to title
descendents of the root node<xsl:template match="channel/title"> <h1><xsl:value-of select="."/></h1> </xsl:template> <xsl:template match="image/title"/> <xsl:template match="item/title"> <p> <b><xsl:value-of select="."/></b><br /> <xsl:value-of select="../description"/> </p> </xsl:template>
title
elements
that are children of channel
elementstitle
) is selected
using .
title
elements
that are children of image
elements and
does nothing (we will see why later)title
elements
that are children of item
elementsdescription
element, which is a
sibling of the matched title
is selected using ../
rss-xpath.xsl
is
rss-fragment-xpath.xml
(rss-fragment-xpath.html
)
<xsl:template> <xsl:apply-templates/> </xsl:template>
template
with no match
attribute matches any node, but the above rule has
the lowest priorityapply-templates
with no select
attribute applies rules to all child nodes<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>
text()
matches text nodes@
matches attribute nodes*
matches any (attribute) name|
matches either of its operands
(text()
or @*
)cd.xml
/CDlist/CD
: all child CD
elements of the
CDlist
element that is the child of the root//composer
: all composer
elements that are descendants of the root//performance/composer
: all composer
child elements of performance
elements which are
descendants of the root//performance[composer]
: all performance
elements that have a composer
element as a child//CD[performance/date]
: all CD
elements
that have a performance
element as a child that has a
date
element as a child//performance[conductor][date]
: all
performance
elements that have both conductor
and date
elements as childrenand
,
or
, not
, =
, ...+
,
-
, ...last()
: returns context sizeposition()
: returns context positioncount(S)
: returns number of nodes in
Sname(S)
: returns name of first node in
Sid(S)
: returns nodes who have an
ID
-type attribute with a value in Sposition()=2
: true if node is 2nd in the
contextposition()=last()
: true if node is last in the
contextcount(//performance)
:
the number of performance
elements//performance[not(date)]
:
performance
elements that do not have a
date
element as a childCD
elements that have "Deutsche Grammophon"
as publisher
and have more than one
performance
element as child:
//CD [publisher="Deutsche Grammophon"
and count(performance) > 1]
or
//CD [publisher="Deutsche Grammophon"] [count(performance) > 1]or
//CD [count(performance) > 1] [publisher="Deutsche Grammophon"]
<xsl:choose> <xsl:when test="..."> ... </xsl:when> <xsl:otherwise> ... </xsl:otherwise> </xsl:choose> ... <xsl:if test="..."> <xsl:copy-of select="..."/> </xsl:if>
choose
for conditional processing:
when
processed if result of
test
expression is trueotherwise
processed if result of
every test
expression is falseif
element is used for conditional processing
where there is no "else" partcopy-of
copies the selected input (whole
tree rooted at node) to outputxsl:variable
element names a variable and assigns a value to itxsl:element
element allows an element to be created
with a computed namexsl:attribute
element can be used to add attributes to result
elementsxsl:text
elementxsl:comment
element is instantiated to create a comment node
in the result treexsl:sort
elements as children of
xsl:apply-templates
or xsl:for-each
elementjson-to-xml()
and xml-to-json()
,
to convert between JSON and XMLprogramme
. A
programme
has a degree
, whose value might
be "MSc", and a year
, whose value
might be "2018/2019". These elements are followed by the
results
for the programme. The results
are partitioned into distinction
, merit
,
pass
and fail
. Within each is a sequence of
name
elements, each containing the name of a person
having achieved the corresponding result
for the
programme
.
<teaches> <teaches-tuple course="IWT" lecturer="Peter Wood"/> <teaches-tuple course="CS" lecturer="Szabolcs Mikulas"/> </teaches>into one of the form:
<teaches> <teaches-tuple> <course>IWT</course> <lecturer>Peter Wood</lecturer> </teaches-tuple> <teaches-tuple> <course>CS</course> <lecturer>Szabolcs Mikulas</lecturer> </teaches-tuple> </teaches>You can assume that
teaches
is the root element, and
that the course
and lecturer
attributes
are required. Obviously your program should work for any number of
occurrences of the teaches-tuple
element.
booker.xml
. This file contains
information about winners of the Booker prize. You
should save a copy of this file in the directory where you intend to
do the exercise. You will need to look at the document in order to
see how the elements are structured.
xsl:sort
element.
This empty element is placed as the contents of an
xsl:apply-templates
element or as the first child of an
xsl:for-each
element. Attributes include
order
, with values "ascending" (the default) and
"descending", data-type
, with values "text" (the default)
and "number", and select
, to order elements
by the values of, for example, one of its child elements.
notes.xsl
xsl.html
notes.html
www.w3.org/Style/XSL/
www.w3.org/TR/xslt
hands-on-xsl.pdf
nwalsh.com/docs/tutorials/xsl/
www.zvon.org/xxl/XSLTreference/Output/
metalab.unc.edu/xml/books/bible/updates/14.html
saxon.sourceforge.net/
chris.photobooks.com/xml/default.htm
github.com/ghislainfourny/jsoniq-tutorial
XSLT is covered in Chapter 6 and 7 of [Jacobs] and in Chapter 5 of [Moller and Schwartzbach].