210 likes | 364 Views
Introduction to XML 2. XSLT. Tim Brailsford. XSL / XSLT / FO. eXtensible Stylesheet Language A language (an XML application) to define the appearance and behaviour of an XML document. Transformation The logical restructuring of a “source document” to produce a “result document”.
E N D
Introduction to XML2. XSLT Tim Brailsford
XSL / XSLT / FO • eXtensible Stylesheet Language • A language (an XML application) to define the appearance and behaviour of an XML document. • Transformation • The logical restructuring of a “source document” to produce a “result document”. • For example XML HTML • Formatting • The precise description of screen/page layout. • XSLT vs FO
The XSLT Language • XML syntax (using the xsl: namespace) • XSL language consists of directives (ie elements in this namespace) • Rule-based • stylesheets consist of a series of templates that contain rules for the processing of a particular element. • XSL stylesheets are not a sequential programming language - rules are applied depending upon the logical structure of the document. • Rules may be conditional • XSL may contain variables (numeric or string), it may perform arithmetic calculations. • There is a library of parameterised functions
The Tree Model of XML <definition> <word>export</word> <part-of-speech>vt</part-of-speech> <meaning> Send out (goods) to another country</meaning> <etymology> <language>Latin</language> <parts> <part> <prefix>ex</prefix> <meaning>out</meaning> </part> <part> <word>portare</word> <meaning>to carry</meaning> </part> </parts> </etymology> </definition>
The Tree Model of XML <definition> <word> <meaning> <part-of-speech> <etymology> <language> <parts> <prefix> <word> <meaning>
XML Node Types • Root Node • The top level node (1 per document) • Element Node • An element bound by a start and finish tag (or a single empty-element tag) • Text Node • A sequence of consecutive characters (PCDATA) • Attribute Node • The name and value of an attribute inside an element • Comment Node • Processing Instruction Node • Namespace Node
XML Node Relationships • Self • Parent • Ancestor • Child • Descendant • Following • Following-Sibling • Preceding • Preceding-Sibling
Self 1
Parent 1
Ancestor 2 1
Child 1 2
Descendant 1 2 3 4 5
Following 1 4 2 3 5 6
Preceding 3 2 1
XSL Templates • <xsl:template match=“GREETING”> . . .</xsl:template> • Templates contain transformation rules • either XSL directives or valid XML output. • Templates are matched to an XML node. see greeting-1.xsl
XPATH • XPATH is a sub-language within XSLT - used to identify components of the document. • XPATH expressions can be used to match a template, or the contents of an element. • Example: <xsl:template match=“GREETING”>
Example XPATH Expressions • <xsl:template match=“GREETING”> • <xsl:template match=“GREETING/MESSAGE/TITLE”> • <xsl:value-of select=“MESSAGE”> • <xsl:value-of select=“@title”> ./MESSAGE ../MESSAGE */MESSAGE MESSAGE/TITLE|MESSAGE/BODY ancestor(CHAPTER)/HEAD BOOK[@title] BOOK[not(@title)] //BOOK[@category=‘fiction’]
XSL apply-templates directive • <xsl:apply-templates /> • Specifies that immediate children of a node should be processed further. • It is possible to specify which children with an optional “select=xpath”<xsl:apply-templates select=“book” /> see greeting-2.xsl