Title: Presenting XML
1Presenting XML
- Need some way to display XML
- Default is to show the tags
- Example see
- http//people.cs.uchicago.edu/dangulo/cspp53025/e
xamples/lec2/BridgeOfDeathEpisode-NoStylesheet.xml
- Bring up in IE6 / NetScape7
- Want a better display
- Convert information to presentation.
- Can use cascading style sheets to control the
appearance of tags - including making either the tags or the text
inside them invisible
2XSL
- eXtensible Stylesheet Language
- Has two parts
- XSLT
- Defines transformations from XML to HTML.
- From XML to XML
- From XML to anything!
- XSL Formatting Objects
- Provides an alternative to CSS
- Formats and styles an XML document
- w3c standard (actually recommendation) see the
w3c links off of the webpage for the definition.
3Using XSLT
XML Servers
Client Side
IE5 Mozilla .9
Web Server
CGI
XSLT-enabled Browser
http request
XML Stylesheet
The Internet
XML response
Server Side
XSLT-engine
HTML
http request
Web Server
Servlet
The Internet
HTML
XML response
4Dont underestimate XSLT
- XSLT is an extremely powerful language.
- For publishing applications, often the entire
application can be performed in XSLT. - Add prefix or suffix text to content
- Remove, create, re-order and sort elements
- Re-use elements elsewhere in the document
- Transform data between different XML dialects
- XSLT can be used like a stylesheet or embedded in
a programming language
5Executing the stylsheet foo.xslOn file
BridgeOfDeathEpisode.xml
- foo.xsl lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl "http//www.w3.org/1999/XSL/Transform
" version"1.0"gt ltxsltemplate
match/gt ltHTMLgtlttitlegtHWlt/titlegtltBODYgtHELLO
WORLD lt/BODYgtlt/HTMLgt lt/xsltemplategt lt/xsls
tylesheetgt - BridgeOfDeath.html lthtmlgtlttitlegthwlt/titlegtltBODYgt
HELLO WORLD lt/BODYgt lt/htmlgt
6Running XSLT
- To see the output in an XSL ready browser
- Add a processing instruction to the XML with a
reference to the stylesheet (xsl or css --- basic
syntax is the same). - BridgeOfDeath.xml
- lt?xml version"1.0" encoding"UTF-8"?gt
- lt?xml-stylesheet hreffoo.xsl typetext/xsl?gt
- ltepisodegt
- ltdialog speaker"Bridgekeeper"gtStop! Who would
cross the Bridge of Death must answer me these
questions three, ere the other side he
see.lt/dialoggt -
- lt/episodegt
- For NetScape 6.0/6.2
- Make sure the mime type returned from the HTTP
server is text/xml - For both .xml and .xsl files
- NetSccape correctly looks for this, MSIE does notg
7XSL apply-templates
- ltxslapply-templates /gt continue processing my
- children by applying templates to them.
To understand this, always imagine the xslt
transformer traversing the document in a
depth-first fashion and outputting the text
it encounters until it reaches a rule that
specifies it do otherwise. When a rule is reached
for a particular node in the xml file,
whatever is specified in the rule is carried out,
and then the transformer would go on the the next
node unless there is an embedded
ltxslapply-templatesgt command
8xsltemplate
- The template is the basis of the rule-based
architecture that XSLT uses. - You define the template to produce an outcome, so
whenever you call that template (using
xslapply-templates or xslcall-template), it
handles this output. - The template is processed either by its match or
name attributes. - The match attribute uses a pattern,
- The name is whatever name you give it.
- Another important attribute of the template is
the mode attribute. If you want different
formatting from the general template that you
have defined, you can define different modes for
that node, which will ignore the general match
instruction. - Note You need to have at least one template in
your XSLT file.
9Xslapply-template
- The apply-template element is always found inside
a template body. - It defines a set of nodes to be processed. In
this process, it then may find any 'sub' template
rules to process (child elements of element in
context). - If you want the apply-template instruction to
only process certain child elements, you can
define a select attribute, to only process
specific nodes. In the following example, we
only want to process the 'name' and 'address'
elements in the root document element - ltxsltemplate match"/"gt ltxslapply-templates
select"person/name person/address"/gtlt/xsltemp
lategt
10XSL apply-templates
- foo2.xsl
- lt?xml version"1.0"?gt
- ltxslstylesheet
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
- version"1.0"gt
- ltxsltemplate match/gt
- ltHTMLgtltBODYgt ltxslapply-templates /gt
lt/BODYgtlt/HTMLgt - lt/xsltemplategt
- ltxsltemplate matchepisodegt
- ltPgtltIgtI have hit the top-level episode
elementlt/Igtlt/Pgt - ltxslapply-templates /gt
- lt/xsltemplategt
- ltxsltemplate matchdialoggt
- ltPgtI have hit some dialoglt/Pgt
- lt/xsltemplategt
- ltxsltemplate matchactiongt
At the beginning output these tags and leave . .
a hole to wait for the stuff you put in between
them. Fill that hole by matching .
further templates against subelements of the
root.
All xsl commands start with xsl
An XSL stylesheet has to be well-formed XML
11XSLvalue-of
- ltxslvalue-of selectnodeinfogt output a string
that is derived from the node represented by
nodeinfo. - ltxslvalue-of select.gt output the value of
this node for an element, this will be cdata
inside of it. - ltxslvalue-of select_at_speakergt output the
value of the speaker attribute.
12XSLvalue-of
- foo3.xsl
- lt?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt - ltxsltemplate match"/"gt
- ltHTMLgtltBODYgt ltxslapply-templates /gt
lt/BODYgtlt/HTMLgt - lt/xsltemplategt
- ltxsltemplate match"dialog"gt
- ltPgt
- ltxslvalue-of select"_at_speaker /gt
- ltIgt ltxslvalue-of select". /gt lt/Igt
- lt/Pgt
- lt/xsltemplategt
- ltxsltemplate match"action"gt
- ltPgt ltBgt ltxslvalue-of select"."/gt lt/Bgt lt/Pgt
- lt/xsltemplategt
- lt/xslstylesheetgt
13XSL Document Structure
lt?xml version1.0?gt ltxslstylesheetgt
ltxsltemplate match/gt
action lt/xsltemplategt
ltxsltemplate matchDialoggt
action lt/xsltemplategt
ltxsltemplate matchActiongt
action lt/xsltemplategt
... lt/xslstylesheetgt
14Select Attributes and Xpath
- The apply-templates tag takes a Select attribute,
as do several other tags (e.g. for-each). - Every Select attribute will takes as a value an
Xpath expression - Xpath expressions
- Find nodes (elements or attributes in the input
document). - Can select specific children
- ltxsltemplate matchEpisode"gt
ltxslapply-templates selectDialog_at_speake
rKing Arthur'"/gtlt/xsltemplategt - By default, the pattern is always applied
starting at the node matching the template (i.e.
we work down the tree)
15XSLT Outline
- Language for transforming XML to XML
- Components of the XSLT Language
- Control structures
- ltxslapply-templates selectexpressiongt
- ltxslchoose gt
- Commands for creating new tags in the output
- ltxslvalue-of selectexpression/gt
- Xpath expression language for selecting
attributes and elements - Miscellaneous other stuff
16Control Structures Conditionals
- Two basic conditional types.
- First is ltxslchoosegt which is like a switch
- ltxslchoose gt
- ltxslwhen testexpressiongt
-
- lt/xslwhengt
- ltxslotherwisegt
-
- lt/xslotherwisegt
- lt/xslchoosegt
- Theres also a simpler form ltxslifgt
- ltxslif testexpressiongt
- Stuff
- lt/xslifgt
- ltxslif test_at_salary gt 200000/gt
- High-Salaried Employee!
- lt/xslifgt
17xslapply-templates
- With no SELECT block, just says apply any
templates to my children continue moving down the
tree. - What if I say to apply templates to some element,
and that element has no template? - lt?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt - ltxsltemplate match/gt
- ltHTMLgtltBODYgt ltxslapply-templates /gt
lt/BODYgtlt/HTMLgt - lt/xsltemplategt
- ltxsltemplate matchepisodegt
- ltPgtltIgt Ive hit the top-level episode element
lt/Igtlt/Pgt - ltxslapply-templates /gt
- lt/xsltemplategt
- lt!-- Forgot the dialog template --gt
- ltxsltemplate matchactiongt
- ltH2gt Ive hit some action lt/H2gt
- lt/xsltemplategt
18xslapply-templates
- If there is no SELECT block, it means to apply
any templates to the children (continue moving
down the tree.) - What if I say to apply templates to some element,
but that element has no template? - Apply the default template see the example on
the web for details.
19Priority
- Suppose more than one template matches?
- XSLT has a way of assigning priorities to
templates - It picks the highest-priority match.
- The most specific expression has highest
priority. - ltxsltemplate matchArticlegt
- Special treatment for article here!
- lt/xsltemplategt
- ltxsltemplate matchgt
- Heres stuff we do for anything that isnt
special - lt/xsltemplategt
20ltxslapply-templatesgt
- Select doesnt have to move down the tree.
- lt?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt - ltxsltemplate match/gt
- ltHTMLgtltBODYgt
- ltxslapply-templates selectHTML/BODY /gt
- lt/BODYgtlt/HTMLgt
- lt/xsltemplategt
- ltxsltemplate matchBODYgt
- This is the text that is part of the document
- ltxslapply-templates select/HTML/HEAD /gt.
- lt/xsltemplategt
- ltxsltemplate matchHEADgt
- ltfont colorredgt
- ltxslvalue-of selectTITLEgt
- lt/fontgt
- lt/xsltemplategt
Jumps back to the HEAD element.
21vNamed Templates
- Can also give a template a name
- ltxsltemplate nameheader gt
- ltH1gt
- The average number of wingbeats per second of a
laden African Swallow is 5 - lt/H1gt
- lt/xsltemplategt
- One can then call a template by name
- ltxsltemplate match/gt
- ltHTMLgt
- ltBODYgt
- ltxslcall-template nameheader gt
- lt/xslcall-templategt
- lt/BODYgt
- lt/HTMLgt
- lt/xsltemplategt
- Important for reusing code!
22Parameters
- Think of templates as being equivalent to
functions. - Thus, it makes sense to be able to pass arguments
to templates. - XSL has a verbose way of doing this
- ltxsltemplate nameemphasizegt
- ltxslparam nametext /gt
- ltxslparam namecolor /gt
- ltfont colorcolorgt
- ltxslvalue-of selecttext /gt
- lt/fontgt
- lt/xsltemplategt
- ltxsltemplate matchTITLEgt
- ltxslcall-template nameemphasizegt
- ltxslwith-param nametext select. /gt
- ltxslwith-param namecolor selectred /gt
- lt/xslcall-templategt
- lt/xsltemplategt
Function Emphasize(text,color)
Emphasize(.,red)
23Parameters
- Parameters are untyped they could contain
strings, numbers, booleans, or nodes of the tree. - Organization.xml
- ltorganizationgt
- ltPREZ fnameDaria/gt
- ltVP fnameQuinn/gt
- lt/organizationgt
- Organization.xsl
- ltxsltemplate nameprocesspersongt
- ltxslparam nameperson/gt
- First Name ltxslvalue-of selectperson/_at_fname
/gt - lt/xsltemplategt
- ltxsltemplate matchorganizationgt
- ltxslcall-template nameprocesspersongt
- ltxslwith-param nameperson selectPREZ /gt
- lt/xslcall-templategt
- ltxslcall-template nameprocesspersongt
- ltxslwith-param nameperson selectVP /gt
24Global Parameters
- If an ltxslparam namemyparam/gt occurs at the
beginning of the stylesheet (before any
templates) then myparam is a global parameter. - A value for myparam should be given on the
command line - In Xalan
- java org.apache.xalan.xslt.Process -IN
episode.xml XSL global.xsl PARAM color red
OUT globalout.html
25Variable Element
- Variables are declared and initialized with an
ltxslvariablegt statement. Two ways to give the
value - ltxslvariable nameCE"gtCarl Everettlt/xslvariab
legt - ltxslvariable namepresident
selectorganization/PREZ/gt - A variable is referenced with varname, like
parameters - ltxslvalue-of selectCE"/gt
- ltxslvalue-of selectpresident/_at_fname/gt
- ltxslapply-templates select"PRESIDENT/gt
- Once the value of the variable is set, it cannot
change - Can only be used (dereferenced) in attribute
value templates - The values of variables declared in a template
are not visible outside of the template. - ltxsltemplate match/gt
- ltxslvariable nameCE"gtCarl
Everettlt/xslvariablegt - ltxslapply-templates selectPREZ/gt
- ltxslvalue-of selectCE/gt
- lt! Carl Everett will print --gt
- lt/xsltemplategt
- ltxsltemplate matchPREZgt
- ltxslvariable nameCE"gtChris
Eigemanlt/xslvariablegt
26Loops
- ltxslfor-each selectexpressiongt
- Block
- lt/xslfor-eachgt
- expression must return some set of nodes in the
input. Will insert the output of Block into the
result - Similar to ltxslcall-templategt, but with no name
for the template.
27Controlling Order
- Both xslfor-each and xslapply-templates have as
default order - The order in which the matching elements occur
within the document. - You can change that order using ltxslsort gt
- ltxslfor-each selectstudentgt
- ltxslsort selectstudidgt
- lt/xslsortgt
- ......
- lt/xslfor-eachgt
- Analagous to SQL order by clause.
- ltxslsort selectstudid orderdescending/gt
28Mode
- Another way of passing an argument to a template
is to stick it in a mode attribute - ltxsltemplate matchHeadgt
- ltxslapply-templates modered /gt
- lt/xsltemplategt
- ltxsltemplate matchBodygt
- ltxslapply-templates modeblue /gt
- lt/xsltemplategt
- ltxsltemplate match modeblue gt
- ltfont colorbluegtltxslvalue-of select.
/gt lt/fontgt - ltxslapply-templates modeblue /gt
- lt/xsltemplategt
- ltxsltemplate match modered gt
- ltfont colorredgt ltxslvalue-of select.
/gtlt/fontgt - ltxslapply-templates modeblue /gt
- lt/xsltemplategt
Useful to control priority between two templates
that match the same tag.
29XSLT Outline
- Language for transforming XML to XML
- Components
- Control structures
- Commands for creating new tags in the output
- Xpath expression language
- Miscellaneous other stuff
30Creating New Elements
- We saw that we can create a fixed tag by just
writing it inside the template. - ltxsltemplates match/gt
- ltHTMLgt ltBODYgt Bonjour Monde lt/BODYgt lt/HTMLgt
- ltxslapply-templatesgt
- We also know how to create dynamic text content
using - ltxslvalue-ofgt
- Suppose the values of attributes in a tag to
depend on stuff in the input document - ltA href article/url gt Go to article lt/Agt
- The bracket says that the stuff inside is to be
evaluated, not written out literally.
31Creating New Elements
- Suppose the name of the attribute depends on the
document? - ltxslattribute namesomeexpression gt
- lt! Whatever is computed here is the value
--gt - lt/xslattributegt
- ltxslattribute namemyname gt
- ltxslvalue-of selectmyvalue /gt
- lt/xslattributegt
- Can do the same thing to create a new element
- ltxslelement namesomethinggt
- ltxslattribute namemyattnamegt
-
- lt/xslattributegt
- lt/xslelementgt
32Copying nodes
- Sometimes you just want to simply copy part of
the input document - to the output.
- You can do this quickly with ltxslcopygt and
ltxslcopy-ofgt - ltxsltemplate matchULgt
- ltxslcopy-of select. /gt
- lt!-- this will copy the OL tag and all of its
subelements verbatim--gt - lt/xsltemplategt
- ltxsltemplate matchOLgt
- ltxslcopy /gt
- lt!-- this will copy the OL tag but leave all of
its subelements out --gt - lt/xsltemplategt
- Note ltxslcopy /gt copies the current node
(although there is a way to add stuff inside
xslcopy to get different behavior). But - ltxslcopy-of gt expects a selectexpression
argument, and copies the result of expression.
33XSLT Outline
- Language for transforming XML to XML
- Components
- Control structures
- Commands for creating new tags in the output
- Xpath expression language
- Overview
- Node-sets, axes, and predicates
- Node-set operators and functions
- Built-in Functions
- Multiple Documents
- Match expressions
- Miscellaneous other stuff
- Namespaces
- XML-schema
34XPath
- A language for identifying stuff within a
document. - Xpath expressions can return
- Bunch of stuff within the document a nodeset
- True/false, Number(s), String(s)
- Used by other XML specifications
- XPointer, XQL, XSLT.
- In XSLT, it is used
- in select attribute values
- in other places (e.g. the test attribute of an
xslwhen and xslif) - Examples
- ltxslapply-templates selectCHAPTERgt
- ltxslvalue-of selectchapter/_at_numbergt
- ltxslvalue-of selectname(..)gt
35Navigating the Tree
- Simple Xpath expressions consist of a navigation
path. - ltxslapply-templates selectCHAPTER/VERSEgt
- ltxslfor-each selectCHAPTER/VERSE/LINEgt
- When used in XSLT, this XPATH expression
- takes as input the current node
- the node that we are processing at that point in
the XSLT transformation - returns the set of elements that are VERSE
subelements of CHAPTER subelements of the node. - The above are examples of relative paths. One can
also have an absolute path. - ltxslapply-templates select/CHAPTER/VERSEgt
- Rough idea of navigation pathssimilar to Unix
path names. - ./CHAPTER any CHAPTER child of mine. (same as
CHAPTER) - .//CHAPTER any CHAPTER child of some child of
mine. - ../CHAPTER any CHAPTER child of my parent.
36Navigation Paths
- On the other hand..bet you cant do this in Unix
(but can in Xpath) - .//CHAPTER any CHAPTER descendant
of the current node. (be careful this is
powerful but potentially inefficient!) - /CHAPTER//VERSE any VERSE that is a
descendant of some CHAPTER child of the root
node. - But XPATH allows you to select not just element
nodes of the input, but attributes also. - ltxslfor-each selectepisode/dialog/_at_speakergt
- ltxslvalue-of select//comment() /gt
- lt!- Will match every comment in the document
- So, puts the content of every comment each
- time any speaker attribute is found --gt
- lt/xslfor-eachgt
37ChangeDialogToCommentValue.xsl
- lt?xml version"1.0" encoding"UTF-8"?gt
- ltxslstylesheet version"1.0" xmlnsxslhttp//www
.w3.org/1999/XSL/Transform xmlnsfo"http//www.w3
.org/1999/XSL/Format"gt - ltxsltemplate match"/"gt
- ltHTMLgtltBODYgt
- ltxslfor-each select"episode/dialog
/_at_speaker"gt - ltxslvalue-of select"//comment()"
/gt - lt!-- Will match every comment in
the document --gt - lt/xslfor-eachgt
- lt/BODYgtlt/HTMLgt
- lt/xsltemplategt
- lt/xslstylesheetgt
38Dialog to Comment
- BridgeOfDeathEpisodeDialogToComment.xml
- lt?xml version"1.0" encoding"UTF-8"?gt
- lt!-- BridgeOfDeathEpisodeDialogToComment.xml --gt
- lt?xml-stylesheet hrefChangeDialogToComment.xsl
typetext/xsl?gt - ltepisodegt
- ltdialog speaker"Bridgekeeper"gtStop! Who would
cross the Bridge of Death must answer me these
questions three, ere the other side he
see.lt/dialoggt - ltdialog speaker"Sir Launselot"gtAsk me the
questions, bridgekeeper. I am not
afraid.lt/dialoggt -
- Output
- ltHTML xmlnsfo"http//www.w3.org/1999/XSL/Format"
gtltBODYgt BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
BridgeOfDeathEpisodeDialogToComment.xml
lt/BODYgtlt/HTMLgt
39Abbreviate
- Many XPath expressions are actually abbreviations
- ./CHAPTER is an abbreviation for childCHAPTER
- DIALOG/_at_speaker abbreviates childDIALOG/attribut
espeaker - ./_at_speaker abbreviates attributespeaker
- We will generally use the short name whenever
possible.
40The Full Tree
/
Think of an Xpath expression as working on a tree
that shows all the information in the
XML document (this tree is called the InfoSet of
the document)
dialog
action
speaker BridgeKeeper
A giant unseen hand pick up Robin and casts
him into the gorge
What is your favorite color?
Every node has a Type (element, attribute,
Comment,processing-instruction, text), and either
a Name (dialog, episode) or a Value
(BridgeKeeper,What is your)
41Location Path
- You can use Xpath to get to various features of a
node - ltxslvalue-of selectname(.)gt
- ltxslapply-templates selectprocessing-instructio
n()comment()/gt - ltxsltemplate matchprocessing-instruction()comm
ent()gt - You can also do things like
- ltxslvalue-of selectname(..)gt
- ltxslvalue-of selectname(preceding-sibling(.))/
gt
42Predicates
- In addition to a location path, you might want to
restrict to nodes that satisfy certain additional
conditions - Predicate filter is used to narrow down the list
- Predicate is held between ' '
- Examples
- //dialog_at_speakerKing Arthur
- All dialog elements anywhere in the document
spoken by King Arthur. - student_at_e-mail
- All student subelements of the current node that
have an e-mail address - courses/course_at_year2001 and
_at_semesterspring - All course elements that are subelements of a
courses subelement of the current node - And which are for spring 2001
- /courses/courseposition()2
- The second course grandchild of the root.
43More Filters
ltxslapply-templates selectdialog_at_speakerspea
kernamegt Apply templates to any dialog
children of the current node whose speaker is the
variable speakername ltxslfor-each
selectcalendarperson/_at_handlevarun/activity
date/_at_month3gt . ltxslvalue-of select
./_at_description/gt lt! Will print out the
description of the activity --gt lt/xslfor-eachgt
For every calendar item subelement of the
current node for every person whose handle is
varun grab any activity they did in month 3, and
do the following (print out description of the
activity.
44Multi-step Xpath
- Can continue on after a predicate test
- student_at_lnamegt c/address/city
- cities of all students whose last name is above c
in the alphabet - (student_at_lnamegt c)position()1
- The first student whose name is above c in the
alphabet - student _at_lnamename/_at_
- All attributes of the student whose last name
matches the variable (or parameter) name
45Multi-step Xpath
- ltxsltemplate matchuniversitygt
- ltxslfor-each selectenrollment/classcount(./stu
dent)5 gt - ltxslvalue-of select.gt
- Note that . here means the class. . normally
stands for the context node, which changes with
each step of the multi-step evaluation process. - Compare this with
- ltxsltemplate matchuniversitygt
- ltxslfor-each select enrollment/classcount(./s
tudent) count(current()/student gt
46Using with Transformer
TransformerFactory tFactory TransformerFactory.n
ewInstance() String stylesheet
"prices.xsl" String sourceId "newXML.xml"
File pricesHTML new File("pricesHTML.html")
FileOutputStream os new FileOutputStream(prices
HTML) Transformer transformer
tFactory.newTransformer(new StreamSource(styleshee
t)) transformer.transform(new
StreamSource(sourceId),
new StreamResult(os))