Lecture_08_XSL.pptx
- Количество слайдов: 59
Lecture 8: XSL 2206 FIS Nurlan Karimzhan
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 (e. Xtensible 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
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.
Using an XSL Processor XSL stylesheet XML document XSL Processor java org. apache. xalan. xslt. Process -IN my. Xml. File. xml -XSL my. Xsl. File. xsl -OUT my. Output. File. html Directly applying the Xalan XSL processor Result is either an XML, HTML or text document
" src="https://present5.com/presentation/102009364_72145941/image-8.jpg" alt="Letting a Browser Perform the Transformation xml version="1. 0" encoding="ISO-8859 -1"? > " />
Letting a Browser Perform the Transformation xml version="1. 0" encoding="ISO-8859 -1"? > xml-stylesheet type="text/xsl" href=“catalog. xsl"? >
The Root of the XSL Document (program) • The Root of the XSL document should be one of the following lines:
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 (/).
. . . Template content." src="https://present5.com/presentation/102009364_72145941/image-11.jpg" alt="Templates • A template has the form:
Focus on Generating HTML XSL (presentation) XML (content) XSL Processor HTML
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). XSL XML Transformation Engine (XSL Parser) HTML
Recall XML • Recall that an XML document is composed of ‘elements’. • For a Book. Catalogue XML document we have the following elements: Book. Catalogue (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. , Book. Catalogue) do [action 1]. • Let the XSL processor know when it encounters the Book element do [action 2]. • Let the XSL processor know when it encounters the Title element do [action 3]. • 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 child element action on the document
XSL Document Structure xml version=“ 1. 0”? >
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:
Template Rules (Example)
XSL Processor • A free, open source, Java XSL processor Apache - Xalan - http: //xml. apache. org/xalan-j/ XSL XML XSL Processor HTML Invoking from the command line: java org. apache. xalan. xslt. Process -IN my. Xml. File. xml -XSL my. Xsl. File. xsl -OUT my. Output. File. html Spring 2010 mark. baker@computer. org
Example - Create XSL for Book. Catalogue. xml xml version="1. 0"? >
First Example • This example will show 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 Book. Catalogue. xml we have (snippet):
Creating a Stylesheet - Step 1 • Draw a tree diagram of your XML document. Document / Document. Type Element Book. Catalogue Element Book. . . Element Book Element Title Text My Life. . . Element Author Element Date Text Paul Mc. Cartney Text July, 1998 Element ISBN Text 9430312021 -43892 Element Book. . . Element Publisher Text Mc. Millin 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
“The XSL processor returns the value of the thing" src="https://present5.com/presentation/102009364_72145941/image-31.jpg" alt="Explanation
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
Creating a Stylesheet - Step 5 • Tell the XSL Processor that when it encounters a
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:
Multiple Applicable Rules • Suppose that the XSL Processor is processing Book. Catalogue and it gets to the
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 Book. Catalogue. html.