Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Extensible Stylesheet Language Transformations : XSLT.

Similar presentations


Presentation on theme: "Lecture 15 Extensible Stylesheet Language Transformations : XSLT."— Presentation transcript:

1 Lecture 15 Extensible Stylesheet Language Transformations : XSLT

2 Example (well-formed) XML document (Lec. 13) Joseph Michael Bloggs Joe Mr 2 Gloucester Road Bristol Avon BS2 4QS 0117 9541054 07710 234674 joe.bloggs@email.com repeating “street element.

3 Example document with validation (Lec. 14) Joseph Michael Bloggs Joe Mr 2 Gloucester Road Bristol Avon BS2 4QS 0117 9541054 07710 234674 joe.bloggs@email.com <xs:schema elementFormDefault="qualified“ attributeFormDefault="unqualified“ xmlns:xs="http://www.w3.org/2001/XMLSchema">. patient.xml patient.xsd “patient.xml” is now an instance document in the vocabulary defined in the schema “patient.xsd”

4 XSLT XSLT – Extensible Stylesheet Language Transformations is an application for specifying rules which transform one XML document into another document. It uses template rules in the stylesheet to match patterns in the input document and when a match is found it writes the template from the rule to the output tree. Basic XSLT processing model

5 XSLT Document Model (showing parser) xpath engine

6 XSLT Parser Processing Model Both the source document and XSLT stylesheet are loaded into the processor's memory. How this happens is dependent on the implementation. One option is that both are loaded as DOM documents under the control of a program. Another option is that the stylesheet is referenced by a processing instruction in the source XML document. IE5/6 or Netscape6 can load the stylesheet when the XML document is loaded.

7 XSLT is a functional “4gl programming” language A function maps one set of “things” onto another set of “things” using one or more rules. simple function: x 2 = ysimple xslt “function” or “template” rule 12345---12345--- 1 4 9 16 25 - -‘ x y when this pattern found in the input document Reuben output this Hello Reuben Hello OUTPUT

8 XSLT uses “XPath” to find nodes in a xml document Joseph Michael Bloggs Joe Example 1: ……………. do something with content xslt rule using xpath expression – match the element Example 2: : =./@nhs-no xslt rule using xpath expressions (2) – get the value of the attribute named “nhs-no” in the element.

9 patient name title address fax tel first middle last previous preferred street1 street2 street3 city county postcode home mobile JosephMichaelBloggs Joe 2 Gloucester Rd BristolAvon BS2 4QS 01179541054 07710234674 Mr nhs-no 7503557856 KEY element content attribute the tree view of example xml document xpath is simply a way of finding specific nodes in a document tree – like files in a file hierarchy – e.g. “c:\teaching\myfiles\thisdoc.doc”.

10 xpath axes (node sets) xpath has thirteen axis child parent decendent ancestor descendent-of-self ancestor-of-self following-sibling preceding-sibling following preceding attribute namespace self

11 xslt & push and pull models of document processing push model - source document controls the structure – e.g cascading style sheet (CSS) – applies a style but cannot change the structure of the input document. pull model – the stylesheet controls the structure and the source documents acts as the data source. xslt can apply both the push and pull model - you can write a xslt stylesheet to change the order of elements, do calculations based on the number of elements (using xpath), do branching depending on an element value, generate other stylesheets, write java or c# code or source code for any other language, use svg to generate graphics, apply formatting object (fo) constructs that tell a fo-processor to lay out pages for printing or write pdf and almost everything else supported by other programming languages. hence it has all the constructs to apply our fundamental ‘Jackson’ concepts of sequence, selection and iteration.

12 XSLT parser processing API’s and the push/pull models Tree Based – these parsers read the source and style documents and build a tree in memory of all nodes. Often some indexing mechanism will be applied. The trees can then be processed in any order since all nodes are available once the tree has been built. Examples include the Document Object Model (DOM) based API’s such as that used in Apache Xerces DOM API.Document Object Model (DOM) Apache Xerces Distadvantages of tree based API models is that because they often load the entire XML document into memory – they can require an awful lot of memory. Sometimes a hundred times as much as the document itself. This makes this model unwieldy for very large documents. Push Model – or the producer/consumer model. These parsers control the pace of the application by parsing the producer (the xml source doc) and informing the consumer (the application program) when certain events occur. The classic example is the SAX (Simple Model for XML) API. The best known SAX implementation is Apache Xerces SAX API.SAXApache Xerces Advantages include the fact that the whole document need not exist in memory all at once. But jumping to various places in the document is hard and must be handled by the programmer. Pull Model - the API requests events from the producer (xml source doc) rather than waiting for these events to occur. A notable pull model parser is found in the XMLReader class in the.NET framework. Typically a loop is created that continually reads from the xml document until the end but acts on open items as they are seen.XMLReader Curser Model - the newest class of xml parsing API. The cursor acts as a lens on the source document but unlike push/pull models the cursor can jump to anywhere in the document. Thus it has all the advantages of the tree based model but without the massive memory overhead. The ObjectXpathNavigator class in the.NET frameowrk implements a cursor based API. ObjectXpathNavigator

13 Joseph Michael Bloggs Joe Mr 2 Gloucester Road Bristol Avon BS2 4QS 0117 9541054 07710 234674 joe.bloggs@email.com stylesheet “patiemt.xslt” used to generate HTML example xslt stylesheet “patient.xslt” (1)

14 example xslt stylesheet (2) minimum stylesheet: the xslt parser (msxml) that “built into” internet explorer applies the above stylesheet

15 xslt stylesheet fragment : from “patient.xslt” (3) fragment 1 Patient Record XSL Transformation Example Patient Record <table border="1" cellpadding="4" cellspacing="0" bordercolor="#cccccc" width="400"> NHS Number start the template end the template output a table output 1 row with two columns -write “NHS Number” in first column - write the value of the attrubute in the second column apply all other templates output: Patient Record XSL Transformation Example Patient Record <table border="1" cellpadding="4" cellspacing="0" bordercolor="#cccccc" width="400"> NHS Number 7503557856 [RESULT FROM APPLYING ALL OTHER TEMPLATES]

16 xslt stylesheet fragment : from “patient.xslt” (4) fragment 2 – make a selection Fax - test if the context node (“self”) is empty if it is write “-” otherwise output the content string of the context node; represented by “.” Email mailto: fragment 3– output a html hyperlink output joe.bloggs@email.com

17 example xslt stylesheet : from “patient.xslt” (5) fragment 4 – do a while loop (iteration) cycle through all the “street” nodes and if not empty then output any content full version of the “patient.xslt” file can be found with the other example files “patient.xml” now points to it ( notice how Internet Explorer applies the stylesheet )


Download ppt "Lecture 15 Extensible Stylesheet Language Transformations : XSLT."

Similar presentations


Ads by Google