XSLT: Transforming XML Documents with XSLT Templates, Slides of Introduction to Database Management Systems

Xslt (extensible stylesheet language transformations) is a language for transforming xml documents into other formats such as xml, html, or plain text. An introduction to xslt, its components, and how to write xslt templates to transform xml documents. It covers topics such as xslt processors, xslt programs, input and output xml, xslt elements, and xslt examples.

Typology: Slides

2011/2012

Uploaded on 01/29/2012

arold
arold 🇺🇸

4.7

(24)

372 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
XSLT
CPS 116
Introduction to Database Systems
2
Announcements
Homework #3 assigned today (October 21)
Due on November 2
Beware of QuiP quirks
A fresher alternative: Saxon-B 8.1.1 (not supported)
Both QuiP and Saxon-B 8.1.1 can be installed on your
own machine
Graded Homework #2 available
Check your score on Blackboard
3
XSLT
W3C recommendation
XML-to-XML rule-based transformation language
An XSLT program is an XML document itself
Used most frequently as a stylesheet language
XSLT processor
XSLT program
Input XML Output XML
Actually, output does not need to be in XML in general
4
XSLT program
An XSLT program is an XML document containing
Elements in the <xsl:> namespace
Elements in user namespace
The result of evaluating an XSLT program on an
input XML document =
the XSLT document where each <xsl:> element
has been replaced with the result of its evaluation
Uses XPath as a sub-language
5
XSLT elements
Element describing transformation rules
<xsl:template>
Elements describing rule execution control
<xsl:apply-templates>
<xsl:call-template>
Elements describing instructions
<xsl:if>, <xsl:for-each>, <xsl:sort>, etc.
Elements generating output
<xsl:value-of>, <xsl:attribute>, <xsl:copy-
of>, <xsl:text>, etc.
6
XSLT example
Find titles of books authored by “Abiteboul”
<?xml version=“1.0”?>
<xsl:stylesheet
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0”>
<xsl:template match=“book[author=‘Abiteboul’]”>
<booktitle>
<xsl:value-of select=“title”/>
</booktitle>
</xsl:template>
</xsl:stylesheet>
Not quite; we will see why later
Standard header of an XSLT document
pf3
pf4

Partial preview of the text

Download XSLT: Transforming XML Documents with XSLT Templates and more Slides Introduction to Database Management Systems in PDF only on Docsity!

XSLT

CPS 116

Introduction to Database Systems

Announcements

™ Homework #3 assigned today (October 21)

ƒ Due on November 2

ƒ Beware of QuiP quirks

ƒ A fresher alternative: Saxon-B 8.1.1 (not supported)

ƒ Both QuiP and Saxon-B 8.1.1 can be installed on your

own machine

™ Graded Homework #2 available

ƒ Check your score on Blackboard

3

XSLT

™ W3C recommendation

™ XML-to-XML rule-based transformation language

™ An XSLT program is an XML document itself

™ Used most frequently as a stylesheet language

XSLT processor

XSLT program

Input XML Output XML Actually, output does not need to be in XML in general

4

XSLT program

™ An XSLT program is an XML document containing

ƒ Elements in the namespace

ƒ Elements in user namespace

™ The result of evaluating an XSLT program on an

input XML document =

the XSLT document where each element

has been replaced with the result of its evaluation

™ Uses XPath as a sub-language

5

XSLT elements

™ Element describing transformation rules

ƒ

™ Elements describing rule execution control

ƒ

ƒ

™ Elements describing instructions

ƒ , , , etc.

™ Elements generating output

ƒ , , , , etc.

6

XSLT example

™ Find titles of books authored by “Abiteboul”

) Not quite; we will see why later

Standard header of an XSLT document

™ is the basic XSLT construct describing a transformation rule ƒ match_expr is an XPath-like expression specifying which nodes this rule applies to

™ evaluates xpath_expr within the context of the node matching the template, and converts the result node-set to a string

™ and simply get copied to the output for each node match

Template in action

™ Example XML fragment

Foundations of Databases Abiteboul Hull Vianu Addison Wesley 1995 ……

A First Course in Databases Ullman Widom Prentice-Hall 2002 ……

Template applies

Foundations of Databases

Template does not apply; default behavior is to process the node recursively and print out all text nodes A First Course in Databases Ullman Widom Prentice-Hall 2002 … …

9

Removing the extra output

™ Add the following template:

™ This template matches all text and attributes

™ XPath features

ƒ text() is a node test that matches any text node

ƒ @* matches any attribute

ƒ | means “or” in XPath

™ Body of the rule is empty, so all text and attributes

become empty string

ƒ This rule effectively filters out things not matched by the

other rule

10

™ Again, find titles of books authored by “Abiteboul”; but

make the output look like

… …

… …

™ A more general method

… …

… …

body adds an attributed named attr with value body to the parent element in the output

11

™ Another slightly different example: return (entire) books

authored by “Abiteboul”

™ copies the entire

contents (including tag structures) of the node-set returned

by xpath_expr to the output

12

Formatting XML into HTML

™ Example templates to

ƒ Render a book title in italics in HTML ƒ Render the authors as a comma-separated list

1]”> ,

™ allows precise control of white space in output

Calling templates & passing parameters

™ Use the generic template

:


™ evaluates xpath_expr and passes its

result as the value of the parameter para_name

™ invokes the named template

without changing the context

XSLT summary

™ Used often as a stylesheet language, but can be

considered a query language too

ƒ Very expressive, with full recursion

  • Cannot be replaced by XQuery

ƒ Easily non-terminating, difficult to optimize

  • Cannot replace XQuery

™ So many features, so little time! ☺

21

Review

™ XML: tree (or graph)-structured data

™ DTD: simple schema for XML

ƒ Well-formed XML: syntactically correct ƒ Valid XML: well-formed and conforms to a DTD

™ XPath: path expression language for XML

ƒ An XPath expression selects a list of nodes in an XML document ƒ Used in other languages

™ XQuery: SQL-like query language for XML

ƒ FLWOR expression, quantified expression, aggregation, etc.

™ XSLT: stylesheet language for XML, in XML

ƒ Transforms input XML by applying template rules recursively on the structure of input XML