Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 1 XSL Transformations (XSLT)

2 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath to navigate in XML documents. An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input (XML) documents.

3 3 XSLT is rule-based XSLT is different from conventional programming languages because XSLT is based on template rules which specify how XML documents should be processed. The stylesheet declares what output should be produced when a pattern in the XML document is matched.

4 4 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml

5 5 Style Sheet Declaration The root element that declares the document to be an XSL style sheet is or. Note: and are completely synonymous and either can be used! The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is: or:

6 6 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl catalog.xsl Includes XHTML elements

7 7 How Does XSLT Work? An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML) Each template has a match attribute that specifies to which source nodes the template can be applied Each source node has a template that matches it The current source node is processed by applying a template that matches this node When processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node The XSLT processor processes the document root ( / )

8 8 Templates A template has the form... The content of a template consists of -XML elements (e.g., XHTML) and text that are copied to the result -XSL elements ( ) that are actually instructions The pattern syntax is a subset of XPath

9 9 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! catalog1.xsl

10 10 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml

11 11

12 12 match The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document). If the path starts with a slash ( / ) it always represents an absolute path to an element!

13 13 Examples of Match Attributes match="cd", -All elements with tag name cd match="//cd", match="/catalog/cd/artist" -All matches of the absolute XPath match="cd/artist" -All artist nodes that have a cd parent match="catalog//artist" -All artist nodes that have a catalog ancestor match="cd[@country='UK']/artist"

14 14 My CD Collection Title Artist..

15 15 Frequently Used Elements of XSL -This element extracts the value of a node from the node list located by xpath -This element loops over all the nodes in the node list located by xpath 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

16 16 My CD Collection Title Artist

17 17 My CD Collection Title Artist

18 18 My CD Collection Title Artist

19 19 My CD Collection Title Artist

20 20,, etc. -This element is for conditional processing The element is used in conjunction with and to express multiple conditional tests. Frequently Used Elements of XSL (cont)

21 21 My CD Collection Title Artist

22 22

23 23 Tells the processor to continue processing other nodes in the input document and instantiate their matching templates. Processing starts by applying a template to the root. -If no specified template matches the root, then one is inserted by default (see slide 25) <xsl:apply-templates>

24 24 (cont) (cont) The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node. This is done by putting inside a template the instruction: In xpath, the current processed node is used as the context node Without the select attribute, this instruction processes all the children of the current node (including text nodes).

25 25 Default Templates XSL provides implicit built-in templates that match every element and text nodes. Templates we write always override these built-in templates (when they match). selects from the root node and matches any element node matches text & attributes nodes

26 26 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 … No template! default template is used

27 27 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd! The template applies to all of the CDs.

28 28 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd! Aretha: Lady Soul Aretha Franklin 9.90 The template doesn’t apply to Aretha Franklin’s CD, default template used here.

29 29 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="catalog/cd[@country='UK']/artist"/> An artist! An artist! Matches the root – default template will not be applied instead of outputting a fixed response, it instantiates the template for each matching artist element

30 30 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="cd[@country='UK']/artist"/> An artist! No match! “catalog” is missing Matches the root – default template will not be applied

31 31 Setting Values in Attributes We can insert expressions into attribute values, by putting the expression inside curly braces ( {} ). Alternatively, we can use in order to construct XML elements.

32 32 An Example In the following example, we add to each CD entitled ”title” a link to the URL /showcd.html?title= ”title” :

33 33

34 34 Using Using : showcd/?title=

35 35 On XSL Code Typically, an XSLT program can be written in several, very different ways -Templates can sometime replace loops and vice versa -Conditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versa A matter of convenience and elegancy

36 36 Identity Transformation Stylesheet Suppose that we want to write an XSL stylesheet that generates a copy of the source document -It is rather easy to do it when the structure of the source XML document is known Can we write an XSL stylesheet that does it for every possible XML document? -Yes! (see next slide)

37 37 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Identity Transformation Stylesheet defines the format of the output document any element node any attribute node

38 38 Generating Valid XHTML By default, the documents that XSL stylesheets generate are not valid XHTML Next, we will show how XSL stylesheet can be changed in order to generate valid XHTML

39 39 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! The Original XSL Example

40 40 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "/> … Modifying the XSL Example

41 41 cd catalog This is a cd catalog! The Transformation Result

42 42 Some Other XSL Elements The element inserts free text in the output The creates a copy of the specified nodes, to insert a result tree fragment into the result tree The element creates a comment node in the result tree The element defines a variable (local or global) that can be used within the program

43 43 The End! These slides are based on: Slides developed for the course: http://www.cs.huji.ac.il/~dbi http://www.cs.huji.ac.il/~dbi Tutorial: http://www.w3schools.com/xsl/


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

Similar presentations


Ads by Google