Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 XSL Transformations (part 1: Introduction)

Similar presentations


Presentation on theme: "Lecture 11 XSL Transformations (part 1: Introduction)"— Presentation transcript:

1 Lecture 11 XSL Transformations (part 1: Introduction)

2 What is XSL? Stylesheets are typically used to specify how something should be displayed/rendered. Stylesheets are typically used to specify how something should be displayed/rendered. They lead to separation of document's content from presentational information. They lead to separation of document's content from presentational information. The Extensible Stylesheet Language (XSL) includes two independent parts: a transformation language (XSLT) and formatting objects language (XSL:FO). The Extensible Stylesheet Language (XSL) includes two independent parts: a transformation language (XSLT) and formatting objects language (XSL:FO).

3 What is XSL? (contd.) Both XSLT and XSL:FO are XML applications. Both XSLT and XSL:FO are XML applications. XSLT provides elements that define rules for how one XML document is transformed into another text document. XSLT provides elements that define rules for how one XML document is transformed into another text document. XSL:FO XSL:FO Page oriented formatting syntax;Page oriented formatting syntax; A formatting object is an area with certain formatting properties;A formatting object is an area with certain formatting properties; Mostly used for PDF generation.Mostly used for PDF generation.

4 .xml XSLT Processor.xsl XSLT Stylesheet XSLT Processor.xml XML Document

5 .xml XSLT Processor.xsl XSLT Stylesheet XSLT Processor XML Document.html HTML Document

6 .xml XSLT Processor.xsl XSLT Stylesheet XSLT Processor XML Document.tex.rtf Text File (eventually containing formatting information)

7 XSLT Stylesheet (1) XML document Pictures from My Holiday Seaside town Sea port

8 XSLT Stylesheet (2) XML Document's Tree Text Node Element Node PictureSet Element Node title Element Node picture Element Node picture Element Node source Element Node source Element Node description Element Node thumbnail Attribute Node location: town.jpg Attribute Node location: port_t.jpg Attribute Node location: port.jpg Attribute Node width : 750 Attribute Node height : 544 Attribute Node height : 509 Attribute Node width : 878 Text Node Element Node description Root

9 XSLT Stylesheet (3) XSLT processor models an XML document as a tree that contains seven kind of nodes: the root, element, text, attribute, namespace, processing instruction, comment. XSLT processor models an XML document as a tree that contains seven kind of nodes: the root, element, text, attribute, namespace, processing instruction, comment. XSLT operates by transforming one XML tree into another XML tree by employing operators for selecting nodes from the tree, reordering the nodes, and outputting nodes. XSLT operates by transforming one XML tree into another XML tree by employing operators for selecting nodes from the tree, reordering the nodes, and outputting nodes. Original XML document An XML/HTML document XSL Transformation

10 XSLT Stylesheet (4) The Stylesheet picturegallery.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> I found PictureSet I found description template rule

11 Template Rules The XPath expression specifies a pattern to be matched by a node in the document's tree. The XPath expression specifies a pattern to be matched by a node in the document's tree. The substitution part specifies what should be inserted instead of the matched pattern: The substitution part specifies what should be inserted instead of the matched pattern: XSL elements, markup and textXSL elements, markup and text Substitution part General structure of an XSLT rule:

12 Template Rules (contd.) An XSLT processor transforms an XML document starting from the root of the document tree. An XSLT processor transforms an XML document starting from the root of the document tree. At each turn, a node from the document tree is compared to the template rules. At each turn, a node from the document tree is compared to the template rules. When the XSLT processor finds a template rule such that the current node matches its pattern then the XSLT processor outputs the template rule’s substitution part to the result document. When the XSLT processor finds a template rule such that the current node matches its pattern then the XSLT processor outputs the template rule’s substitution part to the result document. If the substitution part contains then at that point the child (element and text) nodes of the current node are processed one by one. This is a recursive process. If the substitution part contains then at that point the child (element and text) nodes of the current node are processed one by one. This is a recursive process.

13 Another Version of picturegallery.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> template rule

14 Default Template Rules Matches the document tree’s root node ( / ) and any element node ( * ) and applies template to their child nodes. Matches the document tree’s root node ( / ) and any element node ( * ) and applies template to their child nodes. Note that the document tree's root node is NOT the node that corresponds to the root element of the XML document. Note that the document tree's root node is NOT the node that corresponds to the root element of the XML document. Attribute and Namespace nodes are NOT considered child nodes. Only element, comment, text and processing instruction nodes can be child nodes. Attribute and Namespace nodes are NOT considered child nodes. Only element, comment, text and processing instruction nodes can be child nodes. Default Rule 1

15 Default Template Rules (contd.) matches any text node ( text() ) and any attribute node ( @* ) and outputs their values. matches any text node ( text() ) and any attribute node ( @* ) and outputs their values. Default Rule 2 matches any processing instruction and comment nodes and does not output anything to the result document (i.e., does not perform any action). matches any processing instruction and comment nodes and does not output anything to the result document (i.e., does not perform any action). Default Rule 3

16 Result of Applying the second version of picturegallery.xsl to the example XML Document Pictures from My Holiday Seaside town Busy street Sea port Question: What will be the result of applying the very simple stylesheet <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> to the same XML document? Why?


Download ppt "Lecture 11 XSL Transformations (part 1: Introduction)"

Similar presentations


Ads by Google