Title: XSL Transformation
1XSL Transformation
2Introduction
3Why Styling?
- XML
- Concentrate on structure
- Not concentrate on appearance
- To View XML documents, we need to format or style
them - The styling is directly related to and derived
from the structure of the document - Two W3C styling sheets
- CSS Cascading Styling Sheet
- XSL XML Style-sheet Language
4Cascading Style Sheet (CSS)
- Originally for HTML
- A set of rules that tell the browser which font,
style, and margin to use to display the text
- section.title
- font-family Garamond, Time News Roman,
serif - font-size 10pt
- margin5px
- displayblock
- font-style italic
5XML Stylesheet Language (XSL)
- Support transforming the document before display
- A transformation language
- A formatting language
- ltPgtltBgt Table of Content lt/Bgtlt/Pgt
- ltULgt
- ltxslfor-each select article/section/titlegtltLI
gtltAgt - ltxslvalue-of select./gt lt/Agtlt/LIgt
- lt/xslfor-eachgt
- lt/ULgt
6XSL
- XSLT XSL Transformation
- a language for transforming XML documents into
other XML (or non XML) documents. - Electronic commerce, Electronic data exchange,
Metadata change - XSLFO XSL Formatting Objects
- XML vocabulary for specifying formatting
- XSLFO and CSS are very similar in scope
7XSL Processors
- Xalan (include Java Version and C Version)
- http//xml.apache.org/xalan-c/
- James Clark
- www.jclark.com
8Concepts of XSLT
Source Document
Resulting Document
XSLProcessor
XSLT Style Sheet
9Concept of XSLT (Cont.)
- XSLT A language to specify transformation of XML
documents - Add elements specifically for viewing
- Add the logo or address of the company
- Create new content from an existing one
- Create table of content
- Create views for different users
- Convert between different DTDs
- Transform XML documents into HTML
10Concept of XSLT (Cont.)
- A transformation in the XSLT language is
expressed as a well-formed XML document
conforming to the Namespaces in XML
Recommendation which may include both elements
that are defined by XSLT and elements that are
not defined by XSLT. - XSLT-defined elements are distinguished by
belonging to the XSLT namespace. - XSLT namespace http//www.w3.org/1999/XSL/Transfo
rm - xmlnsxslhttp//www.w3.org/1999/XSL/Transform
11Concept of XSLT (Cont.)
- A transformation expressed in XSLT describes
rules for transforming a source tree into a
result tree. - The transformation is achieved by associating
patterns with templates. - A pattern is matched against elements in the
source tree. - A template is instantiated to create part of the
result tree. - The result tree is separate from the source tree.
- In constructing the result tree, elements from
the source tree can be filtered and reordered,
and arbitrary structure can be added.
12Concept of XSLT (Cont.)
- A transformation expressed in XSLT is called a
stylesheet - A stylesheet contains a set of template rules. A
template rule has two parts - A pattern which is matched against nodes in the
source tree - A template which can be instantiated to form part
of the result tree
13Concept of XSLT (Cont.)
- XSLT transforms one XML tree into another
- select particular nodes from the input tree
- reorder/process nodes
- output nodes
- Tree nodes
- The root
- Elements
- Text
- Attributes
- Namespaces
- Processing instructions
- Comments
14(No Transcript)
15Basic XSLT
16Example Scenario
- An XML document describing two people 8-1.xml
- Basic XSLT
- XSLT is a well-formed XML document
- The tree of the source document
- The tree of the resulting document
- How to transform one into the other
- The root of this document is stylesheet
- In the http//www.w3.org/1999/XSL/Transform name
space - xmlnsxslhttp//www.w3.org/1999/XSL/Transform
- xslstylesheet
17An Minimal XSLT Stylesheet
- 8-1.xsl
- The output consists of a text declaration plus
the text of the input document - The net effect of applying an empty stylesheet to
any input XML document is to reproduce the
content but not the markup of the input document - Built-in rules
18Using Xalan
- Xalan is an XSLT processor that reads an XSLT
stylesheet and an input XML document, and builds
an output document by applying the instructions
in the stylesheet to the information in the input
document - Set up the Xalan Environment
- Download Xalan-C and Xerces-C
- Set path
- Path D\Package\xerces-c_2_3_0-win32\bin
d\package\xml-xalan\c\Build\Win32\VC6\Release
C\WINNT\system32C\WINNTC\WINNT\System32\Wbem
- Use Xalan
- Xalan -o outputfile xmlSource stylesheet
19The xml-stylesheet Processing Instruction
- XML documents that will be served directly to web
browsers can have an xml-stylesheet PI in their
prolog telling the browser where to find the
associated stylesheet for the document - 8-2.xml lt?xml-stylesheet type"text/xsl"
href"./8-1.xsl" ?gt
20Templates and Template Rule
- To control what output is created from what
input, you add template rules to the XSLT
stylesheet - Each template rule is represented by an
xsltemplate element - This element has a match attribute that contains
an XPath pattern identifying the input it matches - Its content lists the elements to be inserted in
the output - Example
- ltxsltemplate matchpersongtA Personlt/xsltemplat
egt - 8-2.xsl literal data characters
- 8-3.xsl literal result elements
21Calculating the Value of an Element with
xslvalue-of
- xslvalue-of takes the value of something and
copies it into the output document - The value of a node is always a string
- Value of an element node concatenation of all
the parsed character data (but not markup)
between its start and end tag - The element whose value is taken is identified by
a select attribute containing an XPath expression - 8-4.xsl
22Path
- XML path is similar to file paths
- Start from the root of an XML document
- Elements are separated by /
- The root is a node before the top-level element
- Path Tips
- / immediate children of a node
- /article/title
- // all the descendant from a node
- /article//title
- wildcard character (/article/)
- or character (title p)
- XPath recommendation (http//www.w3.org/TR/xpath)
23Wildcards
- Asterisk () matches any element node regardless
of name - does not match attributes, text nodes,
comments, or processing-instruction nodes - You can put a namespace prefix in front of
(svg) - node() wildcard matches not only all element
types, but also text nodes, comments,
processing-instruction nodes, and attribute nodes - _at_ matches all attribute nodes
- _at_xlink (namespace)
24Path Example
root
/
/article
article
title
date
abstract
keyword
section
/article/title
25Applying Templates with xslapply-templates
- By default, an XSLT processor reads the input XML
document from top to bottom, starting at the root
of the document and working its way down using
preorder traversal - Template rules are activated in the order in
which they match elements encountered during this
traversal - A template rule for a parent will be activated
before template rules matching the parents
children - The xslapply-templates element lets you make
explicit choice of processing order - Its select attribute contains an XPath expression
telling the XSLT processor which nodes to process
at that point in the output tree - 8-5.xsl, 8-6.xsl, 8-7.xls
26Actions of XSL Processor
- Load the style sheet
- Load the source document
- Walk through the source document from root to
leaf - Match the current node against a template
- If match, generates the node in the resulting
tree - Encounter ltxslapply-templatesgt
- Move to the children of the current node and
repeat the process
Recursive call to the Stylesheet
27The Built-in Template Rules
- ltxsltemplate match / gtltxslapply-template
s /gtlt/xsltemplategt - ltxsltemplate match"text()_at_"gt ltxslvalue-of
select"."/gtlt/xsltemplategt - ltxsltemplate match"processing-instruction()comm
ent()"/gt - A simple template rules matches namespace nodes
and instructs the processor not to copy any or
part of the namespace node to the output
Whenever a text or attribute node is matched ?
output the node value
Note by default the XSLT processor never reaches
attribute nodes, unless a specific rule applies
templates to them
28Modes
- Sometimes the same input content needs to appear
multiple times in the output, formatted according
to a different template each time - Both xslapply-templates and xsltemplate
elements can have optional mode attributes that
connect different template rules to different
uses - A mode attribute on xsltemplate identifies in
which mode that template rules should be
activated - An xslapply-templates with a mode attribute only
activates template rules with matching mode
attribute - 8-8.xsl
29Second Example Scenario
- A monthly e-zine, Pineapplesoft Link
- E-mail to subscribers
- Post a copy on Web
- Use one document structure
- XML (e-zine.xml)
- Two different presentations
- XSLT (e-zine.xsl, e-zine2.xsl)
30Matching on Attributes
- Syntax
- matchelement_at_attributevalue
- matchurl_at_protocolmailto
- lturl protocolmailtogt claven_at_cc.nctu lt/urlgt
- lturl protocolhttpgt http//. lt/urlgt
- matchelement_at_attribute
- matchurl_at_protocol
- lturl protocolhttpgt http//. lt/urlgt
- lturlgthttp//www.w3.org lt/urlgt
31Matching Text and Functions
- Functions restrict paths to specific elements
- /article/sectionposition()2/title/text()
- /article/section2/title/text()
- count(//title)
32Matching Text and Functions (Cont.) -- Common XSL
functions
- position()
- the position of the current node in the node set
- text()
- The text (content) of an element
- last()
- the position of the last node in the current node
set - count()
- the number of nodes in the current node set
- not()
- negates the argument
- contains()
- return true if the first argument contains the
second argument - starts-with()
- return true if the first argument starts with the
second argument
33Other XSLT Elements
- Named templates
- Numbering and sorting output elements
- Conditional processing
- Iteration
- Extension elements and functions
- Importing other stylesheets
34Creating Nodes in the Resulting Tree
- xslelement
- creates element with a computed name
- xslattribute
- creates attribute with a computed value
- xslattribute-set
- Conveniently combines several xslattribute
- xsltext
- Create a text node
- xslprocessing-instruction
- Create a processing instruction
35Creating Nodes in the Resulting Tree (Cont.)
- xslcomment
- create a comment
- xslcopy
- copies the current node
- xslvalue-of
- computes text by extracting from the source tree
or inserting a variable - xslif
- instantiates its content if the expression if
true - xslchoose
- select elements to instantiate among possible
alternatives - xslnumber
- creates formatted number
36Priority
- Templates with more specific paths take
precedence over less specific templates - If conflict between two templates of equivalent
priority - Report an error
- Choose the template that appears last in the
style sheet
37Customized View
XSL Processor
38Advanced XSLT toc.xsl
39Declaring HTML Entities in a Style Sheet
- lt!DOCTYPE xslstylesheet lt!ENTITY copy
0169gt gt
40Reorganizing the Source Tree
- ltMETA NAME"keywords"gt ltxslattribute
name"CONTENT"gt ltxslvalue-of
select"article/keywords"/gt, lt/xslattributegtlt/
METAgt - ltMETA NAMEkeywords contentXML, XSL, style
sheet, publishing, Webgt
41Calling a Template
- Template define with a name
- ltxsltemplate name"title"gt ltxslvalue-of
select"/article/title"/gt ( - ltxslvalue-of select"/article/date"/gt )
- lt/xsltemplategt
- Call a template
- ltxslcall-template nametitle /gt
42Include a Style Sheet
- ltxslinclude hrefcore.xsl /gt
- xslinclude must be a direct child of
xslstylesheet
43Repetitions
- ltxslfor-each select"article/section/title"gtltLIgt
ltAgtltxslattribute name"HREF"gt ltxslvalue-of
select " generate-id()"/gtlt/xslattributegtltxslv
alue-of select"."/gtlt/Agtlt/LIgtlt/xslfor-eachgt - xslfor-each loop over elements
- generate-id() Return a unique identifier for the
current node
44Related Recommendations
- XSL Transformation (XSLT) http//www.w3.org/TR/199
9/REC-xslt-19991116 - XML Path (XPath) http//www.w3.org/TR/xpath
- XML Stylesheet http//www.w3.org/TR/xml-stylesheet
- Associate stylesheets with XML documents