+ 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading
+ Outline Basic Overview. Applying XLST Stylesheets. Using a XSL Processor. Example XSL - generating HTML. Creating a Stylesheet, Template rules Patterns. Some XSL Syntax. Summary.
+ 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.
+ 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
+ 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
+ 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
+ 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
+ The Root of the XSL Document (program) The Root of the XSL document should be one of the following lines: The namespace allows the XSL processor to distinguish between XSL tags and tags of the result document
+ 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 (/).
+ 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 Hello World Hello World Single Template XSL program
Applying a browser to catalog.xml (catalog.xml has a link to catalog.xsl) Hello World
+ Focus on Generating HTML HTML XML (content) XSL (presentation) XSL Processor
+ 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
+ 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.
+ 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…
+ 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
+ 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
+ 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,
+ Template Rules Template rules take the following general form: [ action ]
+ 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.”
+ 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
+ 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.
+ 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.”
+ 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
+ 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.
+ 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.
+ 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.
+ Book Catalogue added these Put an wrapper around the content
+ 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.
+ 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
+ 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.”
+ 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.
+ 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
+ BookCatalogue2.xsl Book Catalogue Here is a book:
+ Put the Data into an HTML Table Book Catalogue
+ 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.
+ Specify the order of the table contents Book Catalogue Process the Author element first (in previous examples the Title element was processed first)
+ [action] This says that for every Book element, do [action]
+ For each Book do... Book Catalogue For each Book create a row and process the Book’s children
+ 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
+ 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
+ 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...
+ 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())
+ 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
+ +1 blue Palatino Book Catalogue Use the named attribute set Define the named attribute set
+ 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.
+ Creating a Numbered Title List Book Catalogue. Here is where we use the number capability
+ 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..
+ 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.
+ 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.