Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 XML Data Management XLST Werner Nutt

2 A Hello World! Stylesheet <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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

3 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.

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

5 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-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd" indent="yes" />

6 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

7 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

8 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 and @ axes, and the abbreviation //. Predicates are allowed.

9 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

10 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.

11 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" />

12 The Mechanism... Jim Joe Jim Joe

13 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.

14 The Execution Model: Illustration

15

16

17

18

19

20 “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"/> Copy each title Iterate over all title elements

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

22 Identity Map Deep copy Shallow copy

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

24 Element Construction Dynamic element constructor

25 Copying Attributes Copying attributes leads to element fusion

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

27 Computing Attribute Values Expressions in {braces} will be evaluated

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

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

30 Nested Iteration with Template Calls Root calls recipes Recipes calls recipe

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

32 Sorted List of All Ingredients can be nested into

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

34 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.

35 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.

36 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.

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

38 XSLT for Recipes (2/6)

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

40 XSLT for Recipes (4/6)

41 XSLT for Recipes (5/6)

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

43 A Different View <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%'}"/>

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

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

46 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" use="@name"/> <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!

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

48 Duplicate Elimination à la Muench <xsl:for-each select="recipe//ingredient [count(. | key('ingredients-by-name', @name)[1]) = 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

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

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

51 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.

52 Restructuring the Countries Document (1) <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"/>

53 Restructuring the Countries Document (2)

54 Restructuring the Countries Document (3)

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

56 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?

57 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 $c/@name return {$c/@name} {attribute speakers {xs:int(($c/@population div 100) * $c/language[.=$l]/@percentage) }} } language is remembered in variable $l

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

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

60 Languages Spoken in Countries: XSLT <xsl:attribute name="speakers" select="format-number( (@population div 100) * $language/@percentage,'0')"/> 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


Download ppt "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."

Similar presentations


Ads by Google