UFCEWT20 3 Advanced Topics in Web Development 201213
UFCEWT-20 -3 Advanced Topics in Web Development 2012/13 Lecture 5 : Extensible Stylesheet Language Transformations : XSLT
Because XML can be used to represent any vocabulary (often defined by some schema), the question is how these different vocabularies can be processed and maybe transformed into something else. This something else may be another XML vocabulary (a common requirement in B 2 B scenarios), or it may be HTML (a common scenario for Web publishing). Using XSL Transformations (XSLT), mapping tasks can be implemented easily. XSLT leverages XPath's expressive power in a rather simple programming language, the programs are often called stylesheets. For easy tasks, XSLT mappings can be specified without much real programming going on, by simply specifying how components of the source markup are mapped to components of the target markup. Erik Wilde
XSLT : objectives and purpose (1) XSLT is an XML-oriented programming language XSLT uses XML as its syntax XSLT is a weakly typed language XSLT is not designed for large programming tasks XSLT is the standard language for XML-to-XML transformations o XSLT is very simple and often too simple o XSLT 2. 0 is much more complex and powerful o o o
XSLT : purpose & mode o XSLT is a functional programming language fundamentally different from the usual languages o not important for very simple mapping applications o important for writing more complex transformations o hard to get used to for procedurally trained people o XSLT has built-in behavior for tree traversal o XPaths allows you to select parts of the document tree o XSLT's default behavior is to traverse the complete tree o the idea of default behavior may seem strange
Example (well-formed) XML document (xml lecture) <? xml version="1. 0" encoding="UTF-8"? > <patient nhs-no="7503557856"> <name> <first>Joseph</first> <middle>Michael</middle> <last>Bloggs</last> <title>Mr</title> <previous /> <preferred>Joe</preferred> </name> <address> Repeating fields are fine in xml <street>2 Gloucester Road</street> documents – remember that in <street /> relational dbs, it is normal practice to decompose them <street /> out to another table <city>Bristol</city> <county>Avon</county> <postcode>BS 2 4 QS</postcode> </address> <tel> <home>0117 9541054</home> <mobile>07710 234674</mobile> </tel> <email>joe. bloggs@email. com</email> <fax /> </patient>
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
XSLT Document Model (showing parser) xpath engine
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. IE 7/8 or Netscape can load the stylesheet when the XML document is loaded.
XSLT Processing (again)
XSLT is a functional “ 4 gl” programming language A function maps one set of “things” onto another set of “things” using one or more rules. simple function: x 2 = y 1 2 3 4 5 - x 1 4 9 16 25 -‘ y simple xslt “function” or “template” rule when this pattern found in the input document <name>Reuben<name> output this <p>Hello Reuben</p> template <xsl: template match=“//name” /> <p>Hello <xsl: text> </xsl: text> <xsl: value-of select=“. ” /> </p> </xsl: template>
XSLT uses “XPath” to find nodes in a xml document Example 1: <name> <first>Joseph</first> <middle>Michael</middle> <last>Bloggs</last> <previous /> <preferred>Joe</preferred> </name> Example 2: <patient nhs-no="7503557856“> : : : </patient> xslt rule using xpath expression – match the <first> element <xsl: : template match = “//patient/name/first” > ……………. do something with content </xsl: template> xslt rule using xpath expressions (2) – get the value of the attribute named “nhs-no” in the <patient> element. <xsl: template match = “//patient”> <xsl: value of select =. /@nhs-no” /> </xsl: template>
XPath basics : o XPath is a language for addressing specific parts of an XML document. XPath, like the Document Object Model (DOM), models an XML document as a tree of nodes. o An XPath expression is a mechanism for navigating through and selecting nodes from the XML document. o An XPath expression is in a way analogous to a Structured Query Language (SQL) query used to select records from a database. o There are different types of nodes, including element nodes, attribute nodes and text nodes. XPath defines a way to compute a string-value for each type of node. o XPath defines a library of standard functions for working with strings, numbers and boolean expressions.
the tree view of example xml document patient name nhs-no 7503557856 tel address title fax Mr first middle last Joseph Michael Bloggs previous preferred Joe street 1 street 2 2 Gloucester Rd street 3 city county postcode Bristol Avon BS 2 4 QS home xpath is simply a way of finding specific nodes in a document tree – like files in a file hierarchy – e. g. “c: teachingmyfilesthisdoc. doc”. 01179541054 mobile 07710234674 KEY element content attribute
xpath has thirteen axis child parent descendent ancestor descendent-of-self ancestor-of-self following-sibling preceding-sibling following preceding attribute namespace self xpath axes (node sets)
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.
Hello World in XSLT o XSLT always transforms an XML document this is hard-coded in the XSLT Processing Model o Simply generating output is impossible - hello world therefore ignores the input o anything can be the input (including the XSLT itself) <? xml version="1. 0" encoding="UTF-8"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> <xsl: text>Hello World!</xsl: text> </xsl: template> </xsl: stylesheet>
example xslt stylesheet “patient. xslt” (1) <? xml version="1. 0" encoding="UTF-8"? > <patient nhs-no="7503557856"> <name> <first>Joseph</first> <middle>Michael</middle> <last>Bloggs</last> <previous /> <preferred>Joe</preferred> </name> <title>Mr</title> <address> <street>2 Gloucester Road</street 1> <street /> <city>Bristol</city> <county>Avon</county> <postcode>BS 2 4 QS</postcode> </address> <tel> <home>0117 9541054</home> <mobile>07710 234674</mobile> </tel> <email>joe. bloggs@email. com</email> <fax /> </patient> stylesheet “patient. xslt” used to generate HTML
example xslt stylesheet (2) minimum stylesheet: <? xml version="1. 0" encoding="UTF-8"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> </xsl: stylesheet> the xslt parser (msxml) that “built into” internet explorer applies the above stylesheet
xslt stylesheet fragment : from “patient. xslt” (3) start the template fragment 1 output a table <xsl: template match="//patient"> <html> <head><title>Patient Record XSL Transformation Example</title> </head> <body> <h 3>Patient Record</h 3> <table border="1" cellpadding="4" cellspacing="0" bordercolor="#cccccc" width="400"> <tr><td width="100">NHS Number</td> <xsl: value-of select=". /@nhs-no" /> </td> </tr> <xsl: apply-templates /> </table> </body> apply all other templates output: <html> <head><title>Patient Record XSL Transformation Example</title> </head> <body> <h 3>Patient Record</h 3> <table border="1" cellpadding="4" cellspacing="0" bordercolor="#cccccc" width="400"> <tr><td width="100">NHS Number</td> <td>7503557856 </td></tr> [RESULT FROM APPLYING ALL OTHER TEMPLATES] </html> </xsl: template> output 1 row with two columns -write “NHS Number” in first column - write the value of the attrubute in the second column end the template </table> </body> </html>
xslt stylesheet fragment : from “patient. xslt” (4) fragment 2 – make a selection <xsl: template match="//fax"> <tr> <td align="left" valign="top">Fax</td> <xsl: choose> <xsl: when test="not(node())"> <xsl: text>-</xsl: text> </xsl: when> <xsl: otherwise> <xsl: value-of select=". " /> </xsl: otherwise> </xsl: choose> </td> </tr> </xsl: template> output <a href="mailto: joe. bloggs@email. com">joe. bloggs@email. com</a> test if the context node (“self”) is empty if it is write “-” otherwise output the content string of the context node; represented by “. ” fragment 3– output a html hyperlink <xsl: template match="//email"> <tr> <td align="left" valign="top">Email</td> <a> <xsl: attribute name="href"> mailto: <xsl: value-of select=". " /> </xsl: attribute> <xsl: value-of select=". " /> </a> </td> </tr> </xsl: template>
example xslt stylesheet : from “patient. xslt” (5) fragment 4 – do a while loop (iteration) <xsl: for-each select="//street"> <xsl: if test="node()“> <xsl: value-of select=". "/><br/> </xsl: if> </xsl: for-each> 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 )
- Slides: 21