1 / 29

CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226)

Lecture 6 XSLT (Based on Møller and Schwartzbach, 2006, Chapter 5). CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226). David Meredith d.meredith@gold.ac.uk www.titanmusic.com/teaching/cis336-2006-7.html.

elaine
Download Presentation

CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 XSLT (Based on Møller and Schwartzbach, 2006, Chapter 5) CIS336Website design, implementation and management(also Semester 2 of CIS219, CIS221 and IT226) David Meredith d.meredith@gold.ac.uk www.titanmusic.com/teaching/cis336-2006-7.html

  2. XML does not contain formatting or layout information • XML provides a general format for representing the logical structure of data • it does not specify how the data should be displayed or presented, for example, in a browser • When the XML file above left is loaded into a browser, it is displayed as at above right • that is, the browser shows the tree structure of the XML file • We need to be able to have precise control over exactly how the data in an XML file is presented or displayed

  3. Using CSS with XML • Example above shows how to use cascading style sheets with an XML file • Various problems with doing this: • Cannot print element names so cannot, e.g., label phone number as a phone number • Data always presented in the order it appears in the XML file (e.g., e-mail address has to come before phone number) • Cannot use data stored in attributes • Cannot introduce extra structure like tables

  4. Extensible Stylesheet Language (XSL) • XSL consists of • XSL Transformations (XSLT) • a declarative programming language for specifying transformations between XML languages • XSL Formatting Objects (XSL-FO) • a particular target language (like, for example, XHTML) suitable for specifying layout • Behaviour of a stylesheet can be obtained by specifying in XSLT a transformation from an XML language into XHTML (or XSL-FO) • XSLT can also be used to • translate between other pairs of XML languages • extract views of XML data • perform query-like computations

  5. XSLT • We'll cover XSLT 2.0 • most current implementations are of XSLT 1.0 • based on XPath 2.0 • XSLT transformations can be executed in standalone tools (e.g., Saxon) • but also supported by modern browsers like Firefox • include processing instruction in XML file to be rendered that links it to an XSLT document • browser then loads XSLT file, executes the transformation and presents the result (usually an XHTML document)

  6. Line 2 at top left links XML file with the XSLT file at bottom left • In XSLT file • stylesheet element consists of set of template rules, each within a template element • each template rule has a pattern given in the match attribute • XSLT template rule computes part of the target document • XSLT document at left uses 3 namespaces • xsl is XSLT namespace • b is source namespace (business card language namespace • default is target namespace(XHTML)

  7. Output of XSLT transformation on business card

  8. XSLT Stylesheets • XSLT stylesheet is a sequence of template rules (see above) • XSLT language defined in an XML Schema http://www.w3.org/2005/02/schema-for-xslt20.xsd • XSLT processor first finds template rule that matches root node and executes corresponding template body

  9. Use of XPath 2.0 in XSLT 2.0 • XSLT 2.0 uses XPath 2.0 for • specifying patterns for template rules • e.g., <xsl:template match="b:card"> • selecting nodes for processing • e.g., <xsl:apply-templates select="b:name"/> • computing boolean conditions • e.g., <xsl:if test="b:logo"> • generating text content for the output document • e.g., <xsl:value-of select="b:name/text()"/>

  10. Template rules • Template rule matches a part of the input document and constructs a part of the output document • match attribute contains a pattern • Content of template element is a sequence constructor • A collection of template rules in an XSLT stylesheet is evaluated in a context as follows: • the template rules whose patterns match the context are identified • most specific template rule is selected • sequence constructor of selected template rule is evaluated in the context

  11. Patterns and matching • XSLT pattern is an XPath 2.0 expression • but not all of XPath 2.0 is allowed within a pattern • pattern can only use abbreviated syntax (e.g., //, @, omission of child::) • it must be either a single expression or a union of expressions, e.g., match="b:name|b:title|b:email|b:phone" match="b:card" • each path step separated by / or // • each step can only use the child or attribute axes (and only in abbreviated syntax) • node tests and predicates can use general syntax • Pattern matches a node, n, if it can be evaluated in a context in which any node in the source document is the context node and the resulting sequence contains n • implies that patterns should be read backwards • e.g., rcp:recipe/rcp:ingredient//rcp:preparation • matches any preparation element that is a descendant of any ingredient element that is a child of a recipe element

  12. Patterns and matching: Further examples • students matches • node 1 • student matches • nodes 2 and 10 • students/student matches • nodes 2 and 10 • @grade matches • all grade attribute nodes • student//@grade[. eq "B+"] matches • grade attribute node in node 18 • student[@id="100078"]/age matches • node 12 • result[ancestor::student/@id="100026"][@grade="D"] matches • node 9

  13. Sequence Constructors • Simplest sequence constructor is literal sequence of character data nodes and element nodes that do not belong to the XSLT namespace • Stylesheet above always generates the same output document regardless of input document

  14. Element and attribute constructors • XSLT stylesheet at left generates same as on previous slide using explicit constructors • Uses XSLT element and attribute constructors • Value of select attribute of attribute constructor (line 14) is an XPath string literal • Explicit constructors and literals can be mixed

  15. Computing attribute values • Attribute values can be computed from source document • see line 14 which finds all attributes called bgcolor in source document (of which there will be one)

  16. Computing attribute values in literal sequence constructors • {...} in a literal sequence constructor is replaced by atomized value of XPath expression that it is assumed to contain

  17. attribute-set element • If collection of attributes often used together, can declare them as an attribute set using the attribute-set XSLT element • Use attribute set by using the use-attribute-sets qattribute • can be used in literals and constructors

  18. text and value-of instructions • text instruction can be wrapped around literal character data to preserve whitespace • value-of instruction generates character data from an XPath expression in its select attribute • Example:<xsl:text>2+2 = </xsl:text><xsl:value-of select="2+2"/>generates2+2 = 4whereas2+2 = <xsl:value-of select="2+2"/>generates2+2 =4 • If value of select expression has length greater than 1, then textual versions of the items are concatenated, separated by a single space

  19. Constructing processing instructions and comments in the target document <- XSLT document Output document ->

  20. apply-templates instruction • apply-templates recursively applies entire stylesheet to nodes specified by the select attribute • default value of select attribute is child::node() • See business card for another example • What do the above stylesheet and source document generate as output?

  21. Output of previous transformation

  22. for-each instruction • Has same effect as stylesheet on slide 20

  23. if instruction • if instruction returns result of sequence constructor in its body if XPath expression in test attribute is true • What is result of running stylesheet above left on XML file at right?

  24. Output of transformation on previous slide

  25. choose instruction • choose allows various different branches to be tried in order • What is the output of the above stylesheet when the source document is as at right?

  26. Output of transformation on previous slide

  27. name and mode attributes • template node may have other attributes besides match: name • provides a name for the template rule which allows it to be called like a function • if name attribute is present, match attribute might not be mode • only allowed if match attribute is present • restricts template rules that can match a given node • a mode is either a QName or #default • value of mode attribute is either • a space-separated sequence of modes • #all indicating all possible modes

  28. Use of mode attribute • apply-templates instruction may use a mode attribute to only consider those template rules that have a corresponding mode attribute • What is the output when the above stylesheet is applied to the XML file on the right?

  29. Result of previous transformation

More Related