Document not found! Please try again

Transforming XML Documents ith XSLT with XSLT - brics

15 downloads 91 Views 591KB Size Report
How XML documents may be rendered in browsers. ▫ How the XSLT language transforms XML d t documents. ▫ How XPath is used in XSLT. 2. An Introduction to  ...
An Introduction to XML and Web Technologies

Transforming XML Documents with ith XSLT

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

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

An Introduction to XML and Web Technologies

2

Presenting a Business Card http://businesscard.org > John Doe CEO, Widget Inc. [email protected] (202) 555-1414 i " id t if"/

An Introduction to XML and Web Technologies

3

Using CSS card name title email phone

{ { { { {

background-color: #cccccc; border: none; width: 300;} display: block; font-size: 20pt; margin-left: 0; } display: block; margin-left: 20pt;} p y block; font-family: y monospace; p margin-left: g 20pt;} p display: 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 John Doe CEO, Widget Inc. [email protected] (202) h (202) 555 555-1414 1414 / h

An Introduction to XML and Web Technologies

5

XSLT for Business Cards (1/2) l "htt // 3 /1999/ ht l"




An Introduction to XML and Web Technologies

6

XSLT for Business Cards (2/2) Phone:

d / l if
An Introduction to XML and Web Technologies

7

XSL--FO XSL ƒ 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

XSL--FO for Business Cards XSL f l name= xsl region body > l "b "/

An Introduction to XML and Web Technologies

/fo:block

9

Overview

ƒ ƒ ƒ ƒ

IIntroduction d i 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 tree or an atomic value) ƒ A context position and size ƒ A set of variable bindings (mapping variable names to values)) ƒ A function library (including those from XPath) ƒ A sett off namespace declarations d l ti

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 th roott element the l t off the th stylesheet t l h t

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

ƒ ƒ ƒ ƒ

IIntroduction d i 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 b H ll World W ld /b An Introduction to XML and Web Technologies

20

Explicit Constructors / > title > Hello World b > Hello World An Introduction to XML and Web Technologies

21

Computed Attributes Values (1/2) / > title > Hello World b > Hello World An Introduction to XML and Web Technologies

22

Computed Attribute Values (2/2) Hello World Hello b H ll World W 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 100026 > number(1)"> number(@amount)>number(1) > s of sl te t / sl te t / l h 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) rcp:preparation >


        An Introduction to XML and Web Technologies

        46

        XSLT for Recipes (6/6) y g g select="@calories"/> / / p / / @calories /> / s te p ate

        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://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 An Introduction to XML and Web Technologies

        "fib" / p

        55

        Grouping / l f h

        An Introduction to XML and Web Technologies

        56

        The Output 1 /> "1"/ p g sauce" count="1"/> / >

        An Introduction to XML and Web Technologies

        59

        Multiple Input Documents Selected Recipes rcp:title/text() /> l " "/ 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) name />
        result >
      1. :
      2. An Introduction to XML and Web Technologies

        62

        The First Output Students
        Joe Average
        Jack h f " d h l#100078" k Doe /
        y

        An Introduction to XML and Web Technologies

        63

        The Second Output Grades Joe 100026 />Joe Average
        • Math 101: C-
        • Biology li i l 101: 101 C+
        • /li
        • 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 g g kort navn titel title titel /title email telefon logo

        An Introduction to XML and Web Technologies

        g g carte nom titre title titre /title courriel telephone logo

        67

        Generating Stylesheets (1/2) match "b:card"> " "/

        An Introduction to XML and Web Technologies

        68

        Generating Stylesheets (2/2) name "{phone}"> /m sl element An Introduction to XML and Web Technologies

        69

        Generated Stylesheet (1/2) p match="b:name"> y

        An Introduction to XML and Web Technologies

        74

        XSLT 2.0 Solution (2/2)


      3. An Introduction to XML and Web Technologies

        75

        XSLT 1.0 Solution (1/3) "integerlist" p

        An Introduction to XML and Web Technologies

        76

        XSLT 1.0 Solution (2/3) Integers p

        An Introduction to XML and Web Technologies

        77

        XSLT 1.0 Solution (3/3)
      4. select "text()"/
      5. 1] >


      6. An Introduction to XML and Web Technologies

        78

        CaloriesFatCarbohydratesProtein Alcohol
        rcp:recipe > 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
        / l t l t