Download presentation
Presentation is loading. Please wait.
Published byMildred Russell Modified over 6 years ago
1
Extensible Markup and Beyond September 26, 2001 Jeff Derstadt
CS 433 Xml, DTD, XPath, & Xslt Extensible Markup and Beyond September 26, 2001 Jeff Derstadt
2
Administration Due: Friday Sept. 28th Logging into Egret Questions?
Relational table creation and summary See course web site for more details Logging into Egret Questions?
3
Overview Xml DTD XPath Xslt A self-describing, hierarchal data model
Standardizing schemas for Xml XPath How to navigate and query Xml documents Xslt How to transform one Xml document into another Xml document
4
Xml – An Example <class name=‘CS 433’>
<location building=‘Olin’ room=‘255’/> <professor>Johannes Gehrke</professor> <ta>Jeff</ta> <student_list> <student id=‘ ’>John Smith</student> <student id=‘ ’>Jane Doe</student> </student_list> </class>
5
Xml – Extensible Markup 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
6
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>
7
Xml – Structure Xml looks like HTML
Xml is a hierarchy of user-defined tags called elements with attributes and data Data is described by elements, elements are described by attributes <student id=‘ ’>John Smith</student> attribute attribute value data closing tag open tag element name
8
Xml – Elements <student id=‘999-991’>John Smith</student>
Xml is case and space sensitive Element opening and closing tag names must be identical Opening 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/> attribute data closing tag open tag element name attribute value
9
Xml – Attributes <student id=‘ ’>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). open tag element name attribute attribute value data closing tag
10
Xml - Data <student id=‘999-991’>John Smith</student>
Xml data is any information between an opening and closing tag Xml data must not contain the ‘<‘ or ‘>’ characters open tag element name attribute attribute value data closing tag
11
Xml – Nesting & Hierarchy
Xml tags can be nested in a tree hierarchy Xml documents can have only one root tag Between an opening and closing tag you can insert: 1. Data 2. More Elements 3. A combination of data and elements <root> <tag1> Some Text <tag2>More</tag2> </tag1> </root>
12
Xml – Storage Storage is done just like an n-ary tree (DOM)
<root> <tag1> Some Text <tag2>More</tag2> </tag1> </root> Node Type: Element_Node Name: Element Value: Root Node Type: Element_Node Name: Element Value: tag1 Node Type: Element_Node Name: Element Value: tag2 Type: Text_Node Name: Text Value: Some Text Node Type: Text_Node Name: Text Value: More
13
Xml vs. Relational Model
<Table> <Computer Id=‘101’> <Speed>800Mhz</Speed> <RAM>256MB</RAM> <HD>40GB</HD> </Computer> <Computer Id=‘102’> <Speed>933Mhz</Speed> <RAM>512MB</RAM> </Table> Computer Table Id Speed RAM HD 101 800Mhz 256MB 40GB 102 933Mhz 512MB
14
DTD – Document Type Definition
A DTD is a schema for Xml data Xml protocols and languages can be standardized with DTD files A DTD says what elements and attributes are required or optional Defines the formal structure of the language
15
DTD – An Example <Basket> <Cherry flavor=‘good’/>
<?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> <Cherry flavor=‘good’/> <Apple color=‘red’/> <Apple color=‘green’/> </Basket> <Basket> <Apple/> <Cherry flavor=‘good’/> <Orange/> </Basket>
16
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
17
DTD - !ATTLIST !ATTLIST defines a list of attributes for an element
<!ATTLIST Cherry flavor CDATA #REQUIRED> <!ATTLIST Orange location CDATA #REQUIRED color ‘orange’> !ATTLIST defines a list of attributes for an element Attributes can be of different types, can be required or not required, and they can have default values. Element Attribute Type Flag
18
DTD –Well-Formed and Valid
<?xml version='1.0'?> <!ELEMENT Basket (Cherry+)> <!ELEMENT Cherry EMPTY> <!ATTLIST Cherry flavor CDATA #REQUIRED> Not Well-Formed <basket> <Cherry flavor=good> </Basket> Well-Formed but Invalid <Job> <Location>Home</Location> </Job> Well-Formed and Valid <Basket> <Cherry flavor=‘good’/> </Basket>
19
XPath – Navigating Xml When Xml is stored in a tree, XPath allows you to navigate to different nodes: Class <Class> <Student>Jeff</Student> <Student>Pat</Student> </Class> Student Student Text: Jeff Text: Pat
20
XPath – Navigating Xml Xml is similar to a file structure, but you can select more than one node: //Class/Student Class <Class> <Student>Jeff</Student> <Student>Pat</Student> </Class> Student Student Text: Jeff Text: Pat
21
XPath – Navigating Xml An XPath expression looks just like a file path
Elements are accessed as /<element>/ Attributes are accessed Everything that satisfies the path is selected You can add constraints in brackets [ ] to further refine your selection
22
XPath – Navigating Xml Starting Element Attribute Constraint
<class name=‘CS 433’> <location building=‘Olin’ room=‘255’/> <professor>Johannes Gehrke</professor> <ta>Dan Kifer </ta> <student_list> <student id=‘ ’>John Smith</student> <student id=‘ ’>Jane Doe</student> </student_list> </class> Starting Element Attribute Constraint Element Path Selection Selection Result: The attribute nodes containing and
23
XPath - Context Context – your current focus in an Xml document Use:
//<root>/… When you want to start from the beginning of the Xml document
24
XPath - Context XPath: List/Student Class Prof Location List Text:
Gehrke Attr: Olin Student Student Text: Jeff Text: Pat
25
XPath - Context XPath: Student Student Text: Jeff Pat Prof Gehrke List
Location Attr: Olin Class
26
XPath – Examples Select all of the red apples:
<Basket> <Cherry flavor=‘sweet’/> <Cherry flavor=‘bitter’/> <Cherry/> <Apple color=‘red’/> <Apple color=‘green’/> … </Basket> Select all of the red apples:
27
XPath – Examples Select the cherries that have some flavor:
<Basket> <Cherry flavor=‘sweet’/> <Cherry flavor=‘bitter’/> <Cherry/> <Apple color=‘red’/> <Apple color=‘green’/> … </Basket> Select the cherries that have some flavor:
28
XPath – Examples Select all the apples in the orchard:
<tree> <apple color=‘red’/> </tree> <basket> <apple color=‘green’/> <orange/> </basket> </orchard> Select all the apples in the orchard: //orchard/descendant()/apple
29
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>
30
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!)
31
Xslt – A First Look <?xml version='1.0'?>
<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=' version='1.0'> <xsl:template match='single_book_order'> <form7957><purchase item='book' property='title' value='{title}‘ quantity='{qty}'/></form7957> </xsl:template> </xsl:stylesheet>
32
Xslt – Header Xslt stylesheets MUST include this body:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl=' version='1.0'> … </xsl:stylesheet>
33
Xslt – Templates Xslt stylesheets are a collection of templates
Templates are like functions The body of a template is the output of a transformation
34
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.
35
Xslt – Template Matching
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 match=‘box’> <box/> <xsl:apply-templates/> <xsl:template> Xml <basket> <apple color=‘red’/> <apple color=‘green/> <box> <orange taste=‘good’/> <peach/> </box> </basket> Transformed Xml: <apple/> <apple/> <apple/> <box/><apple/>
36
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
37
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 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>
38
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 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>
39
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
40
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.
41
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 ‘red’)”> IF <condition><xsl:value-of </xsl:if> </xsl:for-each> </condition_report> </xsl:template> Output Xml: <condition_report> <condition>yummy</condition> <condition>crisp</condition>
42
Xslt – Other Information
W3C is standardizing XPath and Xslt: Lot’s of Books. Here’s a suggestion: D. Martin et al. Professional Xml. Wrox Press, 2000.
43
What’s Next? XSchema XQuery DTDs, but written in XML Will replace DTDs
Fully declarative XML query language Will be able to do anything you can do with XPath and XSLT, plus a LOT more
44
Xml in Commercial Databases
Many Xml parsers and XSLT engines are availbable Microsoft, IBM, and Oracle (among others) are adding native Xml support Native Xml databases
45
URL Tutorials http://msdn.microsoft.com/xml/tutorial/default.asp
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.