1 XML Data Management XLST Werner Nutt. A Hello World! Stylesheet <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> world.

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

Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
Internet Technologies1 1 Lecture 4: Programming with 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.
XSLT Stylesheets Some more advanced examples (adapted from the Edinburgh LT site)
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.
XSL Concepts Lecture 7. XML Display Options What can XSL Transformations do? generation of constant text suppression of content moving text (e.g., exchanging.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
Internet Technologies XSLT Processing XML using XSLT Using XPath Escaping to Java.
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – Carl Lagoze – Cornell University.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Internet Technologies1 XSLT Processing XML using XSLT Using XPath.
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.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
SD2520 Databases using XML and JQuery
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.
JSP Standard Tag Library
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
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.
CSE3201/CSE4500 Information Retrieval Systems
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.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
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.
XML Part 2 Josh Steele CS 6704, Spring XML Part 2 XSL/XSLT –Structure Revisited –Definition –Example –Components XML’s Linking Languages XML’s Uses.
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Lecture 11 XSL Transformations (part 1: Introduction)
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
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 XPath Principles Werner Nutt.
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.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Outline Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
1 Whitespace Handling Roger L. Costello XML Technologies.
Querying XML, Part II Zachary G. Ives University of Pennsylvania CIS 455 / 555 – Internet and Web Systems February 5, 2008.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
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.
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.
Displaying XML Data with XSLT
Applying eXtensible Style Sheets (XSL)
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
More XML XML schema, XPATH, XSLT
Presentation transcript:

1 XML Data Management XLST Werner Nutt

A Hello World! Stylesheet <xsl:stylesheet xmlns:xsl=" version="1.0"> world Top-level: element with a version="1.0" attribute Declarations (all elements except the ones), in this case just an Template rules in this case a template that applies to the root node

Invocation of an XSLT Stylesheet An XSLT stylesheet may be invoked: Programmatically, through an XSLT libraries Through a command line interface. In a Web Publishing context, by including a styling processing instruction in the XML document –the transformation can be processed on the server side by a PHP, ASP, JSP,... Script –or on the client side through the XSLT engines integrated to most browsers.

Web Publishing with XSLT HTML XSLT Stylesheet XSLT Stylesheet Network XML Document HTML XSLT Stylesheet XSLT Stylesheet Network XML Document

Stylesheet Output method is either xml (default), html or text encoding is the desired encoding of the result doctype-public and doctype-system makes it possible to add a document type declaration in the resulting document indent specifies whether the resulting XML document will be indented (default is no ) <xsl:output method="html" encoding="iso " doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system=" indent="yes" />

Handling Whitespace Both elements require a set of space-separated node tests as their attribute. specifies the set of nodes whose whitespace-only text child nodes will be removed allows for exceptions to this list

The Element A template consists of A pattern: an XPath expression (restricted) which determines the nodes to which the template applies. The pattern is the value of the “match” attribute. A body: an XML fragment (valid!) which is inserted in the output document when the template is instantiated The book title is: Authors list

XPath Patterns in XSLT The XPath expression of the “match” attribute describes the nodes that can be the target of a template instantiation. Those expressions are called patterns and must satisfy: A pattern always denotes a node set. Example: is incorrect. Checking whether a node is matched must be easy Example: is meaningful, but difficult to evaluate. Pattern syntax: A pattern is a valid XPath expression which uses only the child axes, and the abbreviation //. Predicates are allowed.

Content of a Template Body Literal elements and text Example: Authors. Creates in the output document an element h2, with a text child node ’Authors’. Values and elements from the input document Example: ). Inserts in the output document a node set, result of the XPath expression title. Call to other templates Example:. Applies a template to each node in the node set result of the XPath expression authors. Only the basics of XSLT programming! Many advanced features (modes, priorities, loops and tests) beyond this core description

Instantiation of an Main principles: Literal elements (those that don’t belong to the XSLT namespace) and text are simply copied to the output document. Context node: A template is always instantiated in the context of a node from the input document. XPath expressions: all the (relative) XPath expression found in the template are evaluated with respect to the context node (an exception: ). Calls with : find and instantiate a template for each node selected by the XPath expression select. Template call substitution: any call to other templates is eventually replaced by the instantiation of these templates.

The Element select: an XPath expression which, if relative, is interpreted with respect to the context node. Note: the default value is child::node(), i.e., select all the children of the context node mode: a label which can be used to specify which kind of template is required. priority: gives a priority level in case of conflict. <xsl:apply-templates select="authors/name" mode="top" priority="1" />

The Mechanism... Jim Joe Jim Joe

The Execution Model of XSLT An XSLT stylesheet consists of a set of templates. The transformation of an input document I proceeds as follows: 1.The engine considers the root node r of I, and selects the template that applies to r. 2.The template body is copied in the output document O. 3.Next, the engine considers all the in O, and evaluates the match XPath expression, taking r as context node. 4.For each node result of the XPath evaluation, a template is selected, and its body replaces the call. 5.The process iterates, as new are inserted in O. 6.The transformation terminates when O is free of xsl: instructions.

The Execution Model: Illustration

“Return all Titles of Recipes” <xsl:stylesheet version="2.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" encoding="iso " indent="yes"/> Copy each title Iterate over all title elements

A Variant That Does Not Copy the Content … … copy-of : produces a deep copy, i.e., copies a subtree copy : produces a shallow copy, i.e., copies one node (plus optionally namespace info for elements)

Identity Map Deep copy Shallow copy

Shallow Copy, with Explicit Text Copy Shallow copy of title Shallow copy of title content

Element Construction Dynamic element constructor

Copying Attributes Copying attributes leads to element fusion

Constructing Attributes Dynamic attribute constructor It’s called “dynamic” because element and attribute names can be computed on the fly

Computing Attribute Values Expressions in {braces} will be evaluated

Nested Iteration with Turn recipe titles into attributes ingredient names into strings

As Before, with 2 Levels of Ingredients Level 1 Level 2

Nested Iteration with Template Calls Root calls recipes Recipes calls recipe

Nested Iteration with Template Calls (cntd) Recipe calls ingredient Ingredient calls ingredient

Sorted List of All Ingredients can be nested into

Sorting: Does This Work? We want to sort all ingredients by name

Exercise: Restructuring Recipes Return a list, inside an element, of recipes, containing for every recipe the recipe’s title element and an element with the number of calories. Use different approaches: (a)express iteration by recursive calls of templates (b)express iteration by elements. Create new elements (a)by explicit construction, that is by writing the tags into the code, (b)by dynamic construction, that is, by using and elements, (c)by shallow and deep copying, wherever the latter is possible.

Ordered Output Using iteration by recursion, return a similar list, alphabetically ordered according to title. Using iteration by means of, return a similar list, ordered according to calories in descending order.

Element and Attribute Construction Return a similar list, with title as attribute and calories as content. Return a list, inside an element, of recipes, where each recipe contains the title and the top level ingredients, while dropping the lower level ingredients.

XSLT for Recipes (1/6) <xsl:output method="html" encoding="iso " indent="yes"/> My Best Recipes

XSLT for Recipes (2/6)

XSLT for Recipes (3/6) number(1)"> s of

XSLT for Recipes (4/6)

XSLT for Recipes (5/6)

XSLT for Recipes (6/6) Calories Fat Carbohydrates Pr otein Alcohol

A Different View <dish name="{title/text()}" alcohol="{if then else '0%'}"/>

The Output <dish name="Ricotta Pie" calories="349" fat="18" carbohydrates="64" protein="18" alcohol="0%"/> <dish name="Linguine alla Pescatora" calories="532" fat="12" carbohydrates="59" protein="29" alcohol="0%"/> <dish name="Zuppa Inglese" calories="612" fat="49" carbohydrates="45" protein="4" alcohol="2"/> <dish name="Cailles en Sarcophages" calories="1892" fat="33" carbohydrates="28" protein="39" alcohol="0%"/>

A Sorted List of Ingredients w/o Duplicates <xsl:if test= = We ensure that only ingredients are output that have not appeared before This test for duplicates can be expensive!

Duplicate Eliminations à la Muench * Step 1: Construct keys (= indices for node sets) name : name of the key match : the node set to be indexed use : the index key values <xsl:key name="ingredients-by-name" match="ingredient" <xsl:key name="recipes-by-title" match="recipe“ use="title"/> * Invented by Steve Muench, called the “Muenchian Method” in the XSLT world is a top-level element that declares a named key that can be used in the style sheet with the key() function. Note: A key does not have to be unique!

Duplicate Eliminations à la Muench Step 2: Iterate over the recipes …

Duplicate Elimination à la Muench <xsl:for-each select="recipe//ingredient [count(. | = 1]"> select those ingredients that occur as the first element in their index group  the others are redundant … sort ingredients by name, then retrieve recipes from their index

Grouping in XSLT 2.0 <xsl:for-each-group select="//ingredient" <use name="{current-grouping-key()}" count="{count(current-group())}"/>

countries.dtd <!ATTLIST country name CDATA #REQUIRED population CDATA #REQUIRED area CDATA #REQUIRED> <!ATTLIST language percentage CDATA #REQUIRED>

Queries About Countries: Example 1 Restructure the document by listing countries according to population, cities within each country according to population, and languages within each country according to percentage.

Restructuring the Countries Document (1) <xsl:stylesheet version="2.0" xmlns:xsl=" <xsl:output method="xml" version="1.0" encoding="iso " indent="yes"/>

Restructuring the Countries Document (2)

Restructuring the Countries Document (3)

Compare the XQuery let $doc := doc("countries.xml") let $cs := $doc//country return element countries {for $c in $cs order by descending return element country for $city in $c/city order by number($city/population) descending return $city, for $l in $c/language order by descending return $l }

Languages Spoken in Countries Return a list of language elements, alphabetically sorted, where each language element contains –a list of country elements, –such that the language is spoken in the country, –together with the number of speakers of the language in that country. Difficulties: eliminate duplicates among languages retrieve the countries where the language is spoken  how can we remember that language?

Languages Spoken in Countries: XQuery let $doc := doc("countries.xml") let $ls := distinct-values($doc//language) let $cs := $doc//country return {for $l in $ls order by $l return {attribute name {$l}} {for $c in $cs[language=$l] order by return {attribute speakers div 100) * }} } language is remembered in variable $l

Languages Spoken in Countries: XSLT <xsl:sort select="text()" order="ascending" data-type="text"/> named template

Languages Spoken in Countries: XSLT why not " not(. = preceding::language)" calling a named template adding a parameter to the call eliminate duplicates

Languages Spoken in Countries: XSLT <xsl:attribute name="speakers" select="format-number( div 100) * context node = context node at call (no matching) makes the parameter of the call available $language is a reference to the paramenter with format-number we can specify number formats, e.g., ‘0’ indicates digit notation