Download presentation
Presentation is loading. Please wait.
Published byAndrew Johns Modified over 9 years ago
1
+ XSL eXtensible Stylesheet Language
2
+ 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading
3
+ Outline Basic Overview. Applying XLST Stylesheets. Using a XSL Processor. Example XSL - generating HTML. Creating a Stylesheet, Template rules Patterns. Some XSL Syntax. Summary.
4
+ XSL XSL is a family of recommendations for defining XML document transformation and presentation. It consists of three parts: XSL Transformations (XSLT) a language for transforming XML, The XML Path Language (XPath), an expression language used by XSLT to access or refer to parts of an XML document. XSL Formatting Objects (XSL-FO) an XML vocabulary for specifying formatting semantics. An XSLT stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses a formatting vocabulary, such as (X)HTML or XSL-FO.
5
+ XSL (eXtensible Stylesheet Language) XSL is a high-level functional language used to transform XML documents into various formats, e.g. XML, HTML etc.. An XSL program consists of a set of TEMPLATE rules. Each rule consists of a pattern and a template: The XSL processor starts from the root element and tries to apply a pattern to that node; If it succeeds, it executes the corresponding template. The template, when executed, usually instructs the processor to produce some XML result and to apply the templates, Recursively on the node's children. An XSL style sheet is a valid XML document
6
+ 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
7
+ 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. This is what we will use
8
+ Letting a Browser Perform the Transformation Dark Side of the Moon Pink Floyd 10.90 … Dark Side of the Moon Pink Floyd 10.90 … A link to the stylesheet
9
+ The Root of the XSL Document (program) 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
10
+ 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 document). Each template has a match attribute that specifies to which source nodes the template can be applied. The current source node is processed by applying a template that matches this node. Processing always starts at the root (/).
11
+ Templates A template has the form:... Template content... The content of a template consists of: XML elements and text (HTML, etc) that are copied to the result, XSL elements that are actually instructions. The pattern syntax is a subset of Xpath.
12
<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 Single Template XSL program
13
Applying a browser to catalog.xml (catalog.xml has a link to catalog.xsl) Hello World
14
+ Focus on Generating HTML HTML XML (content) XSL (presentation) XSL Processor
15
+ Transformation Language XSL is a transformation language it transforms a document written in one language (XML) into a document of another language (e.g., HTML). Transformation Engine (XSL Parser) XSL XML HTML
16
+ Recall XML Recall that an XML document is composed of ‘elements’. For a BookCatalogue XML document we have the following elements: BookCatalogue (the ‘root’ element), Book, Title, Author, Date, ISBN, and Publisher.
17
+ XSL - all about (Template) “Rules” Let the XSL processor know when it encounters the root element (e.g., BookCatalogue) do [action1]. Let the XSL processor know when it encounters the Book element do [action2]. Let the XSL processor know when it encounters the Title element do [action3]. And so forth…
18
+ XSL - all about (Template) “Rules” Each template rule has two parts: A pattern or matching part, that identifies the XML node in the source document to which the action part is to be applied: Matching information is contained in an attribute. An action or formatting, styling and processing part that details the transformation and styling of the resulting node
19
+ XSL Document Structure xsl:stylesheet template rule for the document (template rule for a child element)* action on the document action on the child element
20
+ XSL Document Structure [action] [action] [action]...
21
+ Things to Note XSL documents are full-fledged XML documents: As a consequence, if in [action] you want to, say, output an HTML paragraph break you must have both and : Even though in an HTML document the closing element ( ) is optional, Note: an XML all elements must have a closing element. The root element of all XSL documents is xsl:stylesheet To indicate that a template rule is to be applied to the XML document you use “/” * Alternatively,
22
+ Template Rules Template rules take the following general form: [ action ]
23
+ Template Rules (Example) The XSL processor, as it parses through the XML document and gets to a element use this template rule.” “Go to each of my children and apply the template rules to them.”
24
+ Example - Create XSL for BookCatalogue.xml My Life and Times Paul McCartney July, 1998 94303-12021-43892 McMillin Publishing Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co. The First and Last Freedom J. Krishnamurti 1954 0-06-064831-7 Harper & Row
25
+ First Example This example will show how the XSL Processor parses an XML document and uses the template rules defined in the XSL document to do something for each thing that is found in the XML document.
26
+ Terminology In BookCatalogue.xml we have (snippet): My Life and Times Paul McCartney July, 1998 94303-12021-43892 McMillin Publishing... “Book is a child element of the BookCatalogue element. Title, Author, Date, ISBN, and Publisher are children elements of the Book element.”
27
+ Creating a Stylesheet - Step 1 Draw a tree diagram of your XML document. Document / DocumentType Element BookCatalogue Element Book Element Book Element Book Element Title Element Author Element Date Element ISBN Element Publisher... Text My Life... Text Paul McCartney Text July, 1998 Text 94303- 12021-43892 Text McMillin Publishing
28
+ Creating a Stylesheet - Step 2 Create a template rule for every type of node in your tree: Except for the DOCTYPE node: The current specification does not allow you to have a template rule for the DOCTYPE node. e.g., create one template rule for all the nodes, not one template rule for each of the nodes.
29
+
30
+ Explanation “The XSL processor returns the value of the thing that is selected here” In this example the “thing” that is selected is a text node, so the “thing” that is returned is the value of the text node, i.e., the text.
31
+ Creating a Stylesheet - Step 3 For those nodes in the tree that we want to immediately return (i.e., not process any of its children): remove remove the template rules for the child nodes - these template rules will never be used, so why have them? For our example, we want the XSL processor to traverse the entire XML document tree so we do not do this step.
32
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" > Book Catalogue added these Put an wrapper around the content
33
+ Creating a Stylesheet - Step 5 Tell the XSL Processor that when it encounters a element to output: “I am at a Book element. Processing its children now.” Do this for each element. Before running your XSL document through the XSL Processor, check it for well-formed-ness.
34
+ Book Catalogue I am at BookCatalogue. Processing its children now. I am at Book. Processing its children now. I am at Title. Here's the title: I am at Author. Here's the author's name: I am at Date. Here's the date: I am at ISBN. Here's the ISBN: I am at Publisher. Here's the publisher: BookCatalogue1b.xsl
35
+ Default Template Rules Every XSL document has two default template rules. These rules are applied when the XSL Processor cannot find a template rule to use from what was written. Here are the two default template rules: “Match on the document or any element. Return the result of applying the template rules to my children.” “Match on a text node. Return the value of the text node, i.e., the text.”
36
+ Multiple Applicable Rules Suppose that the XSL Processor is processing BookCatalogue and it gets to the element. Why does it use and not the default template rule after all, both apply! Answer: given two rules that apply, the more specific rule wins: “*” is much more general than “Book”. “*” matches on any element. “Book” just matches on the Book element.
37
+ Example 2 The XSL Processor, when it encounters a book element output: ‘ Here is a book: ’ and then the data in its children elements. This is what we want XSLProcessor to send to BookCatalogue.html. Book Catalogue Here is a book: all the data from the first book Here is a book: all the data from the second book Here is a book: all of the data from the third book
38
+ BookCatalogue2.xsl Book Catalogue Here is a book:
39
+ Put the Data into an HTML Table Book Catalogue
40
+ Single Rule Applied to Multiple Elements Notice in the last example that a single rule is applied to multiple elements: The xsl:apply-templates element (without an attribute) tells the XSL Processor to apply the template rules to all children The xsl:apply-templates element can have an attribute that tells the XSL Processor to process only the child element that matches “pattern”: This apply-templates rule allows us to specify the order in which the children are processed.
41
+ Specify the order of the table contents Book Catalogue Process the Author element first (in previous examples the Title element was processed first)
42
+ [action] This says that for every Book element, do [action]
43
+ For each Book do... Book Catalogue For each Book create a row and process the Book’s children
44
+ Patterns So far we have seen very simple patterns - simply match against an element name. XSL provides a rich pattern matching capability. “When you start processing the document do...” “When you get to a Book element do...” “When you get to either a Title element or an Author element do...” match document match by name match several names
45
+ Patterns “When you get to a Title element that has a Book element as a parent do...” “When you get to a Title element that has a BookCatalogue as an ancestor do...” “When you get to any element that is an immediate child of Book do....” “When you get to an element that has an id “J.K.” do...” match with immediate ancestor match with an ancestor wildcard match by id
46
+ Patterns (matching by attribute) “When you get to a para element do...” “When you get to a para element that has an attribute called type do...” “When you get to a para element that has an attribute called type and its value is ‘opening’ do...” “When you get to a para element that has an attribute called type whose value is ‘opening’ and the element has a parent called chapter and it has an attribute called num that has a value ‘ch1’ do...
47
+ Patterns (matching by position) “When you get to the first Book element do...” “When you get to the last Book element do...” “Use this rule for all but the last Book element” Other position qualifiers: not(first-of-type()) first-of-any()“The first child element of any type” not(first-of-any()) last-of-any() not(last-of-any())
48
+ Named Attribute Sets XSL allows you to create a set of attributes and assign a name to it: What value is this? Reuse! +1 blue Palatino
49
+ +1 blue Palatino Book Catalogue Use the named attribute set Define the named attribute set
50
+ Numbering XSL has the capability to number your elements:. This will print out a number followed by a period followed by the results of processing the Title element. The number starts at one and increments each time this template rule is instantiated.
51
+ Creating a Numbered Title List Book Catalogue. Here is where we use the number capability
52
+ Sorting Sorting is achieved by adding xsl:sort elements as children of xsl:apply-templates or xsl:for-each. The first xsl:sort child specifies the primary sort key, the second xsl:sort specifies the secondary sort key, and so on..
53
+ if test TO Title -
54
+ A Common Mistake ”> Wrong Correct Attributes values cannot contain “ ” Scenario: suppose that the element Book has an id attribute. We would like to create a hyperlink using the value of the id attribute as the value of the hyperlink’s href.
55
+ 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.