managing information (cscu9t4) lecture 5: xml style · 2016. 3. 22. · and is a style sheet...
TRANSCRIPT
-
3/22/2016
1
MANAGING INFORMATION (CSCU9T4)
LECTURE 5: XML STYLE
Gabriela Ochoa
http://www.cs.stir.ac.uk/~goc/
CONTENT
Revise XML Schema example
Content vs. Display (e.g. Word vs. XML)
XSL: eXtensible Stylesheet Language. A style
sheet language for XML documents.
XSLT: XSL Transformations. A language for
transforming XML documents
XPath: a language for navigating in XML
documents
A number of examples
2
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
http://www.cs.stir.ac.uk/~goc/
-
3/22/2016
2
RESOURCES
Books
XML in a Nutshell (2004) by Elliotte
Rusty Harold, W. Scott Means, O'Reilly
XML 1.1 Bible (2004) by Elliotte Rusty
Harold, Wiley
Internet & World Wide Web How to
Program, 5/e by Paul J. Deitel, Harvey M.
Deitel, Abbey Deitel
Links and websites W3 ORG
XML Tutorial: W3Schools
XML.COM O’REALLY
XSLT Online Transformations
W3 Schools: XLST online Editor
3
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
WHAT IS XSL?
XSL stands for eXtensible Stylesheet Language,
and is a style sheet language for XML documents
An XSL style sheet is, like with CSS, a file that
describes how to display an XML document of a
given type
XSL consists of 2 components:
XSLT - a language for transforming XML documents
XSL-FO - a language for formatting XML documents
Related tools
XPath - a language for navigating in XML documents.
XSLT uses XPath to find information in an XML doc
XQuery - a language for querying XML documents
4
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
http://www.w3.org/Style/XSL/WhatIsXSL.htmlhttp://www.w3.org/Style/XSL/WhatIsXSL.htmlhttp://www.w3.org/Style/XSL/WhatIsXSL.htmlhttp://www.xml.com/pub/a/98/10/guide0.htmlhttp://www.utilities-online.info/xsltransformation/.VQPxPxCsUqshttp://www.w3schools.com/xsl/default.asp
-
3/22/2016
3
HOW DOES XLST WORK?
Styling requires:
1. A source XML document, containing the information that
the style sheet will display
2. The style sheet itself which describes how to display a
document of a given type. 5
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XSLT STYLESHEETS
Declarative language
Use pattern matching and templates to specify the
transformation
Vastly more expressive than a CSS stylesheets
May perform arbitrary computations
XSLT transformation can be done either on the
client (e.g. Explorer or Mozilla), or on the server
(e.g. Apache Xalan)
The target form is usually another XML document,
commonly XHTML or HTML
6
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
4
XSLT: AN EXAMPLE TRANSFORMATION
1. Demo: Simple ‘Users’ example in the browser with and w/o stylesheet
2. Example at: http://www.w3schools.com/xml/cd_catalog.xml
Empire Burlesque Bob Dylan USA Columbia 10.90 1985 …
XML file
• No information
about display
• Display is included
in the stylesheet
• Use XSLT to transform XML into HTML, before it is displayed in a browser
• The browser process the transformation
7
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
THE CD CATALOG EXAMPLE
My CD Collection Title Artist
-
3/22/2016
5
XSLT PROCESSING MODEL
An XSLT stylesheet consists of a number of template
rules: template rule = pattern + template
For a given input XML document, the output is
obtained as follows:
The source tree is processed by processing the root node
A single node is processed by:
1. finding the template rule with the best matching pattern
2. instantiating its template (creates result fragment + continues
processing recursively)
A node list is processed by processing each node in order
and concatenating the results
Simple illustrative animation: http://cs.au.dk/~amoeller/XML/transformation/p2.html
9
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
STRUCTURE OF A STYLESHEET
An XSLT stylesheet is itself an XML document
… a template rule template …
• Namespace http://www.w3.org/1999/XSL/Transform is used to recognize the XSLT elements.
• The XML document may refer to a stylesheet using the processing
instruction:
10
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
http://cs.au.dk/~amoeller/XML/transformation/p2.htmlhttp://cs.au.dk/~amoeller/XML/transformation/p2.htmlhttp://cs.au.dk/~amoeller/XML/transformation/p2.html
-
3/22/2016
6
XLST USES XPATH
XPath is an expression language for addressing parts of an
XML document
Xpath data model provides a tree representation of XML
documents as well as atomic values such as integers, strings,
and Booleans.
It uses path expressions to select a set of nodes or atomic
values in an XML document
Path expressions look very much like the expressions you see
when you work with a traditional computer file system
XPath contains a library of standard functions
XPath is a W3C recommendation
11
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XPATH DATA MODEL
XPath sees an XML document as a tree structure
The topmost element of the tree is called the root
element.
Each information (XML elements, attributes,
text, etc.) is called a node.
Nodes that XPath can see
Root node
Elements and attributes
Special nodes like comments, processing instructions,
namespace declarations.
12
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
7
XLST, XML AND TREES The transformation process: XSLT transforms an XML source-tree
into an XML result-tree.
XML Data Representation
sample1.o sample1.cpp sample1.h g++ -c sample1.cpp
XML Tree representation
dependency
object depends
sample1.o sample1.cpp
depends
sample1.h
rule
g++ -c … 13
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
subtree
TREE TERMINOLOGY
Root: node without parent (A)
Siblings: nodes share the same parent
Internal node: node with at least one child (A, B, C, F)
External node (leaf ): node without children (E, I, J, K, G, H, D)
Ancestors of a node: parent, grandparent, grand-grandparent, etc.
Descendant of a node: child, grandchild, grand-grandchild, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any node (3)
Degree of a node: the number of its children
Degree of a tree: the maximum number of its node.
Subtree: tree consisting of a node and its descendants
A
B D C
G H E F
I J K
A Tree is an abstract model of a hierarchical structure.
14
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
8
XPATH SYNTAX
Let us consider the following XML document
Harry Potter 29.99 Learning XML 39.95
15
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XPATH: SELECTING NODES
XPath uses path expressions to select nodes in an XML document
The most useful path expressions are listed below:
Expression Description
Nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node
that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node,
@ Selects attributes
16
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
9
XPATH: EXAMPLES OF PATH EXPRESSIONS
Path Expression Selects
bookstore All nodes with the name "bookstore"
/bookstore The root element bookstore
bookstore/book All book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book
All book elements that are descendant of the
bookstore element, no matter where they are
under the bookstore element
//@lang All attributes that are named lang
17
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
PREDICATES
Are used to find a specific node or a node that contains a specific value.
Are always embedded in square brackets.
Path Expression Selects
/bookstore/book[1] The first book element that is the child of the bookstore element.
/bookstore/book[last()] The last book element that is the child of the bookstore element
/bookstore/book[position()35.00] All the book elements of the bookstore element that have a price element with a value greater
than 35.00
18
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
10
SELECTING UNKNOWN NODES
Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
XPath wildcards can be used to select unknown XML elements.
Some Examples:
Path Expression Selects
/bookstore/* All the child element nodes of the bookstore element
//* All elements in the document
//title[@*] All title elements which have at least one attribute of any kind
19
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XPATH: SELECTING SEVERAL PATHS
• By using the | operator in an XPath expression you can select
several paths.
• Some Examples:
Path Expression Select
//book/title | //book/price All the title AND price elements of all book elements
//title | //price All the title AND price elements in the document
/bookstore/book/title | //price All the title elements of the book element of the bookstore element AND all the price
elements in the document
20
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
11
XPATH AXES
An axis defines a node-set relative to the current node.
Harry Potter 29.99 Learning XML 39.95
Axis Names: • ancestor • ancestor-or-self • attribute • child • descendant • descendant-or-self • following • following-sibling • namespace • parent • preceding • preceding-sibling • self
Example XML:
Details at: http://www.w3schools.com/xpath/xpath_axes.asp
21
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XPATH: LOCATION PATH EXPRESSION
A location path can be absolute or relative
Absolute locations start with a slash ‘/’: /step/step/...
Relative locations does *not* start with a ‘/’: step/step/...
Location paths consist of one or more steps, each separated
by a slash
Each step is evaluated against the nodes in the current
node-set. A step consists of:
an axis: defines the tree-relationship between the selected
nodes and the current node
a node-test: identifies a node within an axis
zero or more predicates: to further refine the selected node-set
Syntax or a location step:
axisname::nodetest[predicate]
22
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
http://www.w3schools.com/xpath/xpath_axes.asphttp://www.w3schools.com/xpath/xpath_axes.asp
-
3/22/2016
12
EXAMPLES: LOCATION PATH EXPRESSION
Example Selects
child::book All book nodes that are children of the current node
attribute::lang The ‘lang’ attribute of the current node
child::* All element children of the current node
attribute::* All attributes of the current node
child::text() All text in the children of the current node
child::node() All children of the current node
descendant::book All book descendants of the current node
ancestor::book All book ancestors of the current node
ancestor-or-self::book All book ancestors of the current node - and the current as well if it is a book node
23
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
BACK TO XSLT • We took some time to cover Xpath
• We get back to explaining more details of XSLT
• Let us first consider the CD Catalog Example
• Then some extensions to it
-
3/22/2016
13
HOW DOES XLST WORK?
Styling requires:
1. A source XML document, containing the information that
the style sheet will display
2. The style sheet itself which describes how to display a
document of a given type. 25
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XSLT ELEMENTS
XSLT is based on the idea of templates.
The idea is to specify a number of templates that each
match XML in the source document
When the matching XML is found, the template is
activated and its contents are added to the output
document.
Example:
A template that matches a element.
For each encountered in the source document the corresponding template will be activated
Any code inside the template will be executed and added to
the output
Using templates to process various parts of the source
document, is one of the most powerful features of XSLT 26
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
14
MAIN XSLT ELEMENTS
All-encompassing document element used to hold all
your templates
Use for setting which version of XSLT you want to use
The most important of XLST elements. It has two
features:
1. What items from the source document it should handle
2. What should be added to the output when it is executed
27
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
MAIN XSLT ELEMENTS
:
An alternative way of processing a number of items in a similar fashion (it can also be done with )
Group the items and produce output based on each one
:
Used to evaluate an expression and add the result to the output.
Example:
Process a element
Use to add the contents of its element to the output
:
Responsible for deciding which items in the source document
should be processed
They are then handled by the appropriate template.
28
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
15
THE CD CATALOG EXAMPLE
My CD Collection Title Artist
-
3/22/2016
16
XSLT ELEMENT
A template contains rules to apply when a specified node is
matched.
The element is used to build templates.
The match attribute is used to associate a template with an
XML element or for the entire XML document.
The value of the match attribute is an XPath expression
In the Example:
The match="/" attribute associates the template with the root of
the XML source document.
The content inside the element defines some HTML to write to the output.
31
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XLST AND
ELEMENT S
Locates elements in the XML document and repeats a
template for each one.
The select attribute describes the element in the source
document.
Selects a child in the hierarchy and
Inserts the content of that child into the template.
32
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
17
XSLT SORT
The element is used to sort the output
Add an element inside the element in the XSL file:
Demo, by editing the XSL file
33
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
XSLT FILTER
It is possible to have filters to the XML file
Add the filter in a element:
Legal operators are:
= (equal)
=! (not equal)
< (less than)
> (greater than)
Demo, by editing the XSL file
34
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
18
XSLT CONDITIONAL IF
It is possible to have a conditional if test against the content of the file, by adding an element to your XSL document :
...some output if the expression is true...
Demo, by editing the XSL file (inside the element ):
-
3/22/2016
19
THE ELEMENT
Applies a template to the current element or to the
current element's child nodes
If we add a select attribute to the
element it will process only the child element that matches the value of the
attribute.
We can use the select attribute to specify the order
in which the child nodes are processed.
37
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
38
My CD Collection
… Continue next slide
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
-
3/22/2016
20
39
Title:
Artist:Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
SUMMARY
XSL: eXtensible Stylesheet Language. A style
sheet language for XML documents.
XSLT: XSL Transformations. A language for
transforming XML documents
XPath: a language for navigating in XML
documents
A number of examples with explanations
More XSLT elements listed at : http://www.w3schools.com/xsl/xsl_w3celementref.asp
40
Ga
brie
la O
choa
, goc@
cs.stir.ac.u
k
http://www.w3schools.com/xsl/xsl_w3celementref.asphttp://www.w3schools.com/xsl/xsl_w3celementref.asp