21
University of Nottingham ool of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford

Introduction to XML 2. XSLT

Embed Size (px)

DESCRIPTION

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”. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to XML 2. XSLT

University of Nottingham

School of Computer Science & Information Technology

Introduction to XML2. XSLT

Tim Brailsford

Page 2: Introduction to XML 2. XSLT

2

University of Nottingham

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

Page 3: Introduction to XML 2. XSLT

3

University of Nottingham

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

Page 4: Introduction to XML 2. XSLT

4

University of Nottingham

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>

Page 5: Introduction to XML 2. XSLT

5

University of Nottingham

The Tree Model of XML

Page 6: Introduction to XML 2. XSLT

6

University of Nottingham

The Tree Model of XML

<definition>

<word> <meaning><etymology> <part-of-speech>

<language> <parts>

<prefix> <word> <meaning>

Page 7: Introduction to XML 2. XSLT

7

University of Nottingham

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

Page 8: Introduction to XML 2. XSLT

8

University of Nottingham

XML Node Relationships Self Parent Ancestor Child Descendant Following Following-Sibling Preceding Preceding-Sibling

Page 9: Introduction to XML 2. XSLT

9

University of Nottingham

Self

1

Page 10: Introduction to XML 2. XSLT

10

University of Nottingham

Parent

1

Page 11: Introduction to XML 2. XSLT

11

University of Nottingham

Ancestor

2

1

Page 12: Introduction to XML 2. XSLT

12

University of Nottingham

Child

1 2

Page 13: Introduction to XML 2. XSLT

13

University of Nottingham

Descendant

1 2

3 4 5

Page 14: Introduction to XML 2. XSLT

14

University of Nottingham

Following

1 4

2 3 5 6

Page 15: Introduction to XML 2. XSLT

15

University of Nottingham

Following-Sibling

1 2

Page 16: Introduction to XML 2. XSLT

16

University of Nottingham

Preceding

3

2 1

Page 17: Introduction to XML 2. XSLT

17

University of Nottingham

Preceding-Sibling

2 1

Page 18: Introduction to XML 2. XSLT

18

University of Nottingham

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

Page 19: Introduction to XML 2. XSLT

19

University of Nottingham

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”>

Page 20: Introduction to XML 2. XSLT

20

University of Nottingham

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’]

Page 21: Introduction to XML 2. XSLT

21

University of Nottingham

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