<<

An Introduction to XML and Web Technologies Transforming XML Documents with XSLT

Anders Møller & Michael I. Schwartzbach © 2006 Addison-Wesley Objectives

ƒ How XML documents may be rendered in browsers ƒ How the XSLT language transforms XML dtdocuments ƒ How XPath is used in XSLT

An Introduction to XML and Web Technologies 2 Presenting a Business Card

John Doe CEO, Widget Inc. [email protected] (202) 555-1414

An Introduction to XML and Web Technologies 3 Using CSS

card { background-color: #cccccc; border: none; width: 300;} name { display: block; font-size: 20pt; margin-left: 0; } title { display: block; margin-left: 20pt;} email { disppylay: block; font-familyyp: monospace; marg in-left: 20pt;} phone { display: block; margin-left: 20pt;}

ƒ the information cannot be rearranged ƒ information encoded in attributes cannot be exploited ƒ additional structure cannot be introduced

An Introduction to XML and Web Technologies 4 Using XSLT

xsl "?> John Doe CEO, Widget Inc. [email protected] (202) 555-1414/h1414

An Introduction to XML and Web Technologies 5 XSLT for Business Cards (1/2)

<xsl:value-of select="b:name/text()"/>



An Introduction to XML and Web Technologies 6 XSLT for Business Cards (2/2)

Phone:

An Introduction to XML and Web Technologies 7 XSLXSL--FOFO

ƒ XSLT was originally design to target XSL-FO ƒ XSL-FO (Formatting Objects) in an XML language for describing physical layout of texts ƒ Widely used in the graphics industry ƒ Not supported by any browsers yet

An Introduction to XML and Web Technologies 8 XSLXSL--FOFO for Business Cards

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://businesscard.org" xmlns:fo="http://www.w3.org/1999/XSL/Format"> content-width="2.5cm"/> page-height="5.5cm" page-width="8.6cm" margin-top="0.4cm" margin-bottom="0.4cm" margin-left="04cm0.4cm" margin-right="0.4cm">

An Introduction to XML and Web Technologies 9 Overview

ƒ IdiIntroduction ƒ Templates and pattern matching ƒ Sequence constructors ƒ Using XSLT

An Introduction to XML and Web Technologies 10 XSLT Stylesheets

...

ƒ XSLT is a domain-specific language for writing XML transformations (compare with e.g. JDOM)

ƒ An XSLT stylesheet contains template rules ƒ Processing starts at the root node of the input document

An Introduction to XML and Web Technologies 11 Template Rules

...

ƒ Find the template rules that match the context node ƒ Select the most specific one ƒ Evaluate the body (a sequence constructor)

An Introduction to XML and Web Technologies 12 Use of XPath in XSLT

ƒ Specifying patterns for template rules ƒ Selecting nodes for processing ƒ Computing boolean conditions ƒ Generating text contents for the output document

An Introduction to XML and Web Technologies 13 Evaluation Context

ƒ A context item (a node in the source or an atomic value) ƒ A context position and size ƒ A set of variable bindings (mapping variable names to values) ƒ A function (including those from XPath) ƒ AtfA set of namespace dldeclara tions

An Introduction to XML and Web Technologies 14 The Initial Context

ƒ The context item is the document root ƒ The context position and size both have value 1 ƒ The set of variable bindings contains only global parameters ƒ The function library is the default one ƒ The namespace declarations are those defined in the root el ement of th e st yl esh eet

An Introduction to XML and Web Technologies 15 Patterns and Matching

ƒ A pattern is a restricted XPath expression • it is a union of path expressions • each path expression contains a number of steps separated by / or // • each step may only use the child or attribute axis ƒ A pattern matches a node if • starting from some node in the tree: • the given node is contained in the resulting sequence

rcp:recipe/rcp:ingredient//rcp:preparation

An Introduction to XML and Web Technologies 16 Names, Modes, Priorities

ƒ Templates may have other attributes

ƒ name: used to call templates like function ƒ mode: used to restrict the candidate templates ƒ priority: used to determine specificity

An Introduction to XML and Web Technologies 17 Overview

ƒ IdiIntroduction ƒ Templates and pattern matching ƒ Sequence constructors ƒ Using XSLT

An Introduction to XML and Web Technologies 18 Sequence Constructors

ƒ Element and attribute constructors ƒ Text constructors ƒ Copying nodes ƒ Recursive application ƒ Repetitions ƒ Conditionals ƒ Template invocation ƒ …

An Introduction to XML and Web Technologies 19 Literal Constructors

Hello World Hello World ld/b

An Introduction to XML and Web Technologies 20 Explicit Constructors

Hello World Hello World

An Introduction to XML and Web Technologies 21 Computed Attributes Values (1/2)

Hello World Hello World

An Introduction to XML and Web Technologies 22 Computed Attribute Values (2/2)

Hello World Hello World ld/b

An Introduction to XML and Web Technologies 23 Text Constructors

ƒ Literal text becomes character data in the output here is some chardata

ƒ Whitespace control requires a constructor:

ƒ The (atomized) value of an XPath expression:

An Introduction to XML and Web Technologies 24 Recursive Application

ƒ The apply-templates element • finds some nodes using the select attribute • applies the entire stylesheet to those nodes • concatenates the resulting sequences ƒ The default select value is child::node()

ƒ Processing is often (but not necessarily!) a simple recursive traversal down the input XML tree

An Introduction to XML and Web Technologies 25 Student Data

Joe Average 21 Biology

C- C+ D Jack Doe A 18 Physics A- XML Science B+ A An Introduction to XML and Web Technologies 26 Generating Students Summaries

An Introduction to XML and Web Technologies 27 Using Modes, Desired Output

Joe Average Jack Doe C- C+ D A A- B+ A

An Introduction to XML and Web Technologies 28 Using Modes (1/2)

An Introduction to XML and Web Technologies 29 Using Modes (2/2)

An Introduction to XML and Web Technologies 30 Repetitions

An Introduction to XML and Web Technologies 31 Conditionals (if)

An Introduction to XML and Web Technologies 32 Conditionals (choose)

xmlns:b="http://businesscard.org" No information available

An Introduction to XML and Web Technologies 33 Template Invocation (1/2)

An Introduction to XML and Web Technologies 34 Template Invocation (2/2)

An Introduction to XML and Web Technologies 35 BuiltBuilt--InIn Template Rules

ƒ What happens if no template matches a node? ƒ XSLT applies a default template rule • text is copied to the output • nodes appl y the st ylesheet rec ursi v ely to the children

ƒ A widely used default rule: for the document root node

An Introduction to XML and Web Technologies 36 Sorting

An Introduction to XML and Web Technologies 37 Copying Nodes

ƒ The copy-of element creates deep copies ƒ The copy element creates shallow copies

ƒ Give top-most HTML lists square bullets:

An Introduction to XML and Web Technologies 38 An Identity Transformation

An Introduction to XML and Web Technologies 39 Overview

ƒ IdiIntroduction ƒ Templates and pattern matching ƒ Sequence constructors ƒ Using XSLT

An Introduction to XML and Web Technologies 40 XSLT 1.0 Restrictions

ƒ Most browsers only support XSLT 1.0 ƒ Can only use XPath 1. 0 ƒ No sequence values, only result tree fragments ƒ …

An Introduction to XML and Web Technologies 41 XSLT for Recipes (1/6)

<xsl:value-of select="rcp:description"/> css"/>

An Introduction to XML and Web Technologies 42 XSLT for Recipes (2/6)

An Introduction to XML and Web Technologies 43 XSLT for Recipes (3/6)

  • s of
  • An Introduction to XML and Web Technologies 44 XSLT for Recipes (4/6)

    An Introduction to XML and Web Technologies 45 XSLT for Recipes (5/6)

    An Introduction to XML and Web Technologies 46 XSLT for Recipes (6/6)

    CaloriesFatCarbohydratesProtein Alcohol
    An Introduction to XML and Web Technologies 47 The Output

    An Introduction to XML and Web Technologies 48 A Different View

    An Introduction to XML and Web Technologies 49 The Output

    An Introduction to XML and Web Technologies 50 A Further Stylesheet

    Nutrition Table

    Dish Calories Fat Carbohydrates Protein

    An Introduction to XML and Web Technologies 51 The Final Output

    An Introduction to XML and Web Technologies 52 Other Language Features

    ƒ Variables and parameters ƒ Numbering ƒ Functions ƒ Sequence types ƒ Multiple input/output documents ƒ Dividing a stylesheet into several files ƒ Stylesheets that generate stylesheets as output

    – see the book!

    An Introduction to XML and Web Technologies 53 Essential Online Resources

    ƒ http://www.w3.org/TR/xslt20/ ƒ http://saxon.sourceforge .net/ ƒ http://www.w3.org/TR/xsl/ ƒ http://xml.apache.org/fop/

    An Introduction to XML and Web Technologies 54 Variables and Parameters

    select="10"/>

    An Introduction to XML and Web Technologies 55 Grouping

    An Introduction to XML and Web Technologies 56 The Output

    ...

    An Introduction to XML and Web Technologies 57 Numbering

    An Introduction to XML and Web Technologies 58 Functions

    An Introduction to XML and Web Technologies 59 Multiple Input Documents

    Selected Recipes

    An Introduction to XML and Web Technologies 60 Multiple Output Documents (1/2)

    Students Grades An Introduction to XML and Web Technologies 61 Multiple Output Documents (2/2)


  • :
  • An Introduction to XML and Web Technologies 62 The First Output

    Students Joe Average
    Jack Doe

    An Introduction to XML and Web Technologies 63 The Second Output

    Grades Joe Average

    • Math 101: C-
    • Bi ol ogy 101 : C+
    • Statistics 101: D
    Jack Doe
    • Math 101: A
    • XML 101: A-
    • Physics 101: B+
    • XML 102: A
    An Introduction to XML and Web Technologies 64 Including a Stylesheet

    I don't like

    I'm crazy for

    Zuppa Inglese

    I don't like Zuppa Inglese

    An Introduction to XML and Web Technologies 65 Importing a Stylesheet

    I don't like

    I'm crazy for

    Zuppa Inglese

    I'm crazy for Zuppa Inglese

    An Introduction to XML and Web Technologies 66 Multilingual Business Cards

    kort carte navn nom titel titre email courriel telefon telephone logo logo

    An Introduction to XML and Web Technologies 67 Generating Stylesheets (1/2)

    An Introduction to XML and Web Technologies 68 Generating Stylesheets (2/2)

    An Introduction to XML and Web Technologies 69 Generated Stylesheet (1/2)

    An Introduction to XML and Web Technologies 70 Generated Stylesheet (2/2)

    An Introduction to XML and Web Technologies 71 Business Card Translation

    John Doe CEO, Widget Inc. [email protected] (202) 555-1414

    John Doe CEO, Widget Inc. [email protected] ( 202) 555-1414

    An Introduction to XML and Web Technologies 72 Red, Blue, and Sorted

    ƒ Transform this list of number to be: •sorted • alternatingly red and blue

    15 12 17 25 18 17 23

    An Introduction to XML and Web Technologies 73 XSLT 2.0 Solution (1/2)

    Integers

    An Introduction to XML and Web Technologies 74 XSLT 2.0 Solution (2/2)

  • An Introduction to XML and Web Technologies 75 XSLT 1.0 Solution (1/3)

    An Introduction to XML and Web Technologies 76 XSLT 1.0 Solution (2/3)

    Integers

    An Introduction to XML and Web Technologies 77 XSLT 1.0 Solution (3/3)

  • An Introduction to XML and Web Technologies 78