Top Banner
1 XML Data Management 7. XLST Werner Nutt
66

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,

Dec 28, 2015

Download

Documents

Trevor Bryant
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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

XML Data Management

7. XLST

Werner Nutt

Page 2: 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,

Kernow and Saxon

• To demo XSLT, we use Kernow

– Kernow is a (graphical) front end for Saxon

• Saxon is an XSLT, XQuery, XPath processor,implemented in Java

• Three editions of Saxon: home, professional, enterprise

– home edition is open source and free

– for professional and enterprise editions,there are commercial licenses

2

Page 3: 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,

A Hello World! Stylesheet

<xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">

<xsl:output method="xml" encoding="utf-8" />

<xsl:template match="/">

<hello>world</hello>

</xsl:template>

</xsl:stylesheet>

Top-level: <xsl:stylesheet> elementwith a version="1.0" attribute

Declarations (all elements except the <xsl:template> ones),

in this case just an <xsl:output>

Template rulesin this case a template that applies to the root node

Page 4: 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,

Invocation of an XSLT Stylesheet

An XSLT stylesheet may be invoked:• Programmatically, through XSLT libraries• Through a command line interface.• In a Web Publishing context, by including a styling processing

instruction in the XML document

<?xml version="1.0"?><?xml-stylesheet href="blabla.xsl" type="application/xml"?>

<doc> <blublu/></doc>

– the transformation can be processed on the server side by a Java, C#, PHP, .NET, JSP, . . . program/script

– or on the client side through the XSLT engines integrated into most browsers.

Page 5: 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,

Web Publishing with XSLT

HTML HTML

XSLTStylesheet

XSLTStylesheet

Network

XML Document

HTMLHTML

XSLTStylesheet

XSLTStylesheet

Network

XML Document

Page 6: 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,

Output Parameters

• method is either xml (default), html or text

• encoding is the desired character encoding of the result

• doctype-public and doctype-system makes it possible to add a document type declaration to the resulting document

• indent specifies whether the resulting XML document will be indented (default is no)

<xsl:output method="html"

encoding="iso-8859-1"

doctype-public="-//W3C//DTD HTML 4.01//EN"

doctype-system="http://www.w3.org/TR/html4/strict.dtd"

indent="yes" />

Only one doctype appears in output

Page 7: 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,

Handling Whitespace

Both elements require a set of space-separated node tests

as their attribute.

• <xsl:strip-space> specifies the set of nodes whosewhitespace-only text child nodes will be removed

• <xsl:preserve-space> allows for exceptions to this list

<xsl:strip-space elements="*" />

<xsl:preserve-space elements="para poem" />

Page 8: 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,

The <xsl:template> 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

<xsl:template match="book"> The book title is: <xsl:value-of select="title" /> <h2>Authors list</h2> <ul> <xsl:apply-templates select="authors/name" /> </ul></xsl:template>

Page 9: 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,

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: <xsl:template match="1"> is incorrect.

• Checking whether a node is matched must be easy

Example: <xsl:template match="preceding::*[12]"> is meaningful, but difficult to evaluate.

Pattern syntax:

• A pattern is a valid XPath expression which uses only the child and @ axes, and the abbreviation //. Predicates are allowed.

Page 10: 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,

Content of a Template Body

• Literal elements and text

Example: <h2>Authors</h2> . Creates in the output document an element h2,

with a text child node ’Authors’.

• Values and elements from the input document

Example: <xsl:value-of select="title"/> ). Inserts in the output document a node set,

result of the XPath expression title.

• Call to other templates

Example: <xsl:apply-templates select="authors"/>. 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

Page 11: 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,

Instantiation of an <xsl:template>

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: <xsl:for-each> ).

• Calls with <xsl:apply-templates>: 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.

Page 12: 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,

The <xsl:apply-templates> 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"/>

Page 13: 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,

The <xsl:apply-templates> Mechanism

<xsl:template match="book"> <ul> <xsl:apply-templates select="authors/name" /> </ul></xsl:template>

<xsl:template match="name"> <li><xsl:value-of select="." /></li></xsl:template>

<book>... <authors> <name>Jim</name> <name>Joe</name> </authors></book>

<ul> <li>Jim</li> <li>Joe</li></ul>

Page 14: 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,

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 <xsl:apply-templates> 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 <xsl:apply-templates> call.

5. The process iterates, as new <xsl:apply-templates> are inserted in O.

6. The transformation terminates when O is free of xsl: instructions.

Page 15: 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,

The Execution Model: Illustration

Page 16: 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,

The Execution Model: Illustration

Page 17: 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,

The Execution Model: Illustration

Page 18: 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,

The Execution Model: Illustration

Page 19: 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,

The Execution Model: Illustration

Page 20: 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,

The Execution Model: Illustration

Page 21: 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,

Built-In Template Rules

What happens if no template matches a node?

•XSLT applies a default template rule:– text is copied to the output– for other nodes

the stylesheet is recursively applied to the children

•Default rule is widely used for the document root node

21

Page 22: 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,

“Return all Titles of Recipes”

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/"> <titles> <xsl:for-each select="//title"> <xsl:copy-of select="./self::*"/> </xsl:for-each> </titles></xsl:template>

</xsl:stylesheet>

Copy each title

Iterate over alltitle elements

Page 23: 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,

A Variant That Does Not Copy the Content

…<xsl:template match="/"> <titles> <xsl:for-each select="//title"> <xsl:copy/> </xsl:for-each> </titles></xsl:template>…

• 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)

Page 24: 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,

Identity Map

Deep copy

Shallow copy

<xsl:template match="/"> <xsl:copy-of select="*"/></xsl:template>

<xsl:template match= "node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy></xsl:template>

This is the same as

child::node()

Page 25: 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,

Shallow Copy, with Explicit Text Copy

<xsl:template match="/">

<titles>

<xsl:for-each select="//title">

<xsl:copy>

<xsl:for-each select="text()">

<xsl:copy/>

</xsl:for-each>

</xsl:copy>

</xsl:for-each>

</titles>

</xsl:template>

Shallow copyof title

Shallow copyof title content

Page 26: 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,

Dynamic Element Construction

<xsl:template match="/">

<xsl:element name="titles">

<xsl:for-each select="//title">

<xsl:copy>

<xsl:for-each select="text()">

<xsl:copy/>

</xsl:for-each>

</xsl:copy>

</xsl:for-each>

</xsl:element>

</xsl:template>

Dynamic element constructor

Page 27: 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,

Attribute Copying with Element Fusion

<xsl:template match="/">

<xsl:element name="recipe-ingredients">

<xsl:for-each select="//recipe//ingredient/@name">

<xsl:element name=”recipe-ingredient">

<xsl:copy/>

</xsl:element>

</xsl:for-each>

</xsl:element>

</xsl:template>

Copying attributes leads to element fusion

Page 28: 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,

Dynamic Attribute Construction

<xsl:template match="/">

<xsl:element name="recipe-ingredients">

<xsl:for-each select="//recipe//ingredient/@name">

<xsl:element name="recipe-ingredient">

<xsl:attribute name="name" select="."/>

</xsl:element>

</xsl:for-each>

</xsl:element>

</xsl:template>

Dynamic attribute constructor

• It’s called “dynamic” because element and attribute namescan be computed on the fly

Page 29: 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,

<xsl:template match="/">

<xsl:element name="recipe-ingredients">

<xsl:for-each select="//recipe//ingredient">

<recipe-ingredient name = "{@name}"/>

</xsl:for-each>

</xsl:element>

</xsl:template>

Computing Attribute Values

Expressions in {braces}will be evaluated

Page 30: 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,

Nested Iteration

<xsl:template match="/">

<my-recipes>

<xsl:for-each select=".//recipe">

<my-recipe title="{title}">

<xsl:for-each select="ingredient">

<my-ingredient>

<xsl:value-of select="@name"/>

</my-ingredient>

</xsl:for-each>

</my-recipe>

</xsl:for-each>

</my-recipes>

</xsl:template>

Turn

• recipe titles into attributes

• ingredient names into strings

Page 31: 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,

Nested Iteration over 2 Levels

<xsl:template match="/"> <my-recipes> <xsl:for-each select=".//recipe"> <my-recipe title="{title}"> <xsl:for-each select="ingredient"> <my-ingredient> <name> <xsl:value-of select="@name"/> </name> <xsl:for-each select="ingredient"> <my-ingredient> <xsl:value-of select="@name"/> </my-ingredient> </xsl:for-each> </my-ingredient> </xsl:for-each> </my-recipe> </xsl:for-each> </my-recipes> </xsl:template>

Level 1

Level 2

Page 32: 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,

Iteration by Template Calls

<xsl:template match="/">

<xsl:apply-templates select="recipes"/>

</xsl:template>

<xsl:template match="recipes">

<my-recipes>

<xsl:apply-templates select="recipe"/>

</my-recipes>

</xsl:template> • Root calls recipes• Recipes calls recipe

Page 33: 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,

Iteration by Template Calls (cntd)

<xsl:template match="recipe"> <my-recipe title="{title}"> <xsl:apply-templates select="ingredient"/> </my-recipe></xsl:template>

<xsl:template match="ingredient"> <my-ingredient> <name> <xsl:value-of select="@name"/> </name> <xsl:apply-templates select="ingredient"/> </my-ingredient></xsl:template>

• Recipe calls ingredient• Ingredient calls ingredient

Page 34: 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,

Sorting

<xsl:template match="/"> <result> <xsl:apply-templates select="/recipes"/> </result></xsl:template>

<xsl:template match="recipes"> <xsl:for-each select="recipe//ingredient"> <xsl:sort select="@name" /> <ingredient> <xsl:value-of select="@name"/> </ingredient> </xsl:for-each></xsl:template> <xsl:sort> can be nested

into <xsl:for-each>

Page 35: 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,

Sorting: Does This Work?

<xsl:template match="/"> <result> <xsl:apply-templates select="//ingredient"/> </result></xsl:template>

<xsl:template match="ingredient"> <xsl:for-each select="."> <xsl:sort select="@name" /> <ingredient> <xsl:value-of select="@name"/> </ingredient> </xsl:for-each></xsl:template>

We want to sort all ingredients by name

Page 36: 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,

Exercise: Restructuring Recipes

Return a list, inside an element <my-recipes>, 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 <xsl:for-each> 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 <xsl:element>

and <xsl:attribute> elements,(c) by shallow and deep copying, wherever the latter is

possible.

Page 37: 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,

Ordered Output

Using iteration by recursion, return a similar list,

alphabetically ordered according to title.

Using iteration by means of <xsl:for-each>,

return a similar list, ordered according to calories

in descending order.

Page 38: 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,

Element and Attribute Construction

Return a similar list, with title as attribute and

calories as content.

Return a list, inside an element <recipes>, of recipes,

where each recipe contains the title and the top level

ingredients, while dropping the lower level ingredients.

Page 39: 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,

Inverting a Hierarchy

Documents may represent many-many relationships:•students enrolled in courses•ingredients used in recipes

Documents are hierarchical One entity is above the other:

e.g., courses above students

How can we invert the hierarchy?

E.g., list students with their courses?

Need:

1.List of students without duplicates

2.For each student, all courses where enrolled (same for ingredients and recipes)

Page 40: 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,

A Sorted List of Ingredients w/o Duplicates

<xsl:template match="recipes"> <xsl:for-each select="recipe//ingredient"> <xsl:sort select="@name" /> <xsl:if test=

"not(@name = preceding::ingredient/@name)”> <ingredient> <xsl:copy-of select="@name"/> </ingredient> </xsl:if> </xsl:for-each></xsl:template>

We ensure that only ingredients are outputthat have not appeared before

This test for duplicates can be expensive!

Page 41: 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,

XPath Predicates Can Replace <xsl:if>

<xsl:for-each> with a path predicaterealizes the test

Page 42: 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,

Inverting a Hierarchy (cntd.)

How can we find •for an ingredient•all recipes that have an ingredient with the same name?

This condition talks about three things at the same time:•an ingredient•a recipe•another ingredient with the same name as the first.

This condition cannot be expressed in XPath 1.0

… which can only talk about two things at the same time

we need a variable to remember the ingredient name

42

Page 43: 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,

Recipes Using an Ingredient (1)“named” template can be called using the name

Page 44: 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,

Recipes Using an Ingredient (2)

calling a named template

eliminate duplicates

adding a parameterto the call

Page 45: 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,

Recipes Using an Ingredient (3)

context node =context node at call

(no matching)makes the parameterof the call available

$ingr-name is a reference to the

paramenter

Page 46: 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,

Duplicate Elimination à 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" use="@name"/>

<xsl:key name="recipes-by-title" match="recipe“ use="title"/>

* Invented by Steve Muench, called the “Muenchian Method” in the XSLT world

<xsl:key> 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!

Page 47: 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,

Duplicate Elimination à la Muench

Step 2: Iterate over the recipes …

<xsl:template match="/"> <result> <xsl:apply-templates select="/recipes"/> </result></xsl:template>

Page 48: 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,

Duplicate Elimination à la Muench

<xsl:template match="recipes"> <xsl:for-each select="recipe//ingredient [count(. | key('ingredients-by-name',

@name)[1]) = 1]"> <xsl:sort select="@name" /> <ingredient> <xsl:copy-of select="@name"/> <xsl:for-each select="key('recipes-by-title',

ancestor::recipe/title)"> <xsl:copy> <xsl:copy-of select="title"/> </xsl:copy> </xsl:for-each> </ingredient> </xsl:for-each></xsl:template>

select those ingredients that occur as the first element in their index groupthe others are redundant …

sort ingredients by name,then retrieve recipesfrom their index

Page 49: 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,

Duplicate Elimination Corrected (1)

Step 1: Construct keys (= indices for node sets)

<xsl:key name="ingredients-by-name" match="ingredient" use="@name"/>

<xsl:key name="recipes-by-ingredient" match="recipe" use=".//ingredient/@name"/>

* Invented by Steve Muench, called the “Muenchian Method” in the XSLT world

Page 50: 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,

Duplicate Elimination Corrected (2)

Step 2: Iterate over the recipes …

<xsl:template match="/"> <result> <xsl:apply-templates select="/recipes"/> </result></xsl:template>

Page 51: 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,

Duplicate Elimination Corrected (3)

<xsl:template match="recipes"> <xsl:for-each select="recipe//ingredient [count(. | key('ingredients-by-name',

@name)[1]) = 1]"> <xsl:sort select="@name" /> <ingredient> <xsl:copy-of select="@name"/> <xsl:for-each select="key('recipes-by-ingredient',

@name)"> <xsl:copy> <xsl:copy-of select="title"/> </xsl:copy> </xsl:for-each> </ingredient> </xsl:for-each></xsl:template>

sort ingredients by name,then retrieve recipesfrom their indexaccording to their ingredients

Page 52: 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,

Grouping in XSLT 2.0

<xsl:template match="/">

<uses>

<xsl:for-each-group select="//ingredient"

group-by="@name">

<xsl:sort select="@name"/>

<use name="{current-grouping-key()}"

count="{count(current-group())}"/>

</xsl:for-each-group>

</uses>

</xsl:template>

Page 53: 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,

Restructuring Using Group-By

Page 54: 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,

Named Templates Give us Functions

Named templates can call each other recursively•parameters pass intermediate data•variables store local data

Moreover, XSLT has conditionals

•<xsl:if>•<xsl:choose> <xsl:when …> … </xsl:when …> <xsl:otherwise …> … </xsl:otherwise …><xsl:choose>

all that is needed for a Turing-complete language

54

Page 55: 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,

Fibonacci in XSLT (1)

<xsl:template match="/">

<xsl:call-template name="fib">

<xsl:with-param name="n" select="10" />

</xsl:call-template>

</xsl:template>

Page 56: 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,

Fibonacci in XSLT (2)

<xsl:template name="fib">

<xsl:param name="n" />

<xsl:choose>

<xsl:when test="$n le 1">

<xsl:value-of select="1" />

</xsl:when>

<xsl:otherwise>

<xsl:variable name="f1">

<xsl:call-template name="fib">

<xsl:with-param name="n" select="$n -1" />

</xsl:call-template>

</xsl:variable>

Page 57: 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,

Fibonacci in XSLT (3)

<xsl:variable name="f2">

<xsl:call-template name="fib">

<xsl:with-param name="n" select="$n-2" />

</xsl:call-template>

</xsl:variable>

<xsl:value-of select="$f1+$f2" />

</xsl:otherwise>

</xsl:choose>

</xsl:template>

Page 58: 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,

XSLT Variables

A (local or global) variable is a (name,value) pair. It may be defined •either as the result of an XPath expression

<xsl:variable name=’pi’ value=’3.14116’/> <xsl:variable name=’children’ value=’//child’/>

•or as the content of the <xsl:variable> element, as in the Fibonacci code •A variable has a scope (all its siblings, and their descendants) and cannot be redefined within this scope. •Recursive use of a template is the only way for a variable or a parameter to vary, for each different instantiation of the template.

58

Page 59: 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,

XSLT for Recipes (1/6)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/"> <html> <head> <title>My Best Recipes</title> </head> <body> <table border="1"> <xsl:apply-templates select="recipes/recipe"/>

</table> </body> </html></xsl:template>

Page 60: 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,

XSLT for Recipes (2/6)

<xsl:template match="recipe">

<tr>

<td>

<h1><xsl:value-of select="title"/></h1>

<ul><xsl:apply-templates select="ingredient"/></ul>

<xsl:apply-templates select="preparation"/>

<xsl:apply-templates select="comment"/>

<xsl:apply-templates select="nutrition"/>

</td>

</tr>

</xsl:template>

Page 61: 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,

XSLT for Recipes (3/6)

<xsl:template match="ingredient"> <xsl:choose> <xsl:when test="@amount"> <li> <xsl:if test="@amount!='*'"> <xsl:value-of select="@amount"/> <xsl:text> </xsl:text> <xsl:if test="@unit"> <xsl:value-of select="@unit"/> <xsl:if test="number(@amount)>number(1)"> <xsl:text>s</xsl:text> </xsl:if> <xsl:text> of </xsl:text> </xsl:if> </xsl:if> <xsl:text> </xsl:text> <xsl:value-of select="@name"/> </li> </xsl:when>

Page 62: 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,

XSLT for Recipes (4/6)

<xsl:otherwise>

<li><xsl:value-of select="@name"/></li>

<ul><xsl:apply-templates select="ingredient"/></ul>

<xsl:apply-templates select="preparation"/>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

Page 63: 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,

XSLT for Recipes (5/6)

<xsl:template match="preparation"> <ol><xsl:apply-templates select="step"/></ol></xsl:template>

<xsl:template match="step"> <li><xsl:value-of select="node()"/></li></xsl:template>

<xsl:template match="comment"> <ul> <li type="square">

<xsl:value-of select="node()"/> </li> </ul></xsl:template>

Page 64: 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,

XSLT for Recipes (6/6)<xsl:template match="nutrition"> <table border="2"> <tr>

<th>Calories</th><th>Fat</th><th>Carbohydrates</th><th>Protein</th>

<xsl:if test="@alcohol"><th>Alcohol</th></xsl:if> </tr> <tr> <td align="right"><xsl:value-of select="@calories"/></td> <td align="right"><xsl:value-of select="@fat"/></td> <td align="right"><xsl:value-of

select="@carbohydrates"/></td> <td align="right"><xsl:value-of

select="@protein"/></td> <xsl:if test="@alcohol"> <td align="right"><xsl:value-of select="@alcohol"/></td> </xsl:if> </tr> </table></xsl:template>

Page 65: 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,

A Different View

<xsl:template match="/"> <nutrition> <xsl:apply-templates select="recipes/recipe"/> </nutrition></xsl:template>

<xsl:template match="recipe"><dish name="{title/text()}" calories="{nutrition/@calories}" fat="{nutrition/@fat}" carbohydrates="{nutrition/@carbohydrates}" protein="{nutrition/@protein}" alcohol="{if (nutrition/@alcohol) then nutrition/@alcohol else '0%'}"/></xsl:template>

Page 66: 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,

The Output

<?xml version="1.0" encoding="iso-8859-1"?><nutrition> <dish name="Beef Parmesan with Garlic Angel Hair Pasta"

calories="1167" fat="23" carbohydrates="45" protein="32" alcohol="0%"/>

<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%"/></nutrition>