42
Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

Embed Size (px)

Citation preview

Page 1: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

Xml, DTD, XPath, & Xslt

Extensible Markup and Beyond

Page 2: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 3: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

Xml – An Example

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

Page 4: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 5: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 6: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 7: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 8: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 9: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 10: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 11: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

Xml – Storage

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

<root>

<tag1>

Some Text

<tag2>More</tag2>

</tag1>

</root>

NodeType: Element_NodeName: -Value: Root

NodeType: Element_NodeName: -Value: tag1

NodeType: Text_NodeName: TextValue: More

NodeType: Element_NodeName: ElementValue: tag2

NodeType: Text_NodeName: TextValue: Some Text

Page 12: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 13: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 14: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 15: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

DTD - !ELEMENT

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

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

Name Children

Page 16: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 17: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 18: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 19: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 20: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 21: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 22: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 23: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

XPath - Context

Student Student

Text:Jeff

Text:Pat

Prof

Text:Gehrke

ListLocation

Attr:Olin

Class

XPath: List/Student

Page 24: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

XPath - Context

Student Student

Text:Jeff

Text:Pat

Prof

Text:Gehrke

ListLocation

Attr:Olin

Class

XPath: Student

Page 25: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 26: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 27: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 28: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 29: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 30: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 31: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 32: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 33: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 34: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 35: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 36: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 37: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 38: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 39: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 40: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 41: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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 42: Xml, DTD, XPath, & Xslt Extensible Markup and Beyond

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