1 Xslt Header Xslt Templates Xslt stylesheets MUST include this - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Xslt Header Xslt Templates Xslt stylesheets MUST include this - - PDF document

Xpath: Summary bib matches a bib element * matches any element XSLT / matches the root element /bib matches a bib element under root bib/paper matches a paper in bib Based on slides by Dan Suciu bib//paper matches a paper in bib, at


slide-1
SLIDE 1

1

1

XSLT

Based on slides by Dan Suciu University of Washington

2

Xpath: Summary

bib matches a bib element * matches any element / matches the root element /bib matches a bib element under root bib/paper matches a paper in bib bib//paper matches a paper in bib, at any depth //paper matches a paper at any depth paper|book matches a paper or a book @price matches a price attribute bib/book/@price matches price attribute in book, in bib bib/book/[@price<“55”]/author/lastname matches…

3

Overview

Querying XML: XPath Transforming XML: XSLT

4

Xslt – Transforming Xml

Amazon.com order form:

<single_book_order> <title>Databases</title> <qty>1</qty> </single_book_order>

Supplier’s order form:

<form7957> <purchase item=’book’ property=’title’ value=’Databases’ quantity=’1’/> </form7957>

5

Xslt - Extensible Style Language for Transformation

Xslt is a language for transforming or

converting one Xml format into another Xml format.

Benefits:

  • No need to parse or interpret many different Xml

formats – they can all be transformed to a single format to facilitate interpretation

  • Language looks like Xml! (remember, Xml defines

languages!)

6

Xslt – A First Look

< single_book_order> < title> Databases< /title> < qty> 1< /qty> < /single_book_order> < form7957> < purchase item= ’book’ property= ’title’ value= ’Databases’ quantity= ’1’/> < /form7957>

< ?xml version= '1.0'?> < xsl:stylesheet xmlns:xsl= 'http://www.w3.org/1999/XSL/Transform' version= '1.0'> < xsl:template match= 'single_book_order'> < form7957> < purchase item= 'book' property= 'title' value= '{ title} ‘ quantity= '{ qty} '/> < /form7957> < /xsl:template> < /xsl:stylesheet>

slide-2
SLIDE 2

2

7

Xslt – Header

Xslt stylesheets MUST include this body:

<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'> … </xsl:stylesheet>

8

Xslt – Templates

Xslt stylesheets are a collection of templates

  • Templates are like functions
  • The body of a template is the output of a

transformation

9

Xslt - Templates

You define a template with the

<xsl:template match=‘’> instruction

You call a template with the

<xsl:apply-templates select=‘’> instruction

  • 1. All elements or attributes that satisfy the select attribute

expression are selected.

  • 2. For each element or attribute that is selected:
  • i. A matching template is found in the stylesheet.
  • ii. The body of the template is executed.

10

Xslt – Template Matching

Xml <basket> <apple color=‘red’/> <apple color=‘green/> <apple color=‘green/> <box> <orange taste=‘good’/> <peach/> <apple color=‘red’/> </box> </basket> Transformed Xml: <new_basket> <apple/> <apple/> <apple/> <box/><apple/> </new_basket> Stylesheet <xsl:template match=‘basket’> <new_basket> <xsl:apply-templates select=‘apple’/> <xsl:apply-templates select=‘box’/> </new_basket> </xsl:template> <xsl:template match=‘apple’> <apple/> </xsl:template> <xsl:template match=‘box’> <box/> <xsl:apply-templates/> <xsl:template> 11

Xslt – choose Instruction

<xsl:choose> instruction is similar to a C++ or

Java switch statement

<xsl:when test=‘’> instruction is similar to the

case statement

<xsl:otherwise> instruction is similar to the

default statement

12

Xslt – choose Example

Original Xml: <customer> <order id=‘5’> <item><title>Database Management Systems</title></item> </order> </customer> Xslt Stylesheet: <xsl:template match=‘customer’> FUNCTION <xsl:choose> SWITCH <xsl:when test='order/@id'> CASE <single_book_order> <title><xsl:value-of select='order/item/title'/></title> </single_book_order> </xsl:when> <xsl:otherwise><single_book_order><fail/> DEFAULT </single_book_order></xsl:otherwise> </xsl:choose> </xsl:template> Output Xml: <single_book_order><title>Database Management Systems</title></single_book_order>

slide-3
SLIDE 3

3

13

Xslt – choose Example 2

Original Xml: <customer> <order> <item><title>Database Management Systems</title></item> </order> </customer> Xslt Stylesheet: <xsl:template match=‘customer’> FUNCTION <xsl:choose> SWITCH <xsl:when test='order/@id'> CASE <single_book_order> <title><xsl:value-of select='order/item/title'/></title> </single_book_order> </xsl:when> <xsl:otherwise><single_book_order><fail/> DEFAULT </single_book_order></xsl:otherwise> </xsl:choose> </xsl:template> Output Xml: <single_book_order><fail/></single_book_order>

14

Xslt – for-each Instruction

<xsl:for-each select=‘item’> instruction is

similar to a foreach iterator or a for loop

The select attribute selects a set of elements

from an Xml document

15

Xslt – if Instruction

<xsl:if test=‘’> instruction is similar to an if statement

in Java or C++

The test attribute is the if condition:

  • True
  • statement is true
  • test returns an element or attribute.
  • False
  • statement is false
  • test returns nothing

There is no ‘else’, so use the <xsl:choose> operator in

this situation.

16

Xslt – for-each and if Example

Original Xml: <basket> <apple color=‘red’ condition=‘yummy’/> <apple color=‘green’ condition=‘wormy/> <apple color=‘red’ condition=‘crisp’/> </basket> Xslt Stylesheet: <xsl:template match=‘basket’> FUNCTION <condition_report> <xsl:for-each select=‘apple’> FOR LOOP <xsl:if test=“contains(@color, ‘red’)”> IF <condition><xsl:value-of select=‘@condition’/></condition> </xsl:if> </xsl:for-each> </condition_report> </xsl:template> Output Xml: <condition_report> <condition>yummy</condition> <condition>crisp</condition> </condition_report>

17

Other miscellaneous features

XML docs can have IDs and IDREFs, URIs

  • reference to another document or document element

Document Object Model (DOM)

  • A tree “object” API for traversing an XML or HTML doc
  • Typically for Java

XML Schema is a proposal to replace/augment DTDs

  • Has a notion of types and typechecking
  • May introduce some notions of IC’s
  • Quite complicated, controversial ... not really adopted yet

XML Namespaces

  • Can import tag names from others
  • Disambiguate by prefixing the namespace name
  • I.e. berkeley-eecs:gpa is different from uphoenix:gpa

Lots of other details, tools, etc.

18

Reminder: Benefits of Relational

Data independence buys you:

  • Evolution of storage -- vs. XML?
  • Evolution of schema (via views) – vs. XML?

Database design theory

  • IC’s, dependency theory, lots of nice tools for ER

Remember, databases are long-lived and reused

  • Today’s “nesting” might need to be inverted tomorrow!

Upshot:

  • XML is good for transient data (e.g. messages)
  • XML is fine for data that will not get reused in a different

way (e.g. Shakespeare, database output like reports)

  • Relational is far cleaner for persistent data
slide-4
SLIDE 4

4

19

XML Databases?

Will XML require a new kind of database? Is there a market for new database vendors? Some predictions that we may or may not believe in:

  • SQL-99 and XQuery will merge
  • Traditional DBMSs will be the serious XML repositories.
  • Doesn’t extend their feature set
  • Oracle and IBM are paranoid and working hard on XML
  • Fighting Oracle and IBM is not a good business plan
  • Also a daunting engineering exercise:
  • Maybe you have a good XPath engine
  • Do you really want to reimplement storage, optimization, 2PL,

ARIES, etc? How do you do row-level locking in XML?

  • There will be lots of user tools to help map to/from XML
  • Not unlike the “import” tools and “report writers” that already

exist

20

Advantages of XML vs. Relational

ASCII makes things easy

  • Easy to parse
  • Easy to ship (e.g. across firewall, via email, etc.)

Self-documenting

  • Metadata (tag names) come with the data

Nested

  • Can bundle lots of related data into one message
  • (Note: object-relational allows this)

Can be sloppy

  • don’t have to define a schema in advance

Standard

  • The most interesting thing about XML is that people are interested

in it!

  • Lots of free Java tools for parsing and munging XML
  • Lots of Microsoft tools (C#) for same

21

What XML does not solve

XML doesn’t standardize metadata

  • It only standardizes the metadata language
  • Not that much better than agreeing on an alphabet
  • E.g. my <price> tag vs. your <price> tag
  • Mine includes shipping and federal tax, and is in $US
  • Yours is manufacturer’s list price in ¥Japan
  • XML Schema is a proposal to help with some of this

XML doesn’t help with data modeling

  • No notions of IC’s, FD’s, etc.
  • In fact, encourages non-first-normal form!

You will probably have to translate to/from XML

  • Relational vendors will help with this ASAP
  • XML “features” (nesting, ordering, etc.) make this a pain
  • Flatten the XML if you want data independence (?)

22

XML Summary

XML is an important new standard for representing

data

It is mostly being used for messages between

business apps

  • Good because it’s easy to read and transmit
  • Can bundle a bunch of related data in one file

Also used for documents, separating content (XML)

from presentation (XSL)

Supports a number of non-first-normal-form

“features”

  • Hence troublesome as a “database”

Expect data to be translated to/from XML frequently

  • And expect to be given nice tools to help with that.

23

More on XML

100 books published in the last 3 years

  • Each seems to be 1000 pages

Try some websites

  • xml.org provides a business software view of XML
  • xml.apache.org has lots of useful shareware for XML
  • www.ibm.com/developerworks/xml/ has shareware,

tutorials, reference info

  • xml.com is the O’Reilly resource site
  • www.w3.org/XML/ is the official XML standard site