University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford
University of Nottingham 2 XSL / XSLT / FO l eXtensible Stylesheet Language l A language (an XML application) to define the appearance and behaviour of an XML document. l Transformation l The logical restructuring of a “source document” to produce a “result document”. l For example XML HTML l Formatting l The precise description of screen/page layout. l XSLT vs FO
University of Nottingham 3 The XSLT Language l XML syntax (using the xsl: namespace) l XSL language consists of directives (ie elements in this namespace) l Rule-based l stylesheets consist of a series of templates that contain rules for the processing of a particular element. l XSL stylesheets are not a sequential programming language - rules are applied depending upon the logical structure of the document. l Rules may be conditional l XSL may contain variables (numeric or string), it may perform arithmetic calculations. l There is a library of parameterised functions
University of Nottingham 4 The Tree Model of XML export vt Send out (goods) to another country Latin ex out portare to carry
University of Nottingham 5 The Tree Model of XML
University of Nottingham 6 The Tree Model of XML
University of Nottingham 7 XML Node Types l Root Node l The top level node (1 per document) l Element Node l An element bound by a start and finish tag (or a single empty-element tag) l Text Node l A sequence of consecutive characters (PCDATA) l Attribute Node l The name and value of an attribute inside an element l Comment Node l Processing Instruction Node l Namespace Node
University of Nottingham 8 XML Node Relationships l Self l Parent l Ancestor l Child l Descendant l Following l Following-Sibling l Preceding l Preceding-Sibling
University of Nottingham 9 Self 1
University of Nottingham 10 Parent 1
University of Nottingham 11 Ancestor 2 1
University of Nottingham 12 Child 12
University of Nottingham 13 Descendant
University of Nottingham 14 Following
University of Nottingham 15 Following-Sibling 12
University of Nottingham 16 Preceding 3 21
University of Nottingham 17 Preceding-Sibling 21
University of Nottingham 18 XSL Templates... l Templates contain transformation rules l either XSL directives or valid XML output. l Templates are matched to an XML node. see greeting-1.xsl
University of Nottingham 19 XPATH l XPATH is a sub-language within XSLT - used to identify components of the document. l XPATH expressions can be used to match a template, or the contents of an element. l Example:
University of Nottingham 20 Example XPATH Expressions l./MESSAGE../MESSAGE */MESSAGE MESSAGE/TITLE|MESSAGE/BODY ancestor(CHAPTER)/HEAD
University of Nottingham 21 XSL apply-templates directive l Specifies that immediate children of a node should be processed further. It is possible to specify which children with an optional “select=xpath” see greeting-2.xsl