XSLT: XML Transformation

Slides:



Advertisements
Similar presentations
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advertisements

Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create structured documents XML vs HTML ◦XML is designed to transport.
XSL XSLT and XPath 11-Apr-17.
XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
1 XSL – XML Stylesheet Language. 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO.
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
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)
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain.
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.
Julien Thibault  HTML is the basic building-blocks of webpages  It is not a language!! (despite its name)  Structure text/media.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
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 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
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.
Lecture 11 XSL Transformations (part 1: Introduction)
XML 2nd EDITION Tutorial 1 Creating An Xml Document.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
XP 1 Creating an XML Document Developing an XML Document for the Jazz Warehouse XML Tutorial.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
XSL and XSLT 06. XSL and XSLT © Aptech Limited Introduction to XSL  In this first lesson, Introduction to XSL, you will learn to:  Define XSL, XSLT,
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.
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.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
1 XMLXSL(T) and CSS NOEA/PQC (rev. fen) 2007 Stylesheets CSS:Cascading Style Sheets XSL(T):eXtended Stylesheet Language (Transformations)
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.
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://
In this session, you will learn to:
XSLT: The XML transformation language
Session II Chapter 14 – XSLT
In this session, you will learn to:
XML Introduction Bill Jerome.
Displaying XML Data with XSLT
Reference: cit /Lectures/xslt.ppt
Using XSLT and XPath to Transform XML Documents
Using Extension Elements and Extension Functions with XSLT and XPath
eXtensible Stylesheet Language
Extensible Markup Language XML
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Session II Chapter 2 - XSLT
Creating an XML Document
Applying eXtensible Style Sheets (XSL)
XML WITH CSS.
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
WHAT IS XSLT?.
XSL XSLT and XPath 24-Feb-19.
XML: eXtensible Markup Language
Attributes, Empty-Element Tags, and XSL
XSL XSLT and XPath.
More XML XML schema, XPATH, XSLT
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.
Presentation transcript:

XSLT: XML Transformation IST 516 Fall 2011 Dongwon Lee, Ph.D.

Cascading Style Sheets HTML+CSS <html> <head> <style type="text/css"> h1 {color:blue;} h1 u {color:red;} h1 i {color:green;} </style> </head> <body> <h1><u>CSS</u> Example for <i>IST Class</i></h1> </body> </html> CSS separates low-level formatting information from HTML

Then, What About XML File? XML files contain only “data”, not the “formatting” information XML itself does NOT do much useful things Just bunch of tags Apps do interesting stuff using XML files Eg, If data in XML files need: To be stored in RDBMS  transform XML files into relational records and SQL statements To be shown on web browsers  transform XML files into HTML files To be emailed to publisher  transform XML files into PDF or LaTeX files

Eg, Showing XML on Browsers? How can we control the look of XML files then? Step 1: transform XML into HTML Step 2: generate CSS code Step 1: XSLT Step 2: XML-FO Both together: XSL Conceptually, one could think as follows: CSS = Style Sheets for HTML XSL = Style Sheets for XML

XSL eXtensible Stylesheet Language (XSL) consists of: XSLT: “XSL Transformation” XSL-FO: “XSL Formatting Object” (low-level formatting information with HTML+CSS) XPath: to specify the portion of XML document to which the transformation and formatting are applied XSLT: the focus of this class

Alternatives of XML Transformation XSLT handles “transforming” XML data into something else (eg, HTML, text, SQL, …) XML Documents XSLT Processor HTML/CSS Document XSLT Stylesheets

Alternatives of XML Transformation Java/C/Perl/Python/etc programs can “transform” XML data into something else too XML Documents Java/C/ Python/… Compiler HTML/CSS Document Java/C/Python/… Program

Difference? Using programming languages is Using XSLT is Analogy More powerful s.t. developers have more fine-grained control More customized transformation Using XSLT is More generic and declarative Arguably easier than writing programs Analogy XML files  Relational tables XSLT  SQL Java code  JDBC code

How Does XSLT Work? Using XPath expressions, one defines parts of XML data that match one or more predefined templates When a match is found, XSLT will transform the matching part of the XML data into the result output XSLT code consists of: XSLT constructs as in <xsl: …> format Everything else (eg, HTML tags, SQL statements, Python codes, …) that one wants to generate

How Does XSLT Work? Typical Workflow One prepares a source XML data: foo.xml One writes XSLT codes: foo.xsl Link XSLT codes to the XML data by adding one line into the XML data <!-- foo.xml --> <?xml version=“1.0”?> <?xml-stylesheet type="text/xsl" href=”foo.xml"?> <foo> … </foo> View the XML data with XSLT-capable s/w such as Firefox or IE

XSLT Declaration <?xml version=“1.0”?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=“emp”> … emp.xsl: transformation logic goes here <?xml version="1.0”?> <?xml-stylesheet type="text/xsl" href=”emp.xsl"?> <emp> … </emp> emp.xml: input XML document about <emp> elements

XSLT Declaration w. Namespace <?xml version=“1.0”?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform xmlns:pike=“http://pike.psu.edu”> <xsl:template match=“pike:emp”> … emp.xsl: transformation logic goes here <?xml version="1.0”?> <?xml-stylesheet type="text/xsl" href=”emp.xsl"?> <emp xmlns=“http://pike.psu.edu”> … </emp> emp.xml: input XML document about <emp> elements

XSLT Processing Most modern web browsers have built-in XSLT engine/processor emp.xml <?xml version="1.0”?> <?xml-stylesheet type="text/xsl" href=”emp.xsl"?> <emp> … </emp> Firefox applies transformation logic in “emp.xsl” to XML data in “emp.xml” file and display the output on the fly

Versions Influenced by DSSSL--An SGML transformation and styling language V 1.0: 1999 http://www.w3.org/TR/xslt Still widely used and supported Most modern web browsers support this Uses XPath 1.0 V 2.0: 2007 Latest: http://www.w3.org/TR/xslt20/ Users XPath 2.0 Focus of this slide

<xsl:> elements +100 built-in XSLT elements perform different functions Format: <xsl: name attr=“value”> Often “value” is XPath expression to return node-set Boolean expression to return TRUE or FALSE By convention, tags starting with “<xsl: …> namespace are XSLT commands Other regular tags are HTML tags

<xsl:output> <xsl:output method=“value”/> Specifies the output format of XSLT transformation “Value” can be xml html text Eg, <xsl:output method=“html”/> indicates that the output of the transformation would be HTML file

<xsl:text> <xsl:text> contents </xsl:text> “contents” between <xsl:text> and </xsl:text> are verbatim copied to the output Attribute: disable-output-escaping="yes|no” Optional “yes" indicates that special characters (like "<") should be output as is "no" indicates that special characters (like "<") should be output as "<” Default is "no".

HTML Special Characters Some special characters and symbols that cannot be easily generated by keyboards Eg, Non-roman characters: Düsseldorf One can use HTML entities to enter them Eg, &  & <  < >  > á  á Could be confused With tag notations of HTML and XML

<xsl:text> <xsl:text disable-output-escaping="yes"> <MyTag>would appear as it is</MyTag> </xsl:text> <MyTag>would appear as it is</MyTag> <xsl:text disable-output-escaping=“no”> <MyTag>HTML Entities would appear</MyTag> </xsl:text> <MyTag> HTML Entities would appear</MyTag>

<xsl:template> XSLT consists of rules, called “templates” Each template contains an individual transformation logic to apply when nodes are matched by the given XPath expression <xsl:template> builds such templates Eg, <xsl: template match=“/”> matches the whole document <xsl:template match=“XPath”> transformation logic here </xsl:template>

<xsl:value-of> <xsl:value-of select=“XPath”/> Extract single value of XML elements that match the given Xpath XPath should return the single value Eg, <xsl:value-of select=“//cd[1]/title”/> extracts the single value of the “title” element of the first appearing “cd” element

Example: <xsl:template> (zvon.org)

Example: <xsl:template> (zvon.org) By default, the processing Is by document order

<xsl:for-each> <xsl:for-each select=“XPath”/> Select every XML element that matches the given XPath Allows “looping” in XSLT Node-set (ie, multiple <cd> elements) are returned here <xsl:for-each select=“//cd”> <xsl:value-of select=“title”/> <xsl:value-of select=“artist”/> </xsl:for-each>

Example: <xsl:for-each> (zvon.org)

Example: <xsl:for-each> (zvon.org)

<xsl:sort> <xsl:sort select=“XPath”/> Sort elements based on element defined by XPath Often used together with <xsl:for-each>  Among many <cd> nodes matching “//cd”, sort them according to the values of <artist> sub-element of <cd> <xsl:for-each select=“//cd”> <xsl:sort select=“artist”/> </xsl:for-each> Sort based on this element

<xsl:if> <xsl:if test=“boolean expression”> contents if TURE </xsl:if> Conditional action using XPath expression <xsl:for-each select=“//cd”> <xsl:if test=“price = 10”> <xsl:value-of select=“artist”/> </xsl:if> </xsl:for-each>

<xsl:choose> Similar to CASE statements in PL <xsl:choose> <xsl:when test=“expression”> … </xsl:when> <xsl:otherwise> … </xsl:otherwise> </xsl:choose> Similar to CASE statements in PL <xsl:for-each select=“//cd”> <xsl:choose> <xsl:when test=“price = 10”> ONE </xsl:when> <xsl:otherwise> TWO </xsl:otherwise> </xsl:choose> </xsl:for-each>

<xsl:apply-templates> Applies the specified template to current element or its children The “select” attribute makes <xsl:apply-templates> to process only children elements <xsl:template match=“//cd”> <xsl:apply-templates select=“title”/> <xsl:apply-templates select=“artist”/> </xsl:template> … <xsl:template match=“title”> … </xsl:template> <xsl:template match=“artist”> … </xsl:template> Processing order is enforced: <title>, followed by <artist>

Example #1: XML  XML Input (person.xml) Output (person2.xml) <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href=”person.xsl"?> <persons> <person username="JS1"> <fname>John</fname> <family-name>Smith</family-name> </person> <person username="MI1"> <fname>Morka</fname> <family-name>Ismincius</family-name> </persons> <?xml version="1.0" encoding="UTF-8"?> <root> <firstname nickname="JS1">John</firstname> <firstname nickname="MI1">Morka</firstname> </root> Example adopted from http://en.wikipedia.org/wiki/XSLT

Transformation Logic (person.xsl) Example #1: XML  XML Transformation Logic (person.xsl) <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/persons"> <root> <xsl:apply-templates select="person"/> </root> </xsl:template> <xsl:template match="person"> <firstname nickname="{@username}"> <xsl:value-of select="fname" /> </firstname> </xsl:stylesheet>

Example #1: XML  XML To begin the transformation Put both person.xml and person.xsl into the same folder View person.xml using a web browser OR Load person.xml into XMLPad “Preview” option <?xml version="1.0" encoding="UTF-8"?> <root> <firstname nickname="JS1">John</firstname> <firstname nickname="MI1">Morka</firstname> </root>

Example #2: XML  HTML Input (card.xml) Output (card.html) <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="card.xsl"?> <card type="simple"> <name>Graham Spanier</name> <title>President of PSU</title> <email>president@psu.edu</email> <phone>(814) 111-2222</phone> </card> <html xmlns="http://www.w3.org/1999/xhtml"> <title>business card</title> <body bgcolor="gray"> <h1>Graham Spanier</h1> <h3><i>President of PSU</i></h3> <p>email: <a href="mailto:president@psu.edu"><tt>president@psu.edu</tt></a></p> <p>phone: (814) 111-2222</p> </body> </html>

Transformation Logic (card.xsl) Example #2: XML  HTML Transformation Logic (card.xsl) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="card[@type='simple']"> <html xmlns="http://www.w3.org/1999/xhtml"> <title>business card</title><body bgcolor="gray"> <xsl:apply-templates select="name"/> <xsl:apply-templates select="title"/> <xsl:apply-templates select="email"/> <xsl:apply-templates select="phone"/> </body></html> </xsl:template>

Transformation Logic (card.xsl) Example #2: XML  HTML Transformation Logic (card.xsl) <xsl:template match="name"> <h1><xsl:value-of select="text()"/></h1> </xsl:template> <xsl:template match="title"> <h3><i><xsl:value-of select="text()"/></i></h3> </xsl:template> <xsl:template match="email"> <p>email: <a href="mailto:{text()}"><tt> <xsl:value-of select="text()"/> </tt></a></p> <xsl:template match="phone"> <p>phone: <xsl:value-of select="text()"/></p></xsl:template> </xsl:stylesheet>

Example #2: XML  HTML To begin the transformation Put both card.xml and card.xsl into the same folder View card.xml using a web browser OR Load card.xml into XMLPad “Preview” option

Example #3: XML  SQL Input: table.xml Goal: to load this data into MySQL Issue: MySQL can’t take an XML file Task: Transform data in table.xml into SQL INSERT statements Eg, XSLT Processor table.xml table.sql table.xsl 38

Transformation Logic (table.xsl) Example #3: XML  SQL Input Data (table.xml) Transformation Logic (table.xsl) <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="table.xsl"?> <employee> <tuple> <id>1</id> <name> <ln>Lee</ln> <fn>Dongwon</fn> </name> <salary>500000</salary> <manager>199</manager> <birthyear>1980</birthyear> <startyear>2002</startyear> </tuple> <id>2</id> <ln>Luo</ln><fn>Robert</fn> … <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0"> <xsl:output method="text"/> <xsl:template match="tuple"> INSERT INTO employee VALUES ( <xsl:value-of select="id"/>, "<xsl:value-of select="name/ln"/>, <xsl:value-of select="name/fn"/>", <xsl:value-of select="salary"/>, <xsl:value-of select="birthyear"/>, <xsl:value-of select="startyear"/>); </xsl:template> </xsl:stylesheet> 39

Example #3: XML  SQL Output (table.sql) INSERT INTO employee VALUES (1, 'Lee, Dongwon', 500000, 199, 1980, 2002); INSERT INTO employee VALUES (2, 'Luo, Robert', 900000, 1, 1985, 2003); Data Insertion 40

Example #3: XML  SQL To begin the transformation Put both table.xml and table.xsl into the same folder View table.xml using a web browser OR Load table.xml into XMLPad “Preview” option

Examples Download Download two examples from here: http://pike.psu.edu/classes/ist516/latest/labs/xslt/ Load them into XMLPad Play with different features of XSLT by modifying “*.xsl” files

Much More XSLT Features XSLT has more than 100 built-in functions Useful online XSLT tool at Zvon http://www.zvon.org/xxl/XSLTutorial/Books/Output/contents.html Try: 1, 3, 6, 8, 9, 11, 14, 15, 18, 19, 26

XSLT Processors XSLT Processor (or Engine): program that can take XML and XSLT as input and apply the specified transformations XT: James Clark’s XSLT processor for Java XALAN: Apache’s XSLT processor for Java/C++ MSXML: Microsoft’s XSLT processor Embedded in Microsoft IE web browser More here: http://www.w3c.org/Style/XSL/

Lab #3 (DUE: Oct. 2 11:55PM) https://online.ist.psu.edu/ist516/labs Tasks: Individual Lab Using the XMLSpy Editor, write a CSS file matching the given HTML output Turn-In CSS file

Reference XSLT, 1st Edition, Doug Tidwell, O’Reilly, 2001 W3Schools XSLT Tutorial http://www.w3schools.com/xsl/ Zvon XSLT Tutorial http://www.zvon.org/xxl/XSLTutorial/Output/index.html