Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "XSLT Extensible Stylesheet Language Transformations CC432 / Short Course 507 Lecturer: Simon Lucas University of Essex Spring 2002."— Presentation transcript:

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

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

3 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!

4 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

5 !! 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

6 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

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

8 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

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

10 Without style sheet

11 With style sheet

12 The Transform Process

13 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.

14 Tree View hello.xml / greeting greetings hello holabonjour

15 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

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

17 Creating XSL Transforms

18 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

19 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

20 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

21 Hello -> text <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Which gives: hello bonjour hola!

22 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

23 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

24 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

25 (just the templates) Greetings Greetings

26 Output in a Browser

27 An identity transformation (elements only) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

28 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

29 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”

30 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

31 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

32 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

33 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

34 ‘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

35 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

36 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. …

37 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)

38 Sort Example - Greetings Greetings Greetings

39 Also Illustrate use of position().

40 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

41 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

42 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

43 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

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

45 Sorted and Numbered

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

47 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.

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

49 Slides Example – DTD

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

51 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

52 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

53 slides.xsl (cont) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

54 slides.xsl (cont) #Slide-

55 #Slide-

56 Slides Example Output

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

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

59 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!

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

61 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!

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

63 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

64 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

65 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

66 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!)

67 Further Reading XSLT: Mastering XML Transformations –Doug Tidwell, O’Reilly, 2001 XSLT Specification –http://www.w3.org/TR/xslthttp://www.w3.org/TR/xslt XSLT Reference guide –http://www.zvon.org/xxl/XSLTreference/Output/http://www.zvon.org/xxl/XSLTreference/Output/


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

Similar presentations


Ads by Google