cs 433 xml, dtd, xpath, & xslt

45
CS 433 Xml, DTD, XPath, & Xslt Extensible Markup and Beyond September 26, 2001 Jeff Derstadt

Upload: louis-durham

Post on 03-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

CS 433 Xml, DTD, XPath, & Xslt. Extensible Markup and Beyond September 26, 2001 Jeff Derstadt. Administration. Due: Friday Sept. 28 th Relational table creation and summary See course web site for more details Logging into Egret Questions?. Overview. Xml - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 433 Xml, DTD, XPath, & Xslt

CS 433Xml, DTD, XPath, & Xslt

Extensible Markup and Beyond

September 26, 2001Jeff Derstadt

Page 2: CS 433 Xml, DTD, XPath, & Xslt

Administration

Due: Friday Sept. 28th

Relational table creation and summary

See course web site for more details

Logging into EgretQuestions?

Page 3: CS 433 Xml, DTD, XPath, & Xslt

Overview

Xml A self-describing, hierarchal data model

DTD Standardizing schemas for Xml

XPath How to navigate and query Xml documents

Xslt How to transform one Xml document into

another Xml document

Page 4: CS 433 Xml, DTD, XPath, & Xslt

Xml – An Example

<class name=‘CS 433’><location building=‘Olin’ room=‘255’/><professor>Johannes Gehrke</professor><ta>Jeff</ta><student_list>

<student id=‘999-991’>John Smith</student>

<student id=‘999-992’>Jane Doe</student></student_list>

</class>

Page 5: CS 433 Xml, DTD, XPath, & Xslt

Xml – Extensible Markup Language

Language A way of communicating information

Markup Notes or meta-data that describe your

data or language

Extensible Limitless ability to define new

languages or data sets

Page 6: CS 433 Xml, DTD, XPath, & Xslt

Xml – What’s The Point?

You can include your data and a description of what the data represents This is useful for defining your own

language or protocol

Example: Chemical Markup Language<molecule>

<weight>234.5</weight><Spectra>…</Spectra><Figures>…</Figures>

</molecule>

Page 7: CS 433 Xml, DTD, XPath, & Xslt

Xml – Structure

Xml looks like HTMLXml is a hierarchy of user-defined tags called elements with attributes and dataData is described by elements, elements are described by attributes

<student id=‘999-991’>John Smith</student>closing tagattribute

attribute value

dataopen tagelement name

Page 8: CS 433 Xml, DTD, XPath, & Xslt

Xml – Elements

<student id=‘999-991’>John Smith</student>

Xml is case and space sensitiveElement opening and closing tag names must be identicalOpening tags: “<” + element name + “>”Closing tags: “</” + element name + “>”Empty Elements have no data and no closing tag: They begin with a “<“ and end with a “/>”

<location/>

closing tagattribute

attribute value

dataopen tagelement name

Page 9: CS 433 Xml, DTD, XPath, & Xslt

Xml – Attributes

<student id=‘999-991’>John Smith</student>

Attributes provide additional information for element tags.There can be zero or more attributes in every element; each one has the the form:

attribute_name=‘attribute_value’- There is no space between the name and the “=‘”- Attribute values must be surrounded by “ or ‘ characters

Multiple attributes are separated by white space (one or more spaces or tabs).

closing tagattribute

attribute value

dataopen tagelement name

Page 10: CS 433 Xml, DTD, XPath, & Xslt

Xml - Data

<student id=‘999-991’>John Smith</student>

Xml data is any information between an opening and closing tagXml data must not contain the ‘<‘ or ‘>’ characters

closing tagattribute

attribute value

dataopen tagelement name

Page 11: CS 433 Xml, DTD, XPath, & Xslt

Xml – Nesting & Hierarchy

Xml tags can be nested in a tree hierarchyXml documents can have only one root tagBetween an opening and closing tag you can insert:

1. Data2. More Elements3. A combination of data and elements

<root> <tag1> Some Text <tag2>More</tag2> </tag1></root>

Page 12: CS 433 Xml, DTD, XPath, & Xslt

Xml – Storage

Storage is done just like an n-ary tree (DOM)

<root>

<tag1>

Some Text

<tag2>More</tag2>

</tag1>

</root>

NodeType: Element_NodeName: ElementValue: Root

NodeType: Element_NodeName: ElementValue: tag1

NodeType: Text_NodeName: TextValue: More

NodeType: Element_NodeName: ElementValue: tag2

NodeType: Text_NodeName: TextValue: Some Text

Page 13: CS 433 Xml, DTD, XPath, & Xslt

Xml vs. Relational Model

Id Speed RAM HD

101

800Mhz

256MB

40GB

102

933Mhz

512MB

40GB

Computer Table

<Table>

<Computer Id=‘101’>

<Speed>800Mhz</Speed>

<RAM>256MB</RAM>

<HD>40GB</HD>

</Computer>

<Computer Id=‘102’>

<Speed>933Mhz</Speed>

<RAM>512MB</RAM>

<HD>40GB</HD>

</Computer>

</Table>

Page 14: CS 433 Xml, DTD, XPath, & Xslt

DTD – Document Type Definition

A DTD is a schema for Xml dataXml protocols and languages can be standardized with DTD filesA DTD says what elements and attributes are required or optional Defines the formal structure of the

language

Page 15: CS 433 Xml, DTD, XPath, & Xslt

DTD – An Example<?xml version='1.0'?><!ELEMENT Basket (Cherry+, (Apple | Orange)*) >

<!ELEMENT Cherry EMPTY><!ATTLIST Cherry flavor CDATA #REQUIRED>

<!ELEMENT Apple EMPTY><!ATTLIST Apple color CDATA #REQUIRED>

<!ELEMENT Orange EMPTY><!ATTLIST Orange location ‘Florida’>

-------------------------------------------------------------------------------- <Basket>

<Apple/> <Cherry flavor=‘good’/> <Orange/></Basket>

<Basket> <Cherry flavor=‘good’/> <Apple color=‘red’/> <Apple color=‘green’/></Basket>

Page 16: CS 433 Xml, DTD, XPath, & Xslt

DTD - !ELEMENT

<!ELEMENT Basket (Cherry+, (Apple | Orange)*) >

!ELEMENT declares an element name, and what children elements it should have Wildcards: * Zero or more + One or more

Name Children

Page 17: CS 433 Xml, DTD, XPath, & Xslt

DTD - !ATTLIST

<!ATTLIST Cherry flavor CDATA #REQUIRED>

<!ATTLIST Orange location CDATA #REQUIREDcolor ‘orange’>

!ATTLIST defines a list of attributes for an elementAttributes can be of different types, can be required or not required, and they can have default values.

Element Attribute Type Flag

Page 18: CS 433 Xml, DTD, XPath, & Xslt

DTD –Well-Formed and Valid

<?xml version='1.0'?><!ELEMENT Basket (Cherry+)>

<!ELEMENT Cherry EMPTY><!ATTLIST Cherry flavor CDATA #REQUIRED>

--------------------------------------------------------------------------------

Well-Formed and Valid<Basket> <Cherry flavor=‘good’/></Basket>

Not Well-Formed<basket> <Cherry flavor=good></Basket>

Well-Formed but Invalid<Job> <Location>Home</Location></Job>

Page 19: CS 433 Xml, DTD, XPath, & Xslt

XPath – Navigating Xml

When Xml is stored in a tree, XPath allows you to navigate to different nodes:

Class

Student Student

Text:Jeff

Text:Pat

<Class>

<Student>Jeff</Student>

<Student>Pat</Student>

</Class>

Page 20: CS 433 Xml, DTD, XPath, & Xslt

XPath – Navigating Xml

Xml is similar to a file structure, but you can select more than one node:

//Class/Student Class

Student Student

Text:Jeff

Text:Pat

<Class>

<Student>Jeff</Student>

<Student>Pat</Student>

</Class>

Page 21: CS 433 Xml, DTD, XPath, & Xslt

XPath – Navigating Xml

An XPath expression looks just like a file path Elements are accessed as /<element>/ Attributes are accessed as @attribute

Everything that satisfies the path is selected You can add constraints in brackets [ ]

to further refine your selection

Page 22: CS 433 Xml, DTD, XPath, & Xslt

XPath – Navigating Xml<class name=‘CS 433’> <location building=‘Olin’ room=‘255’/> <professor>Johannes Gehrke</professor> <ta>Dan Kifer </ta> <student_list> <student id=‘999-991’>John Smith</student> <student id=‘999-992’>Jane Doe</student> </student_list></class>

//class[@name=‘CS 433’]/student_list/student/@id

Starting Element

Attribute Constraint

Element Path

Selection

Selection Result: The attribute nodes containing 999-991 and 999-992

Page 23: CS 433 Xml, DTD, XPath, & Xslt

XPath - Context

Context – your current focus in an Xml document Use:

//<root>/… When you want to start from the

beginning of the Xml document

Page 24: CS 433 Xml, DTD, XPath, & Xslt

XPath - Context

Student Student

Text:Jeff

Text:Pat

Prof

Text:Gehrke

ListLocation

Attr:Olin

Class

XPath: List/Student

Page 25: CS 433 Xml, DTD, XPath, & Xslt

XPath - Context

Student Student

Text:Jeff

Text:Pat

Prof

Text:Gehrke

ListLocation

Attr:Olin

Class

XPath: Student

Page 26: CS 433 Xml, DTD, XPath, & Xslt

XPath – Examples

<Basket><Cherry flavor=‘sweet’/><Cherry flavor=‘bitter’/><Cherry/><Apple color=‘red’/><Apple color=‘red’/><Apple color=‘green’/>…

</Basket>

Select all of the red apples:

//Basket/Apple[@color=‘red’]

Page 27: CS 433 Xml, DTD, XPath, & Xslt

XPath – Examples

<Basket><Cherry flavor=‘sweet’/><Cherry flavor=‘bitter’/><Cherry/><Apple color=‘red’/><Apple color=‘red’/><Apple color=‘green’/>…

</Basket>

Select the cherries that have some flavor:

//Basket/Cherry[@flavor]

Page 28: CS 433 Xml, DTD, XPath, & Xslt

XPath – Examples

<orchard> <tree> <apple color=‘red’/> <apple color=‘red’/> </tree> <basket> <apple color=‘green’/> <orange/> </basket></orchard>

Select all the apples in the orchard:

//orchard/descendant()/apple

Page 29: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 30: CS 433 Xml, DTD, XPath, & Xslt

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!)

Page 31: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 32: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 33: CS 433 Xml, DTD, XPath, & Xslt

Xslt – Templates

Xslt stylesheets are a collection of templates Templates are like functions The body of a template is the output

of a transformation

Page 34: CS 433 Xml, DTD, XPath, & Xslt

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

Page 35: CS 433 Xml, DTD, XPath, & Xslt

Xslt – Template MatchingStylesheet<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>

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>

Page 36: CS 433 Xml, DTD, XPath, & Xslt

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

Page 37: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 38: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 39: CS 433 Xml, DTD, XPath, & Xslt

Xslt – for-each Instruction

<xsl:for-each select=‘item’> instruction is similar to a foreach iterator or a for loopThe select attribute selects a set of elements from an Xml document

Page 40: CS 433 Xml, DTD, XPath, & Xslt

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.

Page 41: CS 433 Xml, DTD, XPath, & Xslt

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>

Page 42: CS 433 Xml, DTD, XPath, & Xslt

Xslt – Other Information

W3C is standardizing XPath and Xslt:

http://www.w3.org/TR/xslt.html

http://www.w3.org/TR/xpath.html

Lot’s of Books. Here’s a suggestion: D. Martin et al. Professional Xml. Wrox

Press, 2000.

Page 43: CS 433 Xml, DTD, XPath, & Xslt

What’s Next?

XSchema DTDs, but written in XML Will replace DTDs

XQuery Fully declarative XML query language Will be able to do anything you can

do with XPath and XSLT, plus a LOT more

Page 44: CS 433 Xml, DTD, XPath, & Xslt

Xml in Commercial Databases

Many Xml parsers and XSLT engines are availbableMicrosoft, IBM, and Oracle (among others) are adding native Xml supportNative Xml databases

Page 45: CS 433 Xml, DTD, XPath, & Xslt

URL Tutorials

http://msdn.microsoft.com/xml/tutorial/default.asp

http://www.ils.unc.edu/~kempa/inls259/xml/

http://www.geocities.com/SiliconValley/Peaks/5957/10minxml.html