Extensible Stylesheet Language Transformations XSLT Example wellformed XML

Extensible Stylesheet Language Transformations : XSLT

Example (well-formed) XML document (xml lecture) repeating “street” element. <? 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> <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>

Example document with validation (Lec. 15) patient. xml <? xml version="1. 0" encoding="UTF-8"? > <patient nhs-no="7503557856“ xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: no. Namespace. Schema. Location="patient. xsd"> <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> “patient. xml” is now an instance document in the vocabulary defined in the schema “patient. xsd” patient. xsd <? xml version="1. 0" encoding="UTF-8" ? > <xs: schema element. Form. Default="qualified“ attribute. Form. Default="unqualified“ xmlns: xs="http: //www. w 3. org/2001/XMLSchema"> <xs: element name="patient“> <xs: complex. Type> <xs: sequence> <xs: element name="name" type="name. Type" /> <xs: element name="title" type="title. Type" /> <xs: element name="address" type="address. Type" /> <xs: element name="tel" type="tel. Type" max. Occurs="2" /> <xs: element name="email" type="email. Type" min. Occurs="0" /> <xs: element name="fax" type="xs: string" min. Occurs="0" /> </xs: sequence> <xs: attribute name="nhs-no" type="xs: integer" use="required" /> </xs: complex. Type> </xs: element> <xs: complex. Type name="name. Type"> <xs: sequence> <xs: element name="first" type="name. String. Type" /> <xs: element name="middle" type="name. String. Type" /> <xs: element name="last" type="name. String. Type" /> <xs: element name="previous" type="name. String. Type" /> <xs: element name="preferred" type="name. String. Type" /> </xs: sequence> </xs: complex. Type>. . . </xs: schema>

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

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.

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: 16