september 15, 2003houssam haitof1 xsl transformation houssam haitof
Post on 22-Dec-2015
219 views
TRANSCRIPT
September 15, 2003
Houssam Haitof 2
XSL
XSL (eXtensible Stylesheet Language) is a language for expressing style sheets.
September 15, 2003
Houssam Haitof 3
What is a Stylesheet?
A stylesheet is a file which contains a set of rules for converting an XML document into another document, which can be XML, HTML, plain text …
September 15, 2003
Houssam Haitof 4
Style Sheet Specifications
XSL - Extensible Style Language Combines features of DSSSL and CSS using XML syntax
DSSSL - Document Style and Semantics Specification Language An international SGML standard for Scheme-like
languages for style sheets and document conversion
CSS - Cascading Style Sheet Specification Simple syntax for assigning styles to elements (in some
HTML browsers)
XSLT is an XML Document!
September 15, 2003
Houssam Haitof 5
XSL
It consists of three parts: XSL Transformations (XSLT): a language for
transforming XML documents An XML vocabulary for specifying formatting
semantics (XSL Formatting Objects) An expression language used by XSLT to
access or refer to parts of an XML document (XPATH)
September 15, 2003
Houssam Haitof 6
XSL-FO
Used to describe XML documents for display
W3C recommendation 15 October 2001 A language for completely describing a
styled document (content organization, styling, layouts and layout-selection rules)
Everything needed to format and paginate a document!
September 15, 2003
Houssam Haitof 7
XPath
Used in conjunction with XSLT Became a W3C recommendation 16
Nov 1999 Provides a language for pointing to
different parts of an XML document specifies location path from specific
context nodes (i.e. starting points)
September 15, 2003
Houssam Haitof 8
XPath Expressions
Addresses a specific part or parts of XML documents which then allows XSLT to transform them
Provides expressions for manipulation of strings, Booleans, numbers so that the XSL elements can act upon them
September 15, 2003
Houssam Haitof 9
XSLT
XSLT is the most important part of the XSL Standard.
It is the part of XSL that is used to transform an XML document into another XML document, or another type of document (text, java code, …)
September 15, 2003
Houssam Haitof 10
XSLT
XSLT can be used to manipulate an XML document
Can transform/extract information as a programming language can
Can be used like a database language to extract information
Uses XML syntax
September 15, 2003
Houssam Haitof 11
XSLT is rule-based
XSLT is based on template rules which specify how XML documents should be processed.
Template rules can be based in any order because XSLT is a declarative language.
The stylesheet declares what output should be produced when a pattern in the XML document is matched.
September 15, 2003
Houssam Haitof 12
Example
A stylesheet could declare that when the XSLT transformation engine finds a 'NAME' element it should add markup by calling the 'NAME' template.
The template syntax is:
<xsl:template match="NAME"> ...
</xsl:template>
September 15, 2003
Houssam Haitof 13
XSLT Capabilities
Add prefix or suffix text to content Remove, create, re-order and sort
elements Re-use elements elsewhere in the
document Transform data between XML formats Uses a recursive mechanism to go
through document
September 15, 2003
Houssam Haitof 14
XSLT consists of two parts
A source document original xml document
A result tree newly-created xml, html or a plain text
document A common way to describe the
transformation process is to say that XSL uses XSLT to transform an XML source tree into an XML result tree.
September 15, 2003
Houssam Haitof 15
Trees & Nodes
With XSLT one does not think in terms of documents, but in terms of trees & nodes
A tree represents the data in a document as a set of nodes
Nodes are elements, attributes, comments, etc in a hierarchy
September 15, 2003
Houssam Haitof 16
Structure of Style Sheet
style sheet Style sheet is made up of templates Templates have other XSL elements/XPath expressions Style sheet is usually given extension *.xsl Must be well-formed XML
XSLT has a number of pre-defined elements to perform certain transformations <xsl:stylesheet> <xsl:template> <xsl:value-of> <xsl:if> <xsl:choose> …
September 15, 2003
Houssam Haitof 17
A Sample XML file
<?xml version="1.0"?><!-- File Name: XslDemo01.xml --><?xml-stylesheet type="text/xsl" href="XslDemo01.xsl"?><BOOKS><BOOK> <TITLE>Moby-Dick</TITLE> <AUTHOR> <FIRSTNAME>Herman</FIRSTNAME> <LASTNAME>Melville</LASTNAME> </AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE></BOOK>
…
</BOOKS>
September 15, 2003
Houssam Haitof 18
A simple transformation of an XML file
Book Description
Author: Herman MelvilleTitle: Moby-DickPrice: $9.95Binding type: hardcoverNumber of pages: 724
Author: John DoeTitle: TalesPrice: $19.95Binding type: softcoverNumber of pages: 450
September 15, 2003
Houssam Haitof 19
<?xml version ="1.0"?><xsl:stylesheet version ="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><HTML><HEAD><TITLE>XSLT Test</TITLE></HEAD><H2>Book Description</H2><xsl:for-each select=“/BOOKS/BOOK”>
<Span Style="font-style:italic">Author: </Span> <xsl:value-of select=“./AUTHOR"/><br/>
<span style="font-style:italic">Title: </span> <xsl:value-of select=“./TITLE"/><br/>
<span style="font-style:italic">Price: </span> <xsl:value-of select=“./PRICE"/><br/>
<span style="font-style:italic">Binding: </span> <xsl:value-of select=“./BINDING"/><br/>
<span style="font-style:italic">Number of Pages: </span> <xsl:value-of select=“./PAGES"/><br/></xsl:for-each></HTML></xsl:template></xsl:stylesheet>
September 15, 2003
Houssam Haitof 20
XSL Processor
An XSL processor can transform an input XML document to a XML or any other output document based on rules in a second XML document (i.e. a XSLT file)
September 15, 2003
Houssam Haitof 21
Template Matching
XPath is used by XSLT in the match attribute of a template e.g. <template match=“sentence”> matches any sentence element.
The stylesheet processor goes through the XML document one element at a time, finds the first template which that element matches, and carries out the instructions in that template.
September 15, 2003
Houssam Haitof 22
Template Matching (cont)
If the element does not match any template in the stylesheet, then the default behaviour is for the processing to pass through to the children of this element without carrying out any instructions.
When the processing reaches an element which has only text children, the result of processing these children is to print out the text.
September 15, 2003
Houssam Haitof 23
Some examples of patterns
* matches any element / matches the top-level element sentence/word matches any word
element with a sentence parent word[@pos=‘noun’] matches any
noun element whose pos attribute has the value noun
Many other possibilities …
September 15, 2003
Houssam Haitof 25
To begin
Start by declaring that this is an xml document <?xml version ="1.0"?>
Next: declare the processing instruction & the namespace
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/
Transform" version="1.0">
September 15, 2003
Houssam Haitof 26
XSLT Namespace
Every XSL file needs to specify the XSL namespace so that the parser knows which version of XSLT to use.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/
Transform" version="1.0"> The namespace prefix xsl: is used in the rest of
the XSL file to identify XSL processing statements.
If a statement isn't prefixed with xsl:, then it's simply copied to the output without being processed.
September 15, 2003
Houssam Haitof 27
XSL Elements
<xsl:template [match = “/”]> Before processing can begin, the part of the XML
document with the information to be copied to the output must be selected with an XPath expression.
The selected section of the document is called a node and is normally selected with the match operator.
If the entire document is to be selected, match the root node using match="/".
Another approach is to match the document element (the element that includes the entire document).
September 15, 2003
Houssam Haitof 28
XSL Elements ..
<xsl:apply-templates> The <xsl:apply-templates> element
applies a template rule to the current element or to the current element's child nodes.
If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute.
September 15, 2003
Houssam Haitof 29
XSL Elements ..
<xsl:for-each> allows for processing on a number of
nodes in the source tree, one at a time Attributes
• “select” denotes the path to the nodes Example
<xsl:for-each select=“/BOOKS/BOOK” >.. // other xsl elements</xsl:for-each>
September 15, 2003
Houssam Haitof 30
<xsl:value-of> When the xsl:for-each expression has
selected a ‘BOOKS' element, the xsl:value-of expression extracts and copies to the output file the value stored in the selected element.
syntax: <xsl:value-of select= Examples
<xsl:value-of select="."><xsl:value-of select="customer/@id"><xsl:value-of select="author"/>
XSL Elements ..
September 15, 2003
Houssam Haitof 31
XSL Elements ..
<xsl:if> Allows for conditional processing.
• Attribute: test Example
<xsl:if test=“title[@type=‘main]”><b><xsl:apply-templates/></b></xsl:if>
September 15, 2003
Houssam Haitof 32
XSL Elements ..
<xsl:sort> XSLT can be used to sort elements by
alphabetical or numerical order, according to attribute values, text contents, in ascending or descending order, and more … <xsl:sort
select = string-expression data-type = { "text" | "number" | Qname } order = { "ascending" | "descending" } case-order = { "upper-first" | "lower-first" } lang = { language-code }/>
September 15, 2003
Houssam Haitof 33
More XSL Elements ..
Some of the xsl elements not covered <xsl:choose> <xsl:when> <xsl:attribute> <xsl:element> <xsl:comment> <xsl:text> <xsl:variable>