29
1 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) David Meredith [email protected] ww.titanmusic.com/teaching/cis336-2006-7.htm

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

  • Upload
    elaine

  • View
    23

  • Download
    2

Embed Size (px)

DESCRIPTION

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 [email protected] www.titanmusic.com/teaching/cis336-2006-7.html. - PowerPoint PPT Presentation

Citation preview

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

1

CIS336Website design, implementation and

management(also Semester 2 of CIS219, CIS221 and

IT226)

Lecture 6XSLT

(Based on Møller and Schwartzbach, 2006, Chapter 5)

David [email protected]

www.titanmusic.com/teaching/cis336-2006-7.html

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

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

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

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

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

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

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

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)

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

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)

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

7

Output of XSLT transformation on business card

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

8

XSLT Stylesheets

• XSLT stylesheet is a sequence of template rules (see above)

• XSLT language defined in an XML Schemahttp://www.w3.org/2005/02/schema-for-xslt20.xsd

• XSLT processor first finds template rule that matches root node and executes corresponding template body

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

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()"/>

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

19

Constructing processing instructions and comments in the target document

Output document ->

<- XSLT document

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

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?

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

21

Output of previous transformation

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

22

for-each instruction

• Has same effect as stylesheet on slide 20

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

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?

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

24

Output of transformation on previous slide

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

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?

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

26

Output of transformation on previous slide

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

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

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

28

• 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?

Use of mode attribute

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

29

Result of previous transformation