XSLT Extensible Stylesheet Language Transformations CC432 / Short Course 507 Lecturer: Simon Lucas University of Essex Spring 2002.

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents.
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
XML: Extensible Markup Language
XSLT 11-Apr-17.
Cascading Style Sheets
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
XSL XSLT and XPath 11-Apr-17.
XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
XML Unit 6 October 31. XML, review XML is used to markup data Used to describe information Uses tags like HTML –But all tags are user-defined –Must be.
Lecture 10. Back to the table example We can now return to considering in detail the first XSLT style-sheet we saw –The one which, when applied to the.
Rendering XML documents with XSL The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language) XSL enables.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – Carl Lagoze – Cornell University.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C standards for stylesheets – CSS – XSL: Extensible Markup.
10/06/041 XSLT: crash course or Programming Language Design Principle XSLT-intro.ppt 10, Jun, 2004.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
XSLT Brent P. Christie Major USMC. XSLT Overview  What is XSLT? –XSL is the Extensible Style Language. –It has two parts: the transformation language.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
CSE3201/CSE4500 Information Retrieval Systems
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
WORKING WITH XSLT AND XPATH
Extensible Markup and Beyond
1 © Netskills Quality Internet Training, University of Newcastle Introducing XML © Netskills, Quality Internet Training University.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
Extensible Stylesheet Language Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XSL-FO XSLT.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Lecture 11 XSL Transformations (part 1: Introduction)
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
Unit 3 — Advanced Internet Technologies Lesson 11 — Introduction to XSL.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
1 XMLXSL(T) and CSS NOEA/PQC (rev. fen) 2007 Stylesheets CSS:Cascading Style Sheets XSL(T):eXtended Stylesheet Language (Transformations)
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
XSLT I Robin Burke ECT 360. Outline History / Terminology XSLT processing XSLT syntax XPath XSLT basics Lab.
VCE IT Theory Slideshows by Mark Kelly study design By Mark Kelly, vceit.com, Begin.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.
XML in Web Technologies
More XML XML schema, XPATH, XSLT
Presentation transcript:

XSLT Extensible Stylesheet Language Transformations CC432 / Short Course 507 Lecturer: Simon Lucas University of Essex Spring 2002

Outline Overview Hello World The Transformation Process XPath Iteration, Sorting and Conditionals Examples Design Guidelines

XSLT Overview XML documents are used to mark-up content But: this is not very human-friendly XML source documents need to be transformed for display purposes XSLT is a way of achieving this By no means the only way!

XSL: Adding value to HTML One view of XSL is as adding value to HTML web-pages In this view, one might design an XSL transform by creating a web-page with some sample data in Then replacing the sample data with XSL template applications The added value is the dynamic data that gets inserted when the transform is now applied to XML source documents

!! Caution !! Some things in XSL are elegant and easy to achieve BUT BEWARE!!! It is also possible for XSL transforms to become very complex Complex XSL docs look ugly, and are hard to read and debug (though good tools may help) Therefore: keep it simple! If your XSL transforms become too complex, either try to decompose them into a set of simpler transforms OR: consider using a different technology

XSL and CSS Cascading Style Sheets (CSS) – can also be used to modify presentation of content CSS generally used within a browser to modify display of HTML elements XSL is much more powerful than CSS – can do sorting, filtering and simple calculations CSS can be generated by XSL to produce simple, good looking presentation in compatible browsers

Example Transform Paths Transform Engine (e.g. Xalan) XML Source Doc XSL-1 XSL-2 HTML For Web PDF for Printing

Transformation in Browser An xml doc can include a stylesheet suggestion Compatible browsers will pick up on this, and try to render the XML as HTML The next slides show similar XML documents rendered in IE 6 With and the stylesheet instruction – and with this instruction commented out

hello.xml <!–- <?xml-stylesheet type="text/xsl" href="greetings.xsl"?> --> hello bonjour hola!

Without style sheet

With style sheet

The Transform Process

An XSL transform takes an XML source document (tree) and produces a result document. The transform engine works by traversing the document source tree, and at each node in the tree working out which templates to apply.

Tree View hello.xml / greeting greetings hello holabonjour

Transform Algorithm 1.Parse the XSL document 2.Parse the Source Document 3.Set the context-set to be the root node of the source document 4.Find the most specific template that matches 5.Process each element in that template a.If it is an instruction, process it; this may involve appending more source elements to the context set e.g. via and recursing to step 4 b.Otherwise, copy it to the output

Comments on algorithm The transform is complete when the context-set is empty Note the recursive step in 5.a

Creating XSL Transforms

XSL Authoring To write an XSL transform, consider the source document hierarchy. Then, write rules on how to transform this Each rule does three things: 1.Specifies how it can match a node in the source 2.Specifies what to create in the result document 3.Specifies which child nodes to visit

XSL authoring contd. Template matches can be specified as absolute or relative Templates can also be given names, and called (invoked) by those names This is the closest XSL comes to a function call

Example: Transforming Hello We now consider some simple XSL transforms on hello.xml They all begin with the same declaration But we’ll choose various output options Note that due to the in-built default template, we get some output even without specifying any user-defined templates

Hello -> text <xsl:stylesheet version="1.0" xmlns:xsl=" Which gives: hello bonjour hola!

Notes on Hello World The attributes specified the version of XSL to use, and the name-space We also specified the output method Common choices are xml, html and text “text” ignores any elements created during the transform process “xml” and “html” options include these elements, but add different tags

Built in default template To allow an entire document to be processed, the following default template is set up: Since this is very general, it is overridden by more specific template matches in the supplied XSL document

Hello -> HTML Here we illustrate a transform to produce a simple HTML document The preamble is much as before We then match the greetings element below the document root And call to match the rest of the child nodes Only the elements find matching templates Note the way HTML is interspersed with XSL

(just the templates) Greetings Greetings

Output in a Browser

An identity transformation (elements only) <xsl:stylesheet version="1.0" xmlns:xsl="

Notes on identity transform A wild-card ‘*’ is used to match any elements We use to create a new element, for each element that the template matches The curly braces indicate that the expression should be evaluated name() is the Xpath expression that gives the name of the current node

Testing the identity transform With hello.xml as input, produced: hello bonjour hola! Note that the xml declaration on line 1 is due to the output type being set to “xml”

XPath So far we’ve looked at an example of how to match some templates to a source document The standard for specifying which elements to match is called XPath We’ll now see how to do some basic matching using XPath

The above template matches the document root The fact that it begins with a slash means it specifies an absolute location other examples: –match=“*” – matches anything –match=“author” Can use ‘|’ as OR in a match

Navigating Trees Recall the source document is a tree Helpful to relate this to a directory tree on a file system Can access the parent or set of children Can make accesses relative or absolute Can also use wild-cards But careful: can be costly

Iteration XSL gives us two ways of iterating: is often to be preferred, since this leads to a design that employs more templates, but each one is smaller With for-each, the iteration is done in-line in the current template However, the for-each method looks like the for-loops we’re all familiar with, and can sometimes be easier to understand initially

‘Function Calling’ can be used to invoke a specific named template This can also specify parameters for the call In this way, can also iterate via recursive calls

One use of XPath expression is in specifying which templates to match from the context node Simplest usage is with no arguments: – This applies any matching templates to the child elements of the context node

contd A given element may have many different types of child element Apply templates allows us to select which children we want E.g. Looks only for author elements i.e. …

Sorting with XSL When applying templates, we can select a sort: This element can appear within a or element (i.e. the main ways of iterating over a set of nodes)

Sort Example - Greetings Greetings Greetings

Also Illustrate use of position().

Expressions Used to perform various useful functions Take arguments of various types And return single values of various types e.g. position() – returns the position of this node among its siblings in the document tree – is affected by in the way that you would normally want position() takes no arguments, and returns a value of type number

Datatypes Xpath and XSLT define five datatypes node-set: can contain any number of nodes (including zero) boolean : the value true or false number : a floating point number string : zero or more characters result tree fragment : gets created by certain XSLT statements

Predicates Boolean expressions in square brackets can be used to filter node sets Suppose we have language attributes: hello Then – can select all English greetings: Could also filter using conditional instructions

Conditionals The instruction evaluates its boolean expression, and if true, its child elements are processed e.g. This is the first sibling! Note that the tags could have been omitted – they just make the textual output stricter Has no ‘else’ clause

Use the instruction with if an else clause is needed … … …

Sorted and Numbered

Attributes Select attributes from source doc using notation Create attributes in result doc using Or by using {}: – link

Navigation - Axes The abbreviated notation is most commonly used to navigate the source document For example, select=“..” identifies the parent of the current node. The unabbreviated notation can also be used: select=“parent” Various other axes are defined to navigate on other criteria, such as ancestor, child, descendant, attribute etc.

Slides Example DTD, XML and XSL for very simple slides Illustrates creation of links within a document

Slides Example – DTD

Slides Example – XML A Short Presentation on XML Brief History of XML Derived from SGML Nearly as powerful Easier to Use

slide.xml continued…. XML Syntax An XML Document begins with some declarations The body consists of a tree of elements Each element consists of a begin-end tag pair Which may have other elements embedded within Elements may also have attributes Which must be defined within the opening tag

Slides Example – XSL This example shows illustrates a procedural XSL style, with few templates and heavy reliance on nested for loops Possible to create more elegant versions with a greater number of smaller, simpler templates

slides.xsl (cont) <xsl:stylesheet xmlns:xsl=" version="1.0" >

slides.xsl (cont) #Slide-

#Slide-

Slides Example Output

Some Design Guidelines Designing for ease of maintenance and greater re-use

Design Style Break the transform down into lots of simple templates This makes each template easier to debug Also fosters greater reuse

Further Design Principles However, under the added-value view, we should be careful to ensure that the XSL documents are well designed For example, keep them general, and ensure that any volatile data (in fact, probably any data!) is extracted from the XML source doc, rather than specified in the XSL transform doc. This is a footnote to the Once and Once Only principle of software engineering Not just once and once only, but also in the right place!

Root Glossary Template What’s wrong, and how would you fix it? Glossary of XML Terms Glossary of XML Terms

Sources of Confusion XSL can sometimes be confusing and hard to debug The main source of confusion arises from not knowing which templates are being applied - Since at any one time, there can be any number of possible matching templates The other problem is that because of the of the forgiving default template, XSLs that are buggy with respect to how you intended them to work, can still appear to work perfectly!!! Except that later on, an innocent change can break it in unexpected ways!

When to apply the transforms Client? Server-static? Server-dynamic?

In the client( browser) Takes load off the server BUT: only supported by recent browsers (e.g. IE 6) May slow down the browsing experience (more requests, more computation) – though you won’t notice this on simple examples

Server-static Here, we apply the transforms to create HTML pages, in advance of requests for those pages The client and server workload is now identical to if the HTML pages had been authored with an HTML editor BUT: the process requires careful management to keep the pages updated

Server-dynamic Here the pages are dynamically produced (i.e. transformed) in response to requests BUT: a significant workload on the server Content will be as up-to-date as the XML source documents (which may themselves be dynamically generated) Caching may be used to reduce the workload

Conclusions XSL is a technology for transforming XML source documents into a variety of output formats We’ve covered some of the most important features of it – but there are many instructions and functions that we’ve left out (see further reading) XSL is a good technology to use, providing what you want to do can be expressed easily XSL has limitations! Do not use XSL for complex calculations (beware even using it for simple ones!)

Further Reading XSLT: Mastering XML Transformations –Doug Tidwell, O’Reilly, 2001 XSLT Specification – XSLT Reference guide –