What is XSLT? XSLT: Using XML to transform. XSLT vs XQUERY. XSLT processing model. An XML based style sheet language for XML documents



Similar documents
Structured vs. unstructured data. Motivation for self describing data. Enter semistructured data. Databases are highly structured

Data Integration through XML/XSLT. Presenter: Xin Gu

XSL Elements. xsl:copy-of

XSLT Mapping in SAP PI 7.1

10CS73:Web Programming

XSL - Introduction and guided tour

Hydrologic Modeling System HEC-HMS

Structured vs. unstructured data. Semistructured data, XML, DTDs. Motivation for self-describing data

How To Use Query Console

Extending the Linked Data API with RDFa

Introduction to Ingeniux Forms Builder. 90 minute Course CMSFB-V6 P

Using Style Sheets for Consistency

metaengine DataConnect For SharePoint 2007 Configuration Guide

An XML Based Data Exchange Model for Power System Studies

Interspire Website Publisher Developer Documentation. Template Customization Guide

XML: extensible Markup Language. Anabel Fraga

Extensible Markup Language (XML): Essentials for Climatologists

HOW TO CREATE THEME IN MAGENTO 2

Building A Very Simple Web Site

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

GraphML Transformation

Introduction to XML Applications

Developers Guide. Designs and Layouts HOW TO IMPLEMENT WEBSITE DESIGNS IN DYNAMICWEB. Version: English

David RR Webber Chair OASIS CAM TC (Content Assembly Mechanism)

JobScheduler Web Services Executing JobScheduler commands

Cross Site Scripting (XSS) and PHP Security. Anthony Ferrara NYPHP and OWASP Security Series June 30, 2011

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

How to Properly Compose HTML Code : 1

LabVIEW Internet Toolkit User Guide

Terms and Definitions for CMS Administrators, Architects, and Developers

Hypercosm. Studio.

New Perspectives on Creating Web Pages with HTML. Considerations for Text and Graphical Tables. A Graphical Table. Using Fixed-Width Fonts

Paging, sorting, and searching using EF Code first and MVC 3. Introduction. Installing AdventureWorksLT database. Creating the MVC 3 web application

Fast track to HTML & CSS 101 (Web Design)

Semistructured data and XML. Institutt for Informatikk INF Ahmet Soylu

Moving from CS 61A Scheme to CS 61B Java

MadCap Software. Import Guide. Flare 11

Overview of DatadiagramML

Chapter 3: XML Namespaces

What I Wish I Had Known. In my first 30 days with umbraco

An Approach to Translate XSLT into XQuery

ITNP43: HTML Lecture 4

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Firewall Builder Architecture Overview

Agents and Web Services

Command-Line Tool for View Manager View Manager 4.0

Chapter 19: XML. Working with XML. About XML

CSS 101. CSS CODE The code in a style sheet is made up of rules of the following types

Example. Represent this as XML

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Using an external style sheet with Dreamweaver (CS6)

Qt Features for Hybrid Web/Native Application Development

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

MASTERTAG DEVELOPER GUIDE

Web Services Technologies

About XML in InDesign

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

XSLT. Patryk Czarnik. Institute of Informatics University of Warsaw. XML and Modern Techniques of Content Management 2010/11

Tutorial 5. Working with Web Tables

Data Tool Platform SQL Development Tools

Web Development 1 A4 Project Description Web Architecture

Interactive Data Visualization for the Web Scott Murray

ORACLE USER PRODUCTIVITY KIT USAGE TRACKING ADMINISTRATION & REPORTING RELEASE 3.6 PART NO. E

Macromedia Dreamweaver 8 Developer Certification Examination Specification

XML Processing and Web Services. Chapter 17

Selectors in Action LESSON 3

CIS 467/602-01: Data Visualization

Responsive Web Design: Media Types/Media Queries/Fluid Images

MAS 500 Intelligence Tips and Tricks Booklet Vol. 1

Guile Present. version 0.3.0, updated 21 September Andy Wingo

Kuali Financial System Interface Specification for Electronic Invoice Feed

Managing XML Documents Versions and Upgrades with XSLT

Deferred node-copying scheme for XQuery processors

Software documentation systems

By Nabil ADOUI, member of the 4D Technical Support team

CNC Transfer. Operating Manual

TagSoup: A SAX parser in Java for nasty, ugly HTML. John Cowan (cowan@ccil.org)

XML and Data Management

Internationalization Tag Set 1.0 A New Standard for Internationalization and Localization of XML

T XML in 2 lessons! %! " #$& $ "#& ) ' */,: -.,0+(. ". "'- (. 1

Quick Start Guide. This guide will help you get started with Kentico CMS for ASP.NET. It answers these questions:

TheEducationEdge. Export Guide

Kentico CMS, 2011 Kentico Software. Contents. Mobile Development using Kentico CMS 6 2 Exploring the Mobile Environment 1

PLAYER DEVELOPER GUIDE

Cleo Communications. CUEScript Training

OPENTABLE GROUP SEARCH MODULE GETTING STARTED ADD RESERVATIONS TO YOUR WEBSITE

UOFL SHAREPOINT ADMINISTRATORS GUIDE

Data XML and XQuery A language that can combine and transform data

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Transcription:

What is XSLT? 2 XSLT: Using XML to transform other XML files Introduction to databases CSC343 Fall 2011 Ryan Johnson An XML based style sheet language for XML documents Tells web browser how to display data Similar to CSS for HTML, but with absolute control over input Achieves similar results to XQuery Pro: supported by all non ancient web browsers Con: XML syntax is [really] verbose Thanks to Manos Papagelis, John Mylopoulos, Arnold Rosenbloom and Renee Miller for material in these slides XSLT processing model 3 XSLT vs XQUERY 4 The XSLT processing model involves: XSLT capabilities overlap with XQuery one (or more) XML source documents one (or more) XSLT style sheet modules the XSLT template processing engine one (or more) result documents XSLT is most often used to convert data between different XML schemas to convert XML data into web pages (HTML) same data model, type system, function library both include XPath 2.0 as a sublanguage Rooted in different traditions and communities XSLT: primarily conceived as a stylesheet language XQuery: primarily conceived as a database query language When to use: XSLT: stronger in its handling of narrative documents with more flexible structure XQuery: stronger in its data handling (e.g, when performing relational joins) 1

Example 1: Transform. XML to HTML 5 Example 1: Transform. XML to HTML 6 <?xml version="1.0" encoding="iso 8859 1"?> <catalog> <cd> <title>empire Burlesque</title> <artist>bob Dylan</artist> <country>usa</country> <company>columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>unchain my heart</title> <artist>joe Cocker</artist> <country>usa</country> <company>emi</company> <price>8.20</price> <year>1987</year> </cd> </catalog> The XML Document <?xml version="1.0" encoding="iso 8859 1"?> <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> <td><xsl:value of select="artist"/></td> </tr> </xsl:for each> </table> </body></html> </xsl:stylesheet> The XSLT for the XML Example 1: Transform. XML to HTML 7 Example 2: Transform. XML to XML 8 My CD Collection Title Empire Burlesque Unchain my heart Artist Bob Dylan Joe Cocker The HTML Result Document <?xml version="1.0"?> <persons> <person username="js1"> <name>john</name> <family name>smith</family name> </person> <person username="mi1"> <name>morka</name> <family name>ismincius</family name> </person> </persons> The XML Document 2

Example 2: Transform. XML to XML 9 Example 2: Transform. XML to XML 10 <?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"/> The XSLT for the XML <xsl:template match="/persons"> <root> <xsl:apply templates select="person"/> </root> <xsl:template match="person"> <name username="{@username}"> <xsl:value of select="name" /> </name> </xsl:stylesheet> <?xml version="1.0" encoding="utf 8"?> The HTML Result Document <root> <name username="js1">john</name> <name username="mi1">morka</name> </root> XSLT in a nutshell 11 Control flow in XSL 12 All XSL commands are xml (xsl: namespace) Everything works by templates Output generated by instantiating templates Templates can call other templates (recursion allowed) No output unless some template matches document root Overloading rules for resolving 2+ matching templates Builds on material we already know XPath used to select nodes for various uses Embedded expressions use {} syntax from XQuery <xsl:for each select= ></xsl:for each> Used to select every XML element of a specified node set Instantiate the template once for each item Inside the template, context node (.) refers to current item <xsl:if test= ></xsl:if> Used to put a conditional test against the content of the XML file Instantiate the template inside if the condition is true No else (use a second xsl:if or xsl:choose instead) <xsl:choose></xsl:choose> used along with <xsl:when> and <xsl:otherwise> to express multiple conditional tests <xsl:when test= ></xsl:when> (same usage as xsl:if) <xsl:otherwise></xsl:otherwise> (no match template) Instantiate only one template (first match wins) 3

Sorting elements 13 Declaring variables 14 Elements can be sorted inside for each loops <xsl:sort select= order= /> <xsl:variable name= select= /> select should identify the element to sort on order can be ascending or descending Example: Store the result of an XPath query in a variable <xsl:variable name= ></xsl:variable> Instantiate a template and store its result in a variable Notes <xsl:for each select= //book > <xsl:sort select= author/surname /> <xsl:sort select= author/given name /> </xsl:for each> Default value (if none specified) is an empty string Cannot be overwritten or updated once set Sometimes valid as input to templates (more later) Declaring templates 15 Applying vs. calling templates 16 Idea: A template contains rules to apply when a specified node is matched. Results are inserted into the output <xsl:template match= mode= > Template instantiated for every element satisfying the match s XPath expression Optional mode attribute allows to select from 2+ templates <xsl:template name= > Template instantiated explicitly OK to name a match template <xsl:apply templates select= mode= /> Applies a template to the current element or to the its child nodes Format a node (default:.) using best matching template Complicated priority rules, but more specific and declared later templates usually chosen => When in doubt disambiguate using optional mode attribute <xsl:call template name= /> Instantiate the named template WARNING: context node unchanged by call! => Call a template which uses. ==> possible infinite loop <xsl:parameter> and <xsl:with param> Templates can declare xsl:parameter (acts just like xsl:variable) => Same effect if template is called normally Template call can have xsl:with param child elements => Overrides value which xsl:parameter would have assigned 4

Applying templates to variables 17 EXSLT 18 Only select type variables have XML content Otherwise, contents treated as text Limitation of both XSLT 1.0 and 2.0 (fixed by EXSLT, next) Example: <xsl:variable name= foo select= //foo /> <xsl:variable name= bar > <xsl:apply templates select= $foo > $foo stores XML </xsl:variable> <xsl:variable name= baz > $bar stores text! <xsl:apply templates select= $bar > </xsl:variable> The EXSLT spec addresses weaknesses of XSLT Several modules each with its own namespace Supported by modern XSLT processors (IE9, FF3+, etc.) Have to ask for it by adding namespaces Especially handy modules: xmlns:exsl= http://exslt.org/common => Provides exsl:node-set, a function to turn text into XML xmlns:str= http://exslt.org/strings => Provides str:split, which works just like python s split() Outputting things 19 Global output controls 20 <xsl:text></xsl:text> Adds text (including whitespace!) to output Optional yes/no attribute: disable output escaping => In theory, good for outputting &, < etc. => WARNING: optional part of spec, often ignored <xsl:value of select= /> Converts the selected node(s) to text and outputs it <xsl:copy of select= /> Embeds the selected node(s) directly into the output e.g. good for emitting snippets of pre constructed HTML <xsl:output method= indent= /> Defines the format of the output document Methods include xml (default), html, and text => Defaults to html method if parser detects HTML output Enabling indentation makes output prettier => Bad idea if whitespace matters for presentation Lots of other options (look them up) 5

Whitespace handling Default: any text node with non space characters preserves all space Override #1: <xsl:strip space elements= /> Specifies that the elements named (separated by spaces) should normalize whitespace before output A corresponding preserve space command exists also Same conflict resolution as used for template matching Override #2: < xml:space= preserve > Little known xml attribute that tells XML processors whether whitespace matters Inherited by child elements (xml:space= default cancels) Watch out for elements forbidding text (e.g. xsl:call template) 21 Keys Like the ID/IDREF from DTD but better No need for the DTD (often ignored even when present) Multiple (named) ID domains allowed <xsl:key name= match= use= /> Declares a key (name) for all nodes which match, as well as how to find (use) the value of the key {key(, )} retrieves the value of a key Example: Find all links which point to an image in the same file <xsl:key name= imglinks match= //a use= @href /> {key( imglinks, //img/@src)} 22 Sample XSLT skeleton <?xml version="1.0" encoding="iso 8859 1"?> <xsl:stylesheet version="1.0 obligatory headers xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match='/'> template for doc root top level content here <xsl:template name='show tree'> </xsl:stylesheet> named template (for explicit instantiation) more template content here 23 Sample: root node template <xsl:template match='/'> <html><body> <xsl:call template name='show tree'> <xsl:with param name='e' select='*'/> <xsl:with param name='i' select='0'/> </xsl:call template> </body></html> <xsl:template match='*'> <xsl:value of select= name(.) /> <xsl:template match= nct_id'> <span style= color:red > <xsl:value of select= self::text() /> </span> Elements template applies to HTML to embed the template in 24 Instantiate show tree template, passing e=* and i=0 as parameters By default, output the name of each element nct_id is important ==> special treatment 6

Sample: recursive template <xsl:template name='show tree'> <xsl:param name='e'/> <xsl:param name='i'/> <div style='padding left:{$i}px'> <xsl:apply templates select='name($e)'/> <xsl:if test='$e/@*'> ( <xsl:for each select='$e/@*'> <xsl:value of select= name(.)'/>, </xsl:for each> ) </xsl:if> <xsl:for each select='$e/*'> <xsl:call template name='show tree'> <xsl:with param name='e' select='.'/> <xsl:with param name='i' select='30'/> </xsl:call template> </xsl:for each> </div> named templates must be called 25 template parameters compute div s padding format the element list attributes, if any exist (surrounded by parens, separated by commas) recursively apply template to children Sample XSLT + clinical_study XML HTML Output <html><body> <div style= margin left: 0px;"> clinical_study (rank, ) <div style= margin left: 30px;"> required_header <div style= margin left: 30px;"> download_date </div> </body></html> Rendered HTML 26 7