Title: XML Language Family Detailed Examples
1XML Language FamilyDetailed Examples
- Most information contained in these slide comes
from http//www.w3.org, http//www.zvon.org/ - These slides are intended to be used as a
tutorial on XML and related technologies - Slide authorJürgen Mangler (juergen.mangler_at_univ
ie.ac.at) - This section contains examples on
- XSLT
- (eXtensible Stylesheet Language -
Transformations)
2- With XSL you can freely modify the content and/or
layout any source text. You can apply different
Stylesheets to the same source to get different
results.
ltsourcegt lttitlegtXSLlt/titlegt ltauthorgtJohn
Smithlt/authorgt lt/sourcegt
ltxslstylesheet version '1.0'Â xmlnsxsl'http//
www.w3.org/1999/XSL/Transform'gt ltxsltemplate
match"/"gt    lth1gtltxslvalue-of
select"//title"/gtlt/h1gt      lth2gtltxslvalue-of
select"//author"/gtlt/h2gt lt/xsltemplategtlt/xsl
stylesheetgt
Output lth1gtJohn Smithlt/h1gtlth2gtXSLlt/h2gt
How can you produce the following
output? lth2gtJohn Smithlt/h2gtlth1gtXSLlt/h1gt
3- Every XSL stylesheet must start with an
xslstylesheet element. The attribute
version'1.0' specifies version of XSL(T)
specification. This example shows the simplest
possible stylesheet. As it does not contain any
information, default processing is used.
ltsourcegt ltemgtHello, worldlt/emgt lt/sourcegt
ltxslstylesheet version'1.0'Â xmlnsxsl'http//ww
w.w3.org/1999/XSL/Transform'gtlt/ xslstylesheetgt
Output Hello, world
4- An XSL processor parses an XML source and tries
to find a matching template rule. If it does,
instructions inside the matching template are
evaluated.
ltsourcegt ltboldgtHello, world.lt/boldgt ltredgtI
am lt/redgt ltitalicgtfine.lt/italicgt lt/sourcegt
ltxslstylesheet version'1.0' xmlnsxsl'http//ww
w.w3.org/1999/XSL/Transform'gt ltxsltemplate
match"bold"gt ltpgtltbgtltxslvalue-of
select"."/gtlt/bgtlt/pgt lt/xsltemplategt
ltxsltemplate match"red"gt ltp
style"colorred"gtltxslvalue-of select"."/gtlt/pgt
lt/xsltemplategt ltxsltemplate
match"italic"gt ltpgtltigtltxslvalue-of
select"."/gtlt/igtlt/pgt lt/xsltemplategtlt/xslsty
lesheetgt
Output ltpgtltbgtHello, world.lt/bgtlt/pgtltp
style"colorred"gtI am lt/pgtltpgtltigtfine.lt/igtlt/pgt
5- Contents of the original elements can be
recovered from the original sources in two basic
ways. - Stylesheet 1 uses xslvalue-of construct. In this
case the contents of the element is used without
any further processing. - The instruction xslapply-templates in Stylesheet
2 is different. The parser further processes
selected elements, for which a template is
defined.
ltsourcegt ltemployeegt    ltfirstNamegtJoelt/firs
tNamegt      ltsurnamegtSmithlt/surnamegt
lt/employeegt lt/sourcegt
For the next page we use this source
6- Stylesheet 1
- ltxslstylesheet version'1.0 xmlnsxsl'http//ww
w.w3.org/1999/XSL/Transform'gt ltxsltemplate
match"employee"gt      ltbgtltxslvalue-of
select"."/gtlt/bgt lt/xsltemplategt
ltxsltemplate match"surname"gt     Â
ltigtltxslvalue-of select"."/gtlt/igt
lt/xsltemplategtlt/xslstylesheetgt
Stylesheet 2 ltxslstylesheet version'1.0'Â xmlns
xsl'http//www.w3.org/1999/XSL/Transform'gt
ltxsltemplate match"employee"gt     Â
ltbgtltxslapply-templates select"firstName"/gtlt/bgt
     ltbgtltxslapply-templates
select"surname"/gtlt/bgt lt/xsltemplategt
ltxsltemplate match"surname"gt   Â
ltigtltxslvalue-of select"."/gtlt/igt
lt/xsltemplategtlt/xslstylesheetgt
Which Stylesheet produces which Output?
ltbgtJoelt/bgtltbgtltigtSmithlt/igtlt/bgt
ltbgtJoeSmithlt/bgt
7Parts of an XML document to which a template
should be applied are determined by location
paths. The required syntax is specified in the
XPath specification. Simple cases looks very
similar to filesystem addressing.
- ltsourcegt ltAAA id"a1" pos"start"gt      ltBBB
id"b1"/gt      ltBBB id"b2"/gt lt/AAAgt
ltAAA id"a2"gt      ltBBB id"b3"/gt      ltBBB
id"b4"/gt      ltCCC id"c1"gt          ltDDD
id"d1"/gt      lt/CCCgt lt/AAAgt lt/sourcegt
ltxslstylesheet version'1.0'
xmlnsxsl'http//www.w3.org/1999/XSL/Transform'gt
ltxsltemplate match"/source/AAA/CCC/DDD"gt
   ltp style"colorred"gt          ltxslvalue-
of select"name()"/gt          ltxsltextgt
idlt/xsltextgt          ltxslvalue-of
select"_at_id"/gt      lt/pgt lt/xsltemplategtlt/x
slstylesheetgt
Output ltp style"colorred"gtDDD idd1lt/pgt
8Processing always starts with the template
match"/". This matches the root node (the node
whose only child element is the document element,
in our case "source"). Many stylesheets do not
contain this element explicitly. When this
template is not explicitly given, the implicit
template is used (it contains as the sole
instruction). ltxsltemplate match"/"gt ltxsl
apply-templates/gt lt/xsltemplategt This
instruction means process all children of the
current node, including text nodes.
9Parses only first level under ltsourcegt
ltxslstylesheet version'1.0'
xmlnsxsl'http//www.w3.org/1999/XSL/Transform'gt
ltxsltemplate match"AAA"gt    ltdiv
style"colorpurple"gt          ltxslvalue-of
select"name()"/gt          ltxsltextgt
idlt/xsltextgt          ltxslvalue-of
select"_at_id"/gt      lt/divgt lt/xsltemplategt
ltxsltemplate match"BBB"gt      ltdiv
style"colorblue"gt          ltxslvalue-of
select"name()"/gt          ltxsltextgt
idlt/xsltextgt          ltxslvalue-of
select"_at_id"/gt      lt/divgt lt/xsltemplategt
lt/xslstylesheetgt
- ltsourcegt ltAAA id"a1" pos"start"gt      ltBBB
id"b1"/gt      ltBBB id"b2"/gt lt/AAAgt
ltAAA id"a2"gt      ltBBB id"b3"/gt      ltBBB
id"b4"/gt      ltCCC id"c1"gt          ltDDD
id"d1"/gt      lt/CCCgt lt/AAAgt lt/sourcegt
Output ltdiv style"colorpurple"gtAAA ida1lt/divgt
ltdiv style"colorpurple"gtAAA ida2lt/divgt
10Recurses into sublevels of ltAAAgt
ltxslstylesheet version'1.0'
xmlnsxsl'http//www.w3.org/1999/XSL/Transform'gt
ltxsltemplate match"AAA"gt    ltdiv
style"colorpurple"gt          ltxslvalue-of
select"name()"/gt          ltxsltextgt
idlt/xsltextgt          ltxslvalue-of
select"_at_id"/gt      lt/divgt
ltxslapply-templates/gt lt/xsltemplategt
ltxsltemplate match"BBB"gt      ltdiv
style"colorblue"gt          ltxslvalue-of
select"name()"/gt          ltxsltextgt
idlt/xsltextgt          ltxslvalue-of
select"_at_id"/gt      lt/divgt lt/xsltemplategt
lt/xslstylesheetgt
- ltsourcegt ltAAA id"a1" pos"start"gt      ltBBB
id"b1"/gt      ltBBB id"b2"/gt lt/AAAgt
ltAAA id"a2"gt      ltBBB id"b3"/gt      ltBBB
id"b4"/gt      ltCCC id"c1"gt          ltDDD
id"d1"/gt      lt/CCCgt lt/AAAgt lt/sourcegt
Output ltdiv style"colorpurple"gtAAA ida1lt/divgt
ltdiv style"colorblue"gtBBB idb1lt/divgt ltdiv
style"colorblue"gtBBB idb2lt/divgt ltdiv
style"colorpurple"gtAAA ida2lt/divgt ltdiv
style"colorblue"gtBBB idb3lt/divgt ltdiv
style"colorblue"gtBBB idb4lt/divgt
11A template can match individual paths being
separated with "" ( Stylesheet 1) from a
selection of location paths. Wildcard "" selects
all possibilities. Compare Stylesheet 1 with
Stylesheet 2.
Stylesheet 1 ltxslstylesheet version'1.0'
  xmlnsxsl'http//www.w3.org/1999/XSL/Transform
'gtltxsltemplate match"firstNamesurname"gt
ltdivgt      ltxsltextgttemplate
lt/xsltextgt           ltxslvalue-of
select"name()"/gt           ltxsltextgt outputs
lt/xsltextgt           ltxslapply-templates/gt
          ltxsltextgt lt/xsltextgt
     lt/divgt lt/xsltemplategtlt/xslstylesheetgt
ltsourcegt ltemployeegt      ltfirstNamegtJoelt/firs
tNamegt      ltsurnamegtSmithlt/surnamegt
lt/employeegt lt/sourcegt
Output ltdivgttemplate firstName outputs Joe
lt/divgt ltdivgttemplate surname outputs Smith
lt/divgt
12A template can match individual paths being
separated with "" ( Stylesheet 1) from a
selection of location paths. Wildcard "" selects
all possibilities. Compare Stylesheet 1 with
Stylesheet 2.
Stylesheet 2 ltxslstylesheet version'1.0'
  xmlnsxsl'http//www.w3.org/1999/XSL/Transform
'gtltxsltemplate match""gt ltdivgt     Â
ltxsltextgttemplate lt/xsltextgt          Â
ltxslvalue-of select"name()"/gt          Â
ltxsltextgt outputs lt/xsltextgt          Â
ltxslapply-templates/gt           ltxsltextgt
lt/xsltextgt      lt/divgt lt/xsltemplategtlt/xsl
stylesheetgt
ltsourcegt ltemployeegt      ltfirstNamegtJoelt/firs
tNamegt      ltsurnamegtSmithlt/surnamegt
lt/employeegt lt/sourcegt
Output ltdivgttemplate source outputs
ltdivgttemplate employee outputs
ltdivgttemplate firstName outputs Joe
lt/divgt     ltdivgttemplate surname outputs
Smith lt/divgtlt/divgtlt/divgt
13With modes an element can be processed multiple
times, each time producing a different result.
Stylesheet 2 ltxslstylesheet version'1.0'
  xmlnsxsl'http//www.w3.org/1999/XSL/Transform
'gtltxsltemplate match"/"gt      ltxslapply-templ
ates select"//CCC" mode"red"/gt
     ltxslapply-templates select"//CCC"/gt
lt/xsltemplategtltxsltemplate match"CCC"
mode"red"gt      ltdiv style"colorred"gt
          ltxslvalue-of select"name()"/gt
     lt/divgt lt/xsltemplategtltxsltemplate
match"CCC"gt      ltdiv style"colorpurple"gt
          ltxslvalue-of select"name()"/gt
     lt/divgt lt/xsltemplategt lt/xslstylesheetgt
ltsourcegtltAAA id"a2"gt      ltCCC id"c1"gt
          ltCCC id"c2"/gt      lt/CCCgt      ltBBB
id"b5"gt           ltCCC id"c3"/gt      lt/BBBgt
lt/AAAgtlt/sourcegt
Output ltdiv style"colorred"gtCCClt/divgtltdiv
style"colorred"gtCCClt/divgtltdiv
style"colorred"gtCCClt/divgtltdiv
style"colorpurple"gtCCClt/divgtltdiv
style"colorpurple"gtCCClt/divgtltdiv
style"colorpurple"gtCCClt/divgt
14Axes play a very important role in XSLT e.g.
child axis, for-each.
Stylesheet 2 ltxslstylesheet version'1.0'
  xmlnsxsl'http//www.w3.org/1999/XSL/Transform
'gtltxsltemplate match"/"gt      lttable
border"1" cellpadding"6"gt           ltxslfor-ea
ch select"/source/"gt                ltxslcall-t
emplate name"print"/gt           lt/xslfor-eachgt
     lt/tablegt lt/xsltemplategt ltxsltemplate
name"print"gt      lttrgt           lttdgt ltxslvalu
e-of select"./_at_id"/gt               Â
ltxsltextgt lt/xsltextgt ltxslfor-each
select"child"gt                    Â
ltxslvalue-of select"./_at_id"/gt                  Â
  ltxsltextgt lt/xsltextgt               lt/x
slfor-eachgt           lt/tdgt      lt/trgt
lt/xsltemplategt lt/xslstylesheetgt
Document ltsourcegtltAAA id"a2"gt      ltBBB
id"b4"/gt      ltCCC id"c1"/gt lt/AAAgtlt/sourcegt
Output lttable border"1" cellpadding"6"gt
lttrgt     lttdgta2 b4 c1lt/tdgt lt/trgt lt/tablegt
15xslelement generates elements in time of
processing. In this example it transforms the
sizes to formating tags.
ltxslstylesheet version'1.0' Â Â xmlnsxsl'http/
/www.w3.org/1999/XSL/Transform'gtltxsltemplate
match"/"gt      ltxslfor-each select"//text"gt
          ltxslelement name"_at_size"gt         Â
     ltxslvalue-of select"."/gt          lt/xslel
ementgt      lt/xslfor-eachgt lt/xsltemplategt lt/xs
lstylesheetgt
Output ltH1gtHeader1lt/H1gtltH3gtHeader3lt/H3gtltbgtBold
textlt/bgtltsubgtSubscriptlt/subgtltsupgtSuperscriptlt/su
pgt
ltsourcegt lttext size"H1"gtHeader1lt/textgt
lttext size"H3"gtHeader3lt/textgt lttext
size"b"gtBold textlt/textgt lttext
size"sub"gtSubscriptlt/textgt lttext
size"sup"gtSuperscriptlt/textgt lt/sourcegt
16xslif instruction enables conditional
processing. A typical case of xslfor-each usage
is to add a text between individual entries. Very
often you do not want to add text after the last
element
ltxslstylesheet version'1.0' Â Â xmlnsxsl'http/
/www.w3.org/1999/XSL/Transform'gtltxsltemplate
match"source"gt      ltxslfor-each
select"entry"gt           ltxslvalue-of
select"_at_name"/gt           ltxslif test"not
(position()last())"gt                ltxsltextgt,
lt/xsltextgt           lt/xslifgt
     lt/xslfor-eachgt lt/xsltemplategt
lt/xslstylesheetgt
ltsourcegt ltentry name"A"/gt    ltentry
name"B"/gt    ltentry name"C"/gt ltentry
name"D"/gt lt/sourcegt
Output A, B, C, D
17Numbering of individual chapter elements depends
on the position of the chapter element. Each
level of chapters is numbered independently.
Setting the attribute level to multiple enables
natural numbering.
ltxslstylesheet version'1.0' Â Â xmlnsxsl'http/
/www.w3.org/1999/XSL/Transform'gtltxsltemplate
match"/"gt      ltTABLE BORDER"1"gt
          ltxslfor-each select"//chapter"gt
               ltTRgt                    ltTDgt
                         ltxslnumber
level"multiple"/gt                          ltxsl
textgt - lt/xsltextgt
ltxslvalue-of select"./text()"/gt
                    lt/TDgt               lt/TRgt
          lt/xslfor-eachgt      lt/TABLEgt
lt/xsltemplategt lt/xslstylesheetgt
ltsourcegt ltchaptergtFirst Chap.lt/chaptergt
ltchaptergtSec. Chap. Â Â Â Â Â ltchaptergtSub
1lt/chaptergt      ltchaptergtSub 2lt/chaptergt
lt/chaptergt lt/sourcegt
What could be the possible output? Continued on
next page.
18Numbering of individual chapter elements depends
on the position of the chapter element. Each
level of chapters is numbered independently.
Setting the attribute level to multiple enables
natural numbering.
Output ltTABLE BORDER"1"gt  ltTRgt     ltTDgt1 -
First Chap.lt/TDgt  lt/TRgt  ltTRgt     ltTDgt2 - Sec.
Chap.lt/TDgt  lt/TRgt  ltTRgt     ltTDgt2.1 - Sub
1lt/TDgt  lt/TRgt  ltTRgt     ltTDgt2.2 - Sub
2lt/TDgt  lt/TRgt  lt/TABLEgt
19You can set variables in a Stylesheet and use
them later in the processing. The following
example demonstrates a way of setting
xslvariable.
ltxslstylesheet version'1.0' Â Â xmlnsxsl'http/
/www.w3.org/1999/XSL/Transform'gtltxslvariable
name"totalChapters"gt      ltxslvalue-of
select"count(//chapter)"/gt lt/xslvariablegt
ltxsltemplate match"/"gt      ltTABLEgt
          ltxslfor-each select"//chapter"gt
               ltTRgtltTDgt                    ltxsl
value-of select"."/gt                    ltxslval
ue-of select"position()"/gt                    ltx
sltextgt/lt/xsltextgt                    ltxslvalu
e-of select"totalChapters"/gt                lt/T
Dgtlt/TRgt           lt/xslfor-eachgt      lt/TABLEgt
lt/xsltemplategt lt/xslstylesheetgt
ltsourcegt ltchaptergtChapterlt/chaptergt
ltchaptergtChapterlt/chaptergt ltchaptergtChapterlt/ch
aptergt ltchaptergtChapterlt/chaptergt lt/sourcegt
What could be the possible output? Continued on
next page.
20You can set variables in a Stylesheet an use them
later in the processing. The following example
demonstrates a way of setting xslvariable.
Output ltTABLEgt  ltTRgt     ltTDgtChapter
1/4lt/TDgt  lt/TRgt  ltTRgt     ltTDgtChapter
2/4lt/TDgt  lt/TRgt  ltTRgt     ltTDgtChapter
3/4lt/TDgt  lt/TRgt  ltTRgt     ltTDgtChapter
4/4lt/TDgt  lt/TRgtlt/TABLEgt