Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 Extensible Stylesheet Language Transformations : XSLT.

Similar presentations


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

1 Lecture 14 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. 15) 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 template

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

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

14 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]

15 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

16 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 14 Extensible Stylesheet Language Transformations : XSLT."

Similar presentations


Ads by Google