Title: XML technologies part 2: XSL, XSLT,
1Presentation 19
- XML technologies part 2 XSL, XSLT,
- XSL-FO, XPath XML Programming
2Outline
- XML recap
- Formatting CSS or XSL?
- XPath
- XSL/XSLT
- XSL-FO
- XML Programming
3XML markup recap
- XML based on SGML (subset of)
- Like SGML for structure not layout (as HTML)
- XML targets the Internet but is also being used
for application exchange formats (Open Office,
XMI) CSVs - XML is an W3C Recommendation
- http//www.w3.org/TR/REC-xml
- XML Namespaces for multiply namespaces
- DTDs or XML Schemas for structure
4Local definitions vs public in XML Schema
- Instead of using the type system
- Use local definitions
- Another public variant is using ref
How may this be done using the type approach
instead?
5Formatting XSL CSS
- XML is only content no formatting
- Possible to transform the data to XHTML (or
other) using JavaScript og server-side - The W3C ideal is using CSS or XSL eXtensible
Style Sheets - CSS is most common today but XSL has more
features
6XSL vs CSS
- W3C made both XSL and CSS
- The Extensible StyleSheet Language
- XML based language for the formatting of document
- which by some is regarded as more advantageous
compared with the more cryptic CSS - XSL consists of XSL-FO (Formatting Objects), the
XSL Transformations language (XSLT) and XPath for
defining and accessing part of the XML document - Exactly the same job may however be solved by
using server-side programming - but of course XSLT may be used client-side to
relief the server (it can also be applied
server-side)
7XSL vs CSS II Why both?
- Why Both (XSL CSS)?
- Well of course there are practical reasons
- If you have already learned CSS JSP, it takes
some efforts to master XSL to the same degree - In fact XSL-FO is not fully implemented
everywhere - but what does the W3C have to say?
8XSL vs CSS III - differences
9When to use XSL CSS (IIII)
- Why does W3C recommend two different style
languages? Which one should you use? Basically,
the rule can be summarized very succinctly - Use CSS when you can, use XSL when you must.
- http//www.w3.org/Style/CSS-vs-XSL
- You may choose to use CSS alongside XSLT when you
are dealing with XHTML - In fact this is the most common solution today
10The 3 Main Technologies of XSL
- XSLT, a language for transforming information
- XSL or XSL-FO, a language for formatting
information - XPath, a language for defining parts of an XML
document and accessing them - Each of these elements could fill an entire
class. - You will only need to be acquainted with the
overall functionality of these
11XPath
- Flexible notation for navigating around trees
- Resembles notation used in Unix filesystems
- A basic technology that is widely used
- uniqueness and scope in XML Schema
- pattern matching an selection in XSLT
- relations in XLink and XPointer
- computations on values in XSLT and XQuery
- XPath is not written in XML
- XPath is a W3C Standard
12Location Paths in XPath
- location path evaluates to a sequence of nodes
- sequence is sorted in document order
- sequence will never contain duplicates
- The location path is a sequence of different
steps - A location step consists of
- an axis (a direction of selection successors,
descendants) - a nodetest (identifies a node within the axis)
- Some predicates (a statement/expression yielding
a boolean) - axis nodetest predicate
- childpriceprice9.90 (example)
13Evaluating Location Paths
- Location Paths may consist of several steps
- A step maps a context node into a sequence
- This also maps sequences to sequences
- each node is used as context node
- and is replaced with the result of applying the
step - The path then applies each step in turn, using
the former step as the sequence to work on - It may be absolute or relative (/cd/price or
cd/price)
14Example finding all C nodes
15What are we looking for here?
16XPath Axes
- An axis is a sequence of nodes
- The axis is evaluated relative to the context
node - XPath supports 12 different axes
- child, attribute, descendant, following, parent,
preceding, ancestor, self, following-sibling,
descendant-or-self, preceding-sibling,
ancestor-or-self - These all have a direction, either forward,
backward or stable (attribute)
17XPath Node Tests
- text() - childtext() all text nodes of current
node - comment() - childcomment()
- processing-instruction()
- node() - childnode() all children of current
node - - attribute all attributes of current node
- More node tests exists
18XPath Predicates
- General XPath expressions evaluated with the
current node as context - Result is coerced (translated) into a boolean
- a number yields true if it equals the context
position - a string yields true if it is not empty
- a sequence yields true if it is not empty
- Example childpriceprice9.90 - selects all
price elements that are children of the current
node with a price element that equals 9.90 - Example childcdposition()lt6 Selects the
first five cd children of the current node
19XPath Abrivated Syntax
- Tedious work entering XPath syntax
- Enter the abrivated syntax
Table from http//www.w3schools.com
20XPath Examples
21XPath Advanced
- This presentation has only shown the basic
elements - Feel free to explore further
- Expressions
- Numerical, Equality, Relational, Boolean, Path,
Filter - Variable References
- test
- Functions
- 106 with namespace fnhttp//www.w3.org/2004/07/xp
ath-functions - Examples fnavg((1,2,3,4)), fnround(23.5)
22XSLT
- XSL Transformations
- Language for transforming a format neutral XML
document into another XML document e.g. XHTML
or WML for presentation - May also add new elements or remove elements
- XSLT relies heavily on XPath for pattern matching
- Specifying patterns for template rules
- Selecting nodes for processing
- Computing boolean conditions
- Generating text contents for the output document
23XSLT Template rules
- An XSLT stylesheet contains template rules
- The processor finds the most specific rule for
the document root - It then executes the template body
- Find the template rules that match the contex
node - Select the most specific one
- Evaluate the body (a sequence constructor)
24Applying Templates
- ltxslapply-templatesgt element applies a template
rule to the current element or to the current
element's child nodes
25Name, Mode, Priority, Sorting Grouping
- Templates may have other attributes
- name used to call templates like function
- mode used to restrict the candidate templates
- priority used to determine specificity
- Other features include
- Conditionals (ltxslif/gt, ltxslchoose/gt)
- Control statemetnts (ltxslfor-each/gt)
- Grouping (ltxslfor-each-group/gt)
- Sorting (ltxslsort/gt)
- Numbering (ltxslnumber/gt)
- Functions (ltxslfunction/gt)
- Parameters (ltxslparam/gt)
26XSL/XSLT example
- Transforming a XML document using XSLT invovles 2
tree structures - Source tree (original XML document)
- Result tree (the transformed document)
- AND of course the XSL document! (so totally 3)
- In this example
- We take an XML document which could have been
generated from a RDBMS database - We want to transform this XML document into an
XHTML document for browsers - And maybe we COULD transform it into a PDF, and a
WML document (for mobile phones) or whatever we
might need in the future
27XML dokumentSource treeGames.xml
- 1 lt?xml version "1.0"?gt
- 2 lt?xmlstylesheet type "text/xsl" href
"games.xsl"?gt - 3
- 4 lt!-- Fig. 20.23 games.xml --gt
- 5 lt!-- Sports Database --gt
- 6
- 7 ltsportsgt
- 8
- 9 ltgame id "783"gt
- 10 ltnamegtCricketlt/namegt
- 11
- 12 ltparagraphgt
- 13 More popular among commonwealth
nations. - 14 lt/paragraphgt
- 15 lt/gamegt
- 16
- 17 ltgame id "239"gt
- 18 ltnamegtBaseballlt/namegt
- 19
Value type specifies that games.xsl is a text/xsl
file.
A processing instruction that references the XSL
stylesheet games.xsl.. Wait isnt there
something wrong here why do we have XSL
embedded in the XML documet? We will look at this
later
28XSL dokumentElements.xsl
- 1 lt?xml version "1.0"?gt
- 2
- 3 lt!-- Fig. 20.24 elements.xsl --gt
- 4 lt!-- A simple XSLT transformation --gt
- 5
- 6 lt!-- reference XSL stylesheet URI --gt
- 7 ltxslstylesheet version "1.0"
- 8 xmlnsxsl "http//www.w3.org/1999/XSL/Tr
ansform"gt - 9
- 10 ltxsloutput method "html"
omit-xml-declaration "no" - 11 doctype-system
- 12 "http//www.w3.org/TR/xhtml1/DTD/xht
ml1-strict.dtd" - 13 doctype-public "-//W3C//DTD XHTML
1.0 Strict//EN"/gt - 14
- 15 ltxsltemplate match "/"gt
- 16
- 17 lthtml xmlns"http//www.w3.org/1999/xhtml"gt
- 18
- 19 ltheadgt
The stylesheet start tagwhich begins the XSL
stylesheet.
Element xsloutput writes an XHTML document type
declaration to the result tree.
The match attribute to select the document root
of the source document (i.e., game.xml).
29Elements.xsl
- 36
- 37 lt!-- insert each name and paragraph
element value --gt - 38 lt!-- into a table row.
--gt - 39 ltxslfor-each select
"sports/game"gt - 40
- 41 lttrgt
- 42 lttdgtltxslvalue-of select
"_at_id"/gtlt/tdgt - 43 lttdgtltxslvalue-of select
"name"/gtlt/tdgt - 44 lttdgtltxslvalue-of select
"paragraph"/gtlt/tdgt - 45 lt/trgt
- 46
- 47 lt/xslfor-eachgt
- 48
- 49 lt/tablegt
- 50
- 51 lt/bodygt
- 52
- 53 lt/htmlgt
- 54
These three select values are all XPath
expressions. Other types of XPath uses include /
for navigating to child nodes, and using
wildcards for selecting unknown elements. Other
types of usage includes indexing e.g. 1 and
selecting attributes using _at_
30XHTML dokumentresult treeResult of XSLT
tranformation
31More examples?
- Will only sedate you with boredom and a sad
feeling of helplessness - Please go try it out yourself instead
- Plenty of examples at
- http//www.w3schools.com/xsl/default.asp
- Assignment (5 min.)
- Try making an XSL document which will print the
following bullet list HTML document - Cricket Popular among commenwealth nations (ID
783) - etc
- Consider the problems when using the XML document
with several XSL documents. How may it be done?
32Not nice with embedded XSL?
- You may use JavaScript to separate gracefully
lthtmlgt ltbodygt ltscript type"text/javascript"gt
// Load XML var xml new
ActiveXObject("Microsoft.XMLDOM") xml.async
false xml.load("cdcatalog.xml") // Load
XSL var xsl new ActiveXObject("Microsoft.XML
DOM") xsl.async false
xsl.load("cdcatalog.xsl") // Transform
document.write(xml.transformNode(xsl))
lt/scriptgt lt/bodygt lt/htmlgt
- Detect type of browser and load proper XSL doc
33 and server-side as well
- You may also use server-side programming for this
lt 'Load XML set xml Server.CreateObject("Micros
oft.XMLDOM") xml.async false xml.load(Server.Map
Path("cdcatalog.xml")) 'Load XSL set xsl
Server.CreateObject("Microsoft.XMLDOM") xsl.async
false xsl.load(Server.MapPath("cdcatalog.xsl"))
'Transform file Response.Write(xml.transformNode(
xsl)) gt
- and of course you dont really need XSL then
as the same job may be done in Java, C, VB etc
34Browser Troubles in Paradise
- XSLT in Internet Explorer 5 (and 5.5) is NOT
compatible with the official W3C XSL
Recommendation. - Internet Explorer 6 fully supports the official
W3C XSLT Recommendation. - Netscape 6 isn't fully supporting the official
W3C XSLT Recommendation. - Netscape 7 supports the official W3C XSLT
Recommendation. - http//www.w3schools.com/xsl/xsl_browsers.asp
35Cross Browser DOM Handling
36XSL-FO
- XSL-FO is a language for formatting XML data
- XSL-FO stands for Extensible Stylesheet Language
Formatting Objects - XSL-FO is a W3C Recommendation
- XSL-FO is now formally named XSL
- XSL-FO is not used extensively with WWW
technologies yet, as CSS and XSLT may be used
together. This may change however! - PDF / Web forms from XSL-FO (Scriptura)
37XSL-FO Formatting instructions
- Block Margin, Border, Padding, Background
- Block Styling Attributes
- font-family
- font-weight
- font-style
- font-size
- font-variant
- text-align
- text-align-last
- text-indent
- start-indent
- end-indent
- wrap-option (defines word wrap)
- break-before (defines page breaks)
- break-after (defines page breaks)
- reference-orientation (defines text rotation in
90" increments) - Which in general will handle the same stuff as
CSS
38Other XSL-FO elements
- Areas, Output, Flow, Pages, List, Tables
- All of which we will not cover here!
39XSL-FO Example
Below is a XSL-FO document
And the output would be
40XML, XML-FO XSLT Example
Below is a XML document
We apply some XSLT including XML-FO
And we get a result
41Programmatic XML access?
- Many APIs framework for XML handling
- For Java
- JDOM http//www.jdom.org/
- JAXP http//java.sun.com/xml/jaxp/
- SAX http//www.saxproject.org/
- C
- using System.Xml / System.Xml.Xsl
- Build-in support for XML, XSD, XSLT, XPATH, SOAP
- XMLSerializer Object / XML mapping
- http//www.csharphelp.com/archives/archive78.html
- Validating XML document against DTD, XSD, XDR
- http//support.microsoft.com/kb/307379
42C System.Xml and System.Xml.Schema
- Validating a Document with DTD in C
43Mapping Tools
- Mapping Tools
- XML to C/C/Java
- XML Spy 2007
44A JDOM Example Double Sugar
Access all ingredients Get the elements whose
attribute name equals sugar Get the amount
attribute and double it!
45Other XML Technologies
- XQuery Query Language of XML
- XLink XML hyperlinks
- XPointer named anchors
- All W3C Recommendations
- Limited browser support
- Study these yourselves