1 cs336607. 2 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.

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.
XSL XSLT and XPath 11-Apr-17.
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
XML Unit 6 October 31. XML, review XML is used to markup data Used to describe information Uses tags like HTML –But all tags are user-defined –Must be.
1 XSL – XML Stylesheet Language. 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
XSL Concepts Lecture 7. XML Display Options What can XSL Transformations do? generation of constant text suppression of content moving text (e.g., exchanging.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
1 XPATH Modified Slides from Dr. Sagiv. 2 XPath A Language for Locating Nodes in XML Documents XPath expressions are written in a syntax that resembles.
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)
SD2520 Databases using XML and JQuery
Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XSL XML Stylesheet Langauage. XPath Notation for addressing elements in an XML document /xyz - selects the root element /xyz/abc - selects all elements.
Introduction technology XSL. 04/11/2005 Script of the presentation Introduction the XSL The XSL standard Tools for edition of codes XSL Necessary resources.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
WORKING WITH XSLT AND XPATH
1 XML Data Management 7. XLST Werner Nutt. Kernow and Saxon To demo XSLT, we use Kernow –Kernow is a (graphical) front end for Saxon Saxon is an XSLT,
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
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.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
Lecture 11 XSL Transformations (part 1: Introduction)
Waqas Anwar Next SlidePrevious Slide. Waqas Anwar Next SlidePrevious Slide XML XML stands for EXtensible Markup Language.
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
ITB Web programming for E- Commerce 1 ITB6227 Programming for E-COMMERCE Lecture Presentation of XML Documents.
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.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
1 XML Data Management Extracting Data from XML: XPath Werner Nutt based on slides by Sara Cohen, Jerusalem.
Dr. Chunbo Chu Week 3. XML Not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to transport and store data,
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
1 XML Data Management XLST Werner Nutt. A Hello World! Stylesheet world.
1 XMLXSL(T) and CSS NOEA/PQC (rev. fen) 2007 Stylesheets CSS:Cascading Style Sheets XSL(T):eXtended Stylesheet Language (Transformations)
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
1 XPath Extracting Data from XML. 2 Data stored in an XML document must be extracted to use with various applications Data can be extracted programmatically.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
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.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
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.
Session IV Chapter 14 – Chapter 14 – XSLThttp://
Unit 4 Representing Web Data: XML
Chapter 7 Representing Web Data: XML
Modified Slides from Dr. Sagiv
Modified Slides from Dr. Sagiv
Presentation transcript:

1 cs336607

2 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 taught XSLT (transformation of documents) XSLFO (FO for formatting objects) This is a rather complex language for typesetting (i.e., preparing text for printing) It will not be taught cs336607

3

4 XSLT XSLT is a language for transforming XML documents into other XML documents For example, XHTML, RSS, KML, GML, MathML, WML Can also transform XML to text documents, e.g., SQL programs An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents cs336607

5 XSLT Processors XSLT Processor cs336607

6 Web Page Layout Doc. Structure Data Web Pages – The Whole Picture XML XSLXHTML Style Knowledge CSS cs336607

7 Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml cs336607

8 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl cs336607

9 Applying XSL Stylesheets to XML There are several ways of applying an XSL stylesheet to an XML document: Directly applying an XSLT processor to the XML document and the XSL stylesheet Calling an XSLT processor from within a program Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation The resulting XHTML document is shown instead of the original XML cs336607

10 Processing XSL in Java You can use the XALAN package of Apache in order to process XSL transformations java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html cs336607

11 How Does XSLT Work? An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML) Each template has a match attribute that specifies to which source nodes the template can be applied Each source node has at a template that matches it The current source node is processed by applying a template that matches this node When processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node The XSLT processor processes the document root ( / ) cs336607

12 Templates A template has the form... The content of a template consists of XML elements (e.g., XHTML) and text that are copied to the result XSL elements ( ) that are actually instructions The pattern syntax is a subset of XPath cs336607

13 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! catalog1.xsl cs336607

14 Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml cs336607

15 The Result cd catalog This is a cd catalog! In XALAN, automatically added to cs336607

16

17 Examples of Match Attributes match="cd", All elements with tag name cd match="//cd", match="/catalog/cd/artist" All matches of the absolute XPath match="cd/artist" All artist nodes that have a cd parent match="catalog//artist" All artist nodes that have a catalog ancestor cs336607

18 Processing starts by applying a template to the root If no specified template matches the root, then one is inserted by default (see the next slide) The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node It is done by putting inside a template the instruction: In xpath, the current processed node is used as the context node Without the select attribute, this instruction processes all the children of the current node (including text nodes) <xsl:apply-templates> cs336607

19 Default Templates XSL provides implicit built-in templates that match every element and text nodes Templates we write always override these built-in templates (when they match) cs336607

20 <xsl:stylesheet version="1.0" xmlns:xsl=" Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 cs In XALAN and in IE (it yields an empty page in Firefox)

21 <xsl:stylesheet version="1.0" xmlns:xsl=" A cd! A cd! cs336607

22 <xsl:stylesheet version="1.0" xmlns:xsl=" A cd! A cd! Aretha: Lady Soul Aretha Franklin 9.90 cs336607

23 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates An artist! An artist! cs336607

24 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates An artist! cs Does not match the context node

25 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates mode=“first” <xsl:apply-templates mode=“second” An artist! The artist is coming back! cs336607

26

27 The Most Frequently Used Elements of XSL This element extracts the value of a node from the nodelist located by xpath This element loops over all the nodes in the node list located by xpath,, etc. This element is for conditional processing cs336607

28 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog [ ] Currently selected element is the context (current) node Example 1 catalog2.xsl cs336607

29

30 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog Example 2 catalog3.xsl cs336607

31 : ( now on sale: $ ) Example 2 (cont.) Entities replace characters price<10 → price<10 cs336607

32 ( Special price! ) ( Good price! ) (Normal price.) Example 3 : Switch syntax for conditions cs336607

33 The Element The element is used to sort the list of nodes that are looped over by the element Thus, the must appear inside the element The looping is done in sorted order cs336607

34 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog CDs are iterated in ascending order of the titles catalog4.xsl cs336607

35 Setting Values in Attributes The element cannot be used within attribute value However, we can insert expressions into attribute values, by putting the expression inside curly braces ( {} ) Alternatively, we can use in order to construct XML elements cs336607

36 An Example In the following example, we add to each CD entitled t a link to the URL /showcd.jsp?title=t : cs336607

37

38 Using : /showcd.jsp?title= cs336607

39 On XSL Code Typically, an XSLT program can be written in several, very different ways Templates can sometime replace loops and vice versa Conditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versa A matter of convenience and elegancy cs336607

40 On Recursive Templates It is not always possible to avoid recursive templates That is, use only the template of the root Suppose that we want to write an XSL stylesheet that generates a copy of the source document It is rather easy to do it when the structure of the source XML document is known Can we write an XSL stylesheet that does it for every possible XML document? Yes! (see next slide) cs336607

41 <xsl:stylesheet xmlns:xsl=" version="1.0"> Identity Transformation Stylesheet cs336607

42 Generating Valid XHTML By default, the documents that XSL stylesheets generate are not valid XHTML Next, we will show how XSL stylesheet can be changed in order to generate valid XHTML cs336607

43 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! The Original XSL Example cs336607

44 cd catalog This is a cd catalog! Uppercase tag name, unclosed element No DOCTYPE The Original Transformation Result cs336607

45 <xsl:stylesheet version="1.0" xmlns:xsl=" xmlns=" <xsl:output method="html" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " "/> … Modifying the XSL Example cs336607

<xsl:output method="xml|html|text|name" version="string" encoding="string" omit-xml-declaration="yes|no" standalone="yes|no" doctype-public="string" doctype-system="string" cdata-section-elements="namelist" indent="yes|no" media-type="string"/> cs

47 cd catalog This is a cd catalog! META is not inserted The Transformation Result cs336607

48 Some Other XSL Elements The element inserts free text in the output The creates a copy of the specified nodes (deep copy, i.e., copies the entire subtree) The creates a copy of the specified nodes (does not copy children or attributes) The element creates a comment node in the result tree The element defines a variable (local or global) that can be used within the program cs336607

Example Transform this list of number to be Sorted Alternatingly red and blue cs

cs

cs

cs Example of Using a Variable

Using Variables and Functions Using names, templates can be called directly and serve as functions It is possible to use variables and function in XSL to perform computations Returned values of functions can be set to variables v=f(3)

Computing Fibonacci Numbers 8 Given value Style sheet

if n<=2 return 1 f1=fib(n-1)f2=fib(n-2) return f1+f2

Fibonacci The Fibonacci number at position is val = the value in return fib(val)

Transforming XML to XHTML, in the Browser Use JavaScript Check what is the current browser before applying the transformation (different browsers use a slightly different DOM) cs

cs function loadXMLDoc(fname) { var xmlDoc; // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=true; xmlDoc.load(fname); return(xmlDoc); } XML to XHTML in Browser

cs function displayResult() { xml=loadXMLDoc("cdcatalog.xml"); xsl=loadXMLDoc("cdcatalog.xsl"); // code for IE if (window.ActiveXObject) { ex=xml.transformNode(xsl); document.getElementById("example").innerHTML=ex; } // code for Mozilla, 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); } XML to XHTML in Browser

Applying Xalan from Java Programs The following example illustrate how to apply Xalan on a given XML and XSL from within a Java program cs

cs // Instantiate a DocumentBuilderFactory. javax.xml.parsers.DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); // Use the DocumentBuilderFactory to provide access to a DocumentBuilder. javax.xml.parsers.DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); // Use the DocumentBuilder to parse the XML input. org.w3c.dom.Document inDoc = dBuilder.parse("foo.xml"); 1.Generate a document builder 2.Parse the XML document

cs // Generate a Transformer. javax.xml.transform.Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource("foo.xsl")); // Create an empy DOMResult object for the output. javax.xml.transform.dom.DOMResult domResult = new javax.xml.transform.dom.DOMResult(); // Perform the transformation. transformer.transform(new javax.xml.transform.dom.DOMSource(inDoc), domResult); // Now you can get the output Node from the DOMResult. org.w3c.dom.Node node = domResult.getNode(); 1.Generate a transformer 2.Create an empty DOM result 3.Apply the transformation 4.Get the output node transformer.transform(new DOMSource(inDoc), new StreamResult(new FileOutputStream(“result.html"))); To produce HTML, replace

Links W3C Recommendation: tml tml cs