Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 XSL – XML Stylesheet Language. 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO.

Similar presentations


Presentation on theme: "1 XSL – XML Stylesheet Language. 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO."— Presentation transcript:

1 1 XSL – XML Stylesheet Language

2 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO (FO for formatting objects) This is a rather complex language for typesetting (i.e., preparing text for printing) It will not be taught

3 3 XPath A Language for Locating Nodes in XML Documents

4 4 XPath XPath expressions are written in a syntax that resembles paths in file systems The list of nodes located by an XPath expression is called a Nodelist XPath is used in XSL and in XQuery (a query language for XML) W3Schools has an XPath tutorialXPath tutorial XPath includes –Axis navigation –Conditions –Functions

5 5 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 An XML document

6 6 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK USA Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 The XML document as a DOM tree The XML document as a DOM tree

7 7 The Main Idea in the Syntax of XPath Expressoins “/” at the beginning of an XPath expression represents the root of the document “/” between element names represents a parent-child relationship “//” represents an ancestor-descendent relationship “@” marks an attribute “[condition]” specifies a condition

8 8 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog Getting the root element of the document USA

9 9 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd Finding child nodes USA

10 10 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd/price Finding descendent nodes USA

11 11 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[price<10] Condition on elements USA

12 12 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 //title // represents any directed path in the document /catalog//title USA

13 13 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd/* * represents any element name in the document USA

14 14 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /*/* What will the following expressions return? //* //*[price=9.90] //*[price=9.90]/* USA * represents any element name in the document

15 15 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[1] Position based condition /catalog/cd[last()] USA

16 16 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[@country=“UK”] @ marks attributes USA

17 17 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 @ marks attributes USA /catalog/cd/@country

18 18 Relative Navigation Using Axes Starts with the current node and not with the root (/) A. marks the current node (e.g.,./title) A.. marks the parent node (e.g., title/../*) There are also other axes, e.g., child, descendent, ancestor, parent, following- sibling, etc.

19 19 Functions Many functions that are included in XPath Some examples: –count() – returns the number of nodes in a nodelist –last() – returns the last node in a nodelist –name() – returns the name of a node –position() – returns the position of the node in the nodelist

20 20 Additional Examples of XPath Expressions These examples use element names that are not necessarily from the XML document that was shown previously

21 21 Examples of XPath Expressions para –Selects the para children elements of the context node * –Selects all element children of the context node text() –Selects all text node children of the context node @name –Selects the name attribute of the context node

22 22 More Examples of XPath Expressions @* –Selects all the attributes of the context node para[1] –Selects the first para child of the context node para[last()] –Selects the last para child of the context node */para –Selects all para grandchilren of the context node

23 23 More Examples of XPath Expressions /doc/chapter[5]/section[2] –Selects the second section of the fifth chapter of the doc chapter//para –Selects the para element descendants of the chapter element children of the context node //para –Selects all the para descendants of the document root and thus selects all para elements in the same document as the context node

24 24 More Examples of XPath Expressions //olist/item –Selects all the item elements that have an olist parent and are in the same document as the context node. –Selects the context node.//para –Selects the para descendants of the context node.. –Selects the parent of the context node

25 25 More Examples of XPath Expressions../@lang –Selects the lang attribute of the parent of the context node para[@type=“warning”] –Selects the para children of the context node that have a type attribute with value warning chapter[title] –Selects the chapter children of the context node that have one or more title children

26 26 More Examples of XPath Expressions para[@type=“warning”][5] –Selects the fifth para child among the children of the context node that have a type attribute with value warning para[5][@type=“warning”] –Selects the fifth para child of the context node if that child has a type attribute with value warning

27 27 More Examples of XPath Expressions Chpater[title=“Introduction”] –Selects the chapter children of the context node that have one or more title children with string-value equal to Introduction employee[@secretary and @assistant] –Selects employee children of the context node that have both a secretary attribute and an assistant attribute

28 28 More Examples of Xpath Expressions /university/department/course –This Xpath expression matches any path that starts at the root, which is a university element, passes through a department element and ends in a course element./department/course[@year=2002] –This Xpath expression matches any path that starts at the current element, continues to a child which is a department element and ends at a course element with a year attribute that is equal to 2002

29 29 Location Paths The previous examples are abbreviations of location paths –See XPath tutorial in W3Schools or Costello’s slides on long names XPath tutorial For example, // is short for /descendant-or-self::node()/. //para is short for /descendant-or-self::node()/child::para

30 30 XSLT Transforming XML documents into other XML documents

31 31 XSLT Stylesheet An XSLT stylesheet is a program that transforms an XML document into another XML document For example: –Transforming XML to XHTML (HTML the conforms to XML syntax) –Transforming an XML document to WML (a format of XML that cellular phones can display)

32 32 A Few Things About XSL XSL is a high-level, functional language The syntax is a bit peculiar and possibly confusing An XSL style sheet is a valid XML document –Valid with respect to the XSL namespace Therefore, commands in XSL are XSL elements

33 33 Applying XSLT Stylesheets to XML Documents There are three ways of applying an XSLT stylesheet to an XML document –Directly applying an XSLT processor to the XML document and the XSLT stylesheet –Calling an XSLT processor from within a (Java) program –Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation

34 34 Using an XSL Processor XSL Processor XML document XML document XSL stylesheet XSL stylesheet Result is either an XML, HTML or text document Result is either an XML, HTML or text document java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html Directly applying the Xalan XSL processor

35 35 Letting a Browser Perform the Transformation <?xml-stylesheet type="text/xsl" href=“catalog.xsl"?> Dark Side of the Moon Pink Floyd 10.90 … <?xml-stylesheet type="text/xsl" href=“catalog.xsl"?> Dark Side of the Moon Pink Floyd 10.90 … A link to the stylesheet

36 36 The Root of the XSL Document The Root of the XSL document should be one of the following lines: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> The namespace allows the XSL processor to distinguish between XSL tags and tags of the result document

37 37 How Does XSLT Work? An XSL stylesheet is a collection of templates, such that each template –Matches some nodes of the XML document –When there is a match, the elements inside the template are written to the output –Some of the elements inside the template are XSL elements (i.e., commands) that use XPath expressions in order to extract data from the XML document –The extracted data is written to the output

38 38 Templates A template is an XSL element The match attribute of the element contains an XPath expression For example, if match="/" is specified, then the template matches a single node of the XML document, namely, the root Many useful XSL stylesheets can be written using a single template that matches the root

39 39 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> Hello World <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> Hello World

40 40 Applying a browser to catalog.xml (catalog.xml has a link to catalog.xsl)

41 41 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:template match=“cd[title=‘Space Oddity’]"> Hello World <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:template match=“cd[title=‘Space Oddity’]"> Hello World The XPath expression is different from the previous one, but this stylesheet still gives the same result as the previous one (when applied to the same XML document as before)

42 42 The Most Frequently Used Elements of XSL –This element extracts the value of a node from the nodelist located by xpath-expression –This element loops over all the nodes in the nodelist located by xpath-expression,, etc. –This element is for conditional processing

43 43 The Element The XSL element can be used to extract the value of an element that is selected from the input XML document The extracted value is added to the output stream The selected element is located by an XPath expression that appears as the value of the select attribute

44 44 Selected values

45 45 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist

46 46 <xsl:value-of select="catalog/cd/title"/> <xsl:value-of select="catalog/cd/artist"/> <xsl:value-of select="catalog/cd/title"/> <xsl:value-of select="catalog/cd/artist"/> Note that only the first matched elements are retrieved

47 47 The Element The element loops over all the nodes in the nodelist of the XPath expression that appears as the value of the select attribute The value of each node can be extracted by an element

48 48 All the values are selected

49 49 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist As in the previous example

50 50 Note that all the /catalog/cd elements are retrieved

51 51 <xsl:for-each select="catalog/cd[price<10]"> <xsl:for-each select="catalog/cd[price<10]"> Only elements that satisfy /catalog/cd[price<10] are retrieved Consider the following change in the select attribute:

52 52

53 53 The Element The element is used to sort the list of nodes that are looped over by the element Thus, the must appear inside the element The looping is done in sorted order

54 54 Sorted by the name of the artist

55 55 The /catalog/cd elements are sorted according to the value of the artist element

56 56 The Element The element is used for conditional processing The condition appears as the value of the test attribute, for example: some output... The elements inside the element are processed if the condition is true

57 57 Note Processing the inside elements means –Copying them into the output stream if they are not XSL elements, and –Evaluating them if they are XSL elements If the value of the test attribute is just an XPath expression (i.e., without any comparison), then the test is satisfied if the nodelist of this XPath expression is not empty

58 58 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog Title Artist As in the previous examples

59 59 Only /catalog/cd with price>10 are retrieved

60 60

61 61 Question In the previous example, what should be the condition if we want to extract the information for all cd’s that have a price (i.e., the information about the price is not missing)?

62 62 The Element The element is used in conjunction with and to express multiple conditional tests There can be many inside an element, but there should be a single inside an element

63 63 Using To insert a conditional choose test against the content of the XML file, simply add the,, and elements to your XSL document like this:... some code...... some code....

64 64 <xsl:when test="price>9 and price<=10"> <xsl:when test="price>9 and price<=10">

65 65

66 66 The Element Templates can be applied recursively to children or descendants If doesn’t have the select attribute, then all templates are applied to the children (in document order) of the current node If there is a select attribute, then all templates are applied to the nodelist of the XPath expression that appears as the value of the select attribute

67 67 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> A CD Catalog

68 68 Title: Title:

69 69 Artist: Artist:

70 70

71 71 Default Templates If the XSL stylesheet doesn’t have any template that can be applied, then the following two default templates are applied

72 72 Is Recursive Application of Templates Really Needed? The output of the previous example can also be generated by an XSL stylesheet that uses only one template that matches the root (and does not use the element ) However, some tasks can only be done by applying templates recursively –This typically happens when the structure of the input XML document is not known

73 73 For example Suppose that we want to write an XSL stylesheet that generates an exact copy of the input XML document –It is rather easy to do it when the structure of the input XML document is known Can we write an XSL stylesheet that does it for every possible XML document? –Yes! (see next slide)

74 74 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Identity Transformation Stylesheet

75 75 The Element <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>...... Tells in what format the output should be: xml/html/text

76 76 Some Other XSL Elements The element allows to insert free text in the output The element creates a copy of the current node The element is used to create a comment node in the result tree There are more elements and functions: look in the specification!

77 77 Costello’s Tutorial (See DBI Lecture Schedule for a reference) Costello has an excellent and detailed tutorial –First part (xsl01.ppt) has many examples that do not use recursion –Second part (xsl02.ppt) explains how to use recursion and why it is needed (the identity transformation shown earlier is from Costello’s tutorial)

78 78 W3Schools Tutorial on XSLT The W3Schools XSLT Tutorial has (among other things) tables that list all the elements and functions of XSLTW3Schools XSLT Tutorial It also has some details about implementations –Some browsers may not implement all features or may implement some features differently from the specifications

79 79 How to Use the Examples Since XSL is a high-level language, XSLT stylesheets are relatively short and easy to understand It is easy to modify an existing style- sheet into another stylesheet with a similar functionality –The many examples of Costello and W3Schools are very useful as a starting point for writing your own stylesheets

80 80 Summary XSLT is a high-level transformation language Create core output once in XML format (using Servlets, JSP, etc.) Use XSLT to transform the core output as needed

81 81 For Example Suppose that you have produced an XML catalog of products Now you decide to lower all prices by 10% It is easy to do it in XSLT –Modify the identity transformation to do that


Download ppt "1 XSL – XML Stylesheet Language. 2 XSL XSL = XML Stylesheet Language XSL cosists of –XPath (navigation in documents) –XSLT (T for transformations) –XSLFO."

Similar presentations


Ads by Google