1 Session II Chapter 14 – XSLT http://www.profburnett.comCMP 051 XML Introduction Session II Chapter 14 – XSLT
2 Outline XSLT Introduction XSLT Browser SupportCreating Simple Stylesheet XSLT Functions XSLT User Defined Functions Calling UDF XSLT Elements Generating Multiple Documents Validating XSLT Output Generating XHTML Output Generating XHTML Output on the Client Generating XHTML Output on the Server 8/1/2014 Copyright © Carl M. Burnett
3 XSLT Introduction XSL-FOXSL stands for EXtensible Stylesheet Language. CSS = Style Sheets for HTML XSL = Style Sheets for XML XSL - More Than a Style Sheet Language XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents XSLT = XSL Transformations XQuery XPointer XLink XPath XSLT XSL-FO 8/1/2014 Copyright © Carl M. Burnett
4 XSLT Browser Support Internet Explorer supports XML, XSLT, and XPath from version 6. Chrome supports XML, XSLT, and XPath from version 1. Firefox supports XML, XSLT, and XPath from version 3. Safari supports XML and XSLT from version 3. Opera supports XML, XSLT, and XPath from version 9. 8/1/2014 Copyright © Carl M. Burnett
5 Creating Simple Stylesheet The famous Greek historian Herodotus wrote of seven great architectural achievements. And although his writings did not survive, he planted seeds for what has become the list of the Seven Wonders of the Ancient World. These ancient wonders are
6 XSLT - Functions XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library. XSLT includes over 100 built-in functions. Functions Reference The default prefix for the function namespace is fn: The URI of the function namespace is: 8/1/2014 Copyright © Carl M. Burnett
7 XSLT - User Defined Functions (UDF)
8 Calling UDF You call a UDF anywhere an XPath expression is expected.
9 XSLT - Elements XSLT My CD Collection
Title Artist .
10 XSLT - Elements XSLT My CD Collection
Title Artist
11 XSLT - Elements XSLT My CD Collection
Title Artist
12 XSLT - Elements XSLT My CD Collection
Title Artist
13 XSLT - Elements XSLT My CD Collection
Title Artist
14 XSLT - Elements XSLT My CD Collection
Title Artist
15 Generating Multiple DocumentsWith XSLT 2.0 you can generate multiple documents from a single process. History
16 Validating XSLT Output
18 Generating XHTML OutputClient-side XSL Transformations Browser requests XML file Server responds by sending XML file to browser Browser reads XML directive and calls XSLT file Server sends XSLT file to browser Browser transforms XML data and displays it in browser 8/1/2014 Copyright © Carl M. Burnett
19 Generating XHTML Output - Linked File on ClientXSL File – xsl XML File The famous Greek historian Herodotus wrote of seven great architectural achievements. And although his writings did not survive, he planted seeds for what has become the list of theSeven Wonders of the Ancient World

20 Generating XHTML Output – JavaScript on ClientMore versatile solution is to use a JavaScript to do the transformation. Using a JavaScript, we can: do browser-specific testing use different style sheets according to browser and user needs JavaScript Converts XML file to XHTML file in the client browser The loadXMLDoc() function does the following: Create an XMLHttpRequest object Use the open() and send() methods of the XMLHttpRequest object to send a request to a server Get the response data as XML data Example XML File 8/1/2014 Copyright © Carl M. Burnett { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://slideplayer.com/12256619/72/images/20/Generating+XHTML+Output+%E2%80%93+JavaScript+on+Client.jpg", "name": "Generating XHTML Output – JavaScript on Client", "description": "More versatile solution is to use a JavaScript to do the transformation. Using a JavaScript, we can: do browser-specific testing. use different style sheets according to browser and user needs. JavaScript Converts XML file to XHTML file in the client browser. The loadXMLDoc() function does the following: Create an XMLHttpRequest object. Use the open() and send() methods of the XMLHttpRequest object to send a request to a server. Get the response data as XML data. function loadXMLDoc(filename) { if (window.ActiveXObject) { xhttp = new ActiveXObject( Msxml2.XMLHTTP ); } else { xhttp = new XMLHttpRequest(); } xhttp.open( GET , filename, false); try {xhttp.responseType = msxml-document } catch(err) {} // Helping IE11 xhttp.send( ); return xhttp.responseXML; } function displayResult() { xml = loadXMLDoc( cdcatalog.xml ); xsl = loadXMLDoc( cdcatalog.xsl ); // code for IE if (window.ActiveXObject || xhttp.responseType == msxml-document ) { ex = xml.transformNode(xsl); document.getElementById( example ).innerHTML = ex; } // code for Chrome, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml, document); document.getElementById( example ).appendChild(resultDocument); } } Example XML File. 8/1/2014. Copyright © Carl M. Burnett.", "width": "1024" }
21 Generating XHTML Output – ASP/PHP on ServerTransforming XML in the Browser will not work in a browser that doesn't have an XML parser. Transformation must be done on the server. Data is sent back to the browser as XHTML. PHP Code: Transform XML to XHTML on the Server ASP Code: Transform XML to XHTML on the Server Example File Example File 8/1/2014 Copyright © Carl M. Burnett
22 Outline XSLT Introduction XSLT Browser SupportCreating Simple Stylesheet XSLT Functions XSLT User Defined Functions Calling UDF XSLT Elements Generating Multiple Documents Validating XSLT Output Generating XHTML Output Generating XHTML Output on the Client Generating XHTML Output on the Server Next: Chapter 5 - XSL-FO 8/1/2014 Copyright © Carl M. Burnett