Session II Chapter 14 – XSLT

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
XSLT (eXtensible Stylesheet Language Transformation) 1.
XSL XSLT and XPath 11-Apr-17.
XML: Managing Data Exchange Stylesheets. Lesson Contents CSS The basic XSL file XSL transforms Templates Sort Numbering Parameters and Variables Datatypes.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
Recycled from Gill Windall’s notes
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C standards for stylesheets – CSS – XSL: Extensible Markup.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
1 cs XSL XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
CIS 451: XSL Dr. Ralph Westfall February, Problems With XML no formatting capabilities contra formatting tags like, etc. in HTML CSS can be used.
WORKING WITH XSLT AND XPATH
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Session II Chapter 2 – Chapter 2 – XSLhttp://
Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
Extensible Stylesheet Language Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XSL-FO XSLT.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Presentation Topic: XML and ASP Presented by Yanzhi Zhang.
Lecture 11 XSL Transformations (part 1: Introduction)
Session I Chapter 1 - Introduction to Web Development
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
1 cs XSL XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be.
Session 1 Chapter 1 - Introduction to Web Development ITI 133: HTML5 Desktop and Mobile Level I
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
Session IV Chapter 14 – Chapter 14 – XSLThttp://
XSLT: The XML transformation language
XSLT Introduction.
XSL Transformations (XSLT)
In this session, you will learn to:
XSLT: XML Transformation
XML Introduction Bill Jerome.
Unit 4 Representing Web Data: XML
Displaying XML Data with XSLT
Reference: cit /Lectures/xslt.ppt
Using Extension Elements and Extension Functions with XSLT and XPath
eXtensible Stylesheet Language
XSLT 1.0.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Session II Chapter 2 - XSLT
Chapter 7 Representing Web Data: XML
Applying eXtensible Style Sheets (XSL)
HTML5 Level I Session I Chapter 1 - Introduction to Web Development
XML WITH CSS.
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
WHAT IS XSLT?.
XSL XSLT and XPath 24-Feb-19.
CMP 051 XML Introduction Session IV Chapter 12 – XML Namespaces
XSL XSLT and XPath.
New Perspectives on XML
7 September 1999 The Basics of XSLT Assuming a basic knowledge of XML and XML Namespaces Thomas G. Habing Grainger Engineering Library Information Center.
CMP 051 XML Introduction Session IV Chapter 12 – XML Namespaces
eXtensible Markup Language XML
Unit 6 - XML Transformations
Presentation transcript:

Session II Chapter 14 – XSLT http://www.profburnett.com CMP 051 XML Introduction Session II Chapter 14 – XSLT http://www.profburnett.com

Outline XSLT Introduction XSLT Browser Support Creating 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

XSLT Introduction XSL-FO XSL 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

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

Creating Simple Stylesheet <?xml version="1.0"?> <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="2.0"> <head> <title>Wonders of the World</title> </head> <body> <p>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 <strong>Seven Wonders of the Ancient World</strong>.</p> <p>These ancient wonders are <xsl:for-each select="ancient_wonders/wonder/name[@language='English']"> <xsl:value-of select="."/> <xsl:choose> <xsl:when test="position()=last()">.</xsl:when> <xsl:when test="position()=last()-1">, and </xsl:when> <xsl:otherwise>, </xsl:otherwise> </xsl:choose> </xsl:for-each> </p> </body> </html> 8/1/2014 Copyright © Carl M. Burnett

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: http://www.w3.org/2005/xpath-functions 8/1/2014 Copyright © Carl M. Burnett

XSLT - User Defined Functions (UDF) <?xml version="1.0"?> <xsl:stylesheet hmlns:xsl="http:// www.w3.org/1999/XSL/Transform" version="2.0" xmlns:khg="http:// www.kehogo.com/ns/khg" exclude-result-prefixes="khg"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Hello World</title></head> <body> <xsl:value-of select="khg:helloWorld()"/> </body> </html> </xsl:template> <xsl:function name="khg:helloWorld"> Hello World on <xsl:value-of select=“ format-dateTime(current-dateTime(), '[M01]/[D01]/[Y0001] at [H01]:[m01]:[s01]')"/> </xsl:function> </xsl:stylesheet> You declare UDF’s name, input, what is does with the input, and its output. Declare UDF Function - <xsl:function Declare the function name - name=“UDF_Name“ Declare any UDF Parameters – <xsl:param name=“param_name”/> Close out the UDF function – </xsl:function> 8/1/2014 Copyright © Carl M. Burnett

Calling UDF You call a UDF anywhere an XPath expression is expected. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http:// www.w3.org/1999/XSL/Transform" version="2.0" xmlns="http:// www.w3.org/1999/xhtml" xmlns:khg="http://www.kehogo.com/ns/khg" exclude-result-prefixes="khg"> … <xsl:function name="khg:third"> <xsl:param name="dimension"/> <xsl:value-of select=“ (ceiling($dimension div 3))"/> </xsl:function> Type – prefix: Type – UDF _Name( Type the value(s) of the input parameters separated by a comma. 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:template> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:template> Element The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document). 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:value-of> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node. 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:for-each> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:for-each> Element The <xsl:for-each> element allows you to do looping in XSLT. 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:sort> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:sort> Element The <xsl:sort> element is used to sort the output. 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:if> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:if> Element The <xsl:if> element is used to put a conditional test against the content of the XML file. 8/1/2014 Copyright © Carl M. Burnett

XSLT - Elements XSLT <xsl:choose> Element <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSLT <xsl:choose> Element The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. 8/1/2014 Copyright © Carl M. Burnett

Generating Multiple Documents With XSLT 2.0 you can generate multiple documents from a single process. <xsl:result-document href="history.html" method="xhtml"> <html> <head><title>Wonders of the World</title></head> <body> <h2>History</h2> <xsl:apply-templates select="ancient_wonders/wonder/history"> <xsl:sort select="year_built" order="descending" data-type="number" /> </xsl:apply-templates> </body> </html> </xsl:result-document> 1. Type – <xsl:result-document to begin the output declaration. 2. Type – href=“output.uri’> where the URI identifies the location of document where the output for these XSLT instruction are located. 3. Type </xsl:result-document> to close out the output instructions. 8/1/2014 Copyright © Carl M. Burnett

Validating XSLT Output <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:import-schema schema-location="14-13.xsd"/> <xsl:template match="/"> <sources xsl:validation="strict"> <xsl:for-each-group select="//source" group- by="@newspaperid"> <newspapers> <newspaperid> <xsl:value-of select="@newspaperid"/> </newspaperid> <sectionid> <xsl:value-of select="current- group()/@sectionid"/> </sectionid> </newspapers> </xsl:for-each-group> </sources> </xsl:template> </xsl:stylesheet> With XSLT 2.0 you can validate those output of a XSLT transformation against the XML schema. Type – <xsl:import-schema 2. Type – schema-location=“schema.uri”/> where the URI identifies the location of XML Schema you want to use for validation. 3. Type in the root element of the output you want to validate: xsl:validation=:”strict” 8/1/2014 Copyright © Carl M. Burnett

Generating XHTML Output Server-side XSL Transformations Browser requests dynamic page Web server finds page and passes it to application server Application server scans page for instructions and gets XSLT fragment Application server performs transformation (reads XSLT fragment, gets and formats xml data) Application server inserts transformed fragment into page and passes it back to the web server Web server sends finished page to browser PHP or ASP Code performs the XML to XHTML Transformation on the server. 8/1/2014 Copyright © Carl M. Burnett

Generating XHTML Output Client-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

Generating XHTML Output - Linked File on Client XSL File – 14-03.xsl XML File <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xhtml"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1 align="center">Seven Wonders of the Ancient World</h1> <p align="center"><img src="herodotus.jpg" width="120" height="171" /></p> <p>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 <strong>Seven Wonders of the Ancient World</strong>. </p> <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="14-03.xsl"?> <ancient_wonders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <wonder> <name language="English">Colossus of Rhodes</name> <name language="Greek">Κολοσσός της Ρόδου</name> <location>Rhodes, Greece</location> <height units="feet">107</height> <history> <year_built era="BC">282</year_built> <year_destroyed era="BC">226</year_destroyed> <how_destroyed>earthquake</how_destroyed> <story> In 294 BC, …………… </story> </history> <main_image file="colossus.jpg" w="528" h="349"/> <source sectionid="101" newspaperid="21"/> </wonder> Example XML File 8/1/2014 Copyright © Carl M. Burnett

Generating XHTML Output – JavaScript on Client 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 <html> <head> <script> 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);   } } </script> </head> <body onload="displayResult()"> <div id="example" /> </body> </html> Example XML File 8/1/2014 Copyright © Carl M. Burnett

Generating XHTML Output – ASP/PHP on Server Transforming 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

Outline XSLT Introduction XSLT Browser Support Creating 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