1
play

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


  1. 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 any depth //paper matches a paper at any depth University of Washington 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… 1 2 Overview Xslt – Transforming Xml � Querying XML: XPath Amazon.com order form: <single_book_order> � Transforming XML: XSLT <title>Databases</title> <qty>1</qty> </single_book_order> Supplier ’ s order form: <form7957> <purchase item= ’ book ’ property= ’ title ’ value= ’ Databases ’ quantity= ’ 1 ’ /> </form7957> 3 4 Xslt - Extensible Style Language for Xslt – A First Look Transformation < single_book_order> < title> Databases< /title> � Xslt is a language for transforming or < qty> 1< /qty> converting one Xml format into another Xml < /single_book_order> format. < form7957> � Benefits: < purchase item= ’book’ property= ’title’ value= ’Databases’ quantity= ’1’/> < /form7957> • No need to parse or interpret many different Xml formats – they can all be transformed to a single < ?xml version= '1.0'?> format to facilitate interpretation < xsl:stylesheet xmlns:xsl= 'http://www.w3.org/1999/XSL/Transform' version= '1.0'> < xsl:template match= 'single_book_order'> • Language looks like Xml! (remember, Xml defines < form7957> < purchase item= 'book' property= 'title' value= '{ title} ‘ languages!) quantity= '{ qty} '/> < /form7957> < /xsl:template> < /xsl:stylesheet> 5 6 1

  2. Xslt – Header Xslt – Templates � Xslt stylesheets MUST include this body: � Xslt stylesheets are a collection of templates • Templates are like functions <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'> • The body of a template is the output of a … transformation </xsl:stylesheet> 7 8 Xslt - Templates Xslt – Template Matching Stylesheet Xml <xsl:template match=‘basket’> <basket> � You define a template with the <new_basket> <apple color=‘red’/> <xsl:apply-templates select=‘apple’/> <apple color=‘green/> <xsl:template match=‘’> instruction <xsl:apply-templates select=‘box’/> <apple color=‘green/> </new_basket> <box> � You call a template with the </xsl:template> <orange taste=‘good’/> <peach/> <xsl:apply-templates select=‘’> instruction <xsl:template match=‘apple’> <apple color=‘red’/> <apple/> </box> 1. All elements or attributes that satisfy the select attribute </xsl:template> </basket> expression are selected. <xsl:template match=‘box’> Transformed Xml: 2. For each element or attribute that is selected: <box/> <new_basket> i. A matching template is found in the stylesheet. <xsl:apply-templates/> <apple/> <apple/> <apple/> <xsl:template> <box/><apple/> ii. The body of the template is executed. </new_basket> 9 10 Xslt – choose Instruction Xslt – choose Example Original Xml: <customer> � <xsl:choose> instruction is similar to a C++ or <order id=‘5’> <item><title>Database Management Systems</title></item> Java switch statement </order> </customer> � <xsl:when test=‘’> instruction is similar to the Xslt Stylesheet: <xsl:template match=‘customer’> � FUNCTION case statement <xsl:choose> � SWITCH <xsl:when test='order/@id'> � CASE <single_book_order> � <xsl:otherwise> instruction is similar to the <title><xsl:value-of select='order/item/title'/></title> </single_book_order> default statement </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> 11 12 2

  3. Xslt – choose Example 2 Xslt – for-each Instruction Original Xml: <customer> � <xsl:for-each select=‘item’> instruction is <order> <item><title>Database Management Systems</title></item> similar to a foreach iterator or a for loop </order> </customer> � The select attribute selects a set of elements Xslt Stylesheet: <xsl:template match=‘customer’> � FUNCTION from an Xml document <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> 13 14 Xslt – if Instruction Xslt – for-each and if Example Original Xml: <basket> <apple color=‘red’ condition=‘yummy’/> � <xsl:if test=‘’> instruction is similar to an if statement <apple color=‘green’ condition=‘wormy/> in Java or C++ <apple color=‘red’ condition=‘crisp’/> </basket> � The test attribute is the if condition: Xslt Stylesheet: <xsl:template match=‘basket’> � FUNCTION • True <condition_report> <xsl:for-each select=‘apple’> � FOR LOOP • statement is true <xsl:if test=“contains(@color, ‘red’)”> � IF • test returns an element or attribute. <condition><xsl:value-of select=‘@condition’/></condition> </xsl:if> • False </xsl:for-each> • statement is false </condition_report> </xsl:template> • test returns nothing � There is no ‘else’, so use the <xsl:choose> operator in Output Xml: <condition_report> <condition>yummy</condition> this situation. <condition>crisp</condition> </condition_report> 15 16 Other miscellaneous features Reminder: Benefits of Relational � XML docs can have IDs and IDREFs, URIs • reference to another document or document element � Data independence buys you: � Document Object Model (DOM) • Evolution of storage -- vs. XML? • A tree “object” API for traversing an XML or HTML doc • Evolution of schema (via views) – vs. XML? • Typically for Java � Database design theory � XML Schema is a proposal to replace/augment DTDs • IC’s, dependency theory, lots of nice tools for ER • Has a notion of types and typechecking � Remember, databases are long-lived and reused • May introduce some notions of IC’s • Today’s “nesting” might need to be inverted tomorrow! • Quite complicated, controversial ... not really adopted yet � Upshot: � XML Namespaces • XML is good for transient data (e.g. messages) • Can import tag names from others • XML is fine for data that will not get reused in a different • Disambiguate by prefixing the namespace name way (e.g. Shakespeare, database output like reports) • I.e. berkeley-eecs:gpa is different from uphoenix:gpa • Relational is far cleaner for persistent data � Lots of other details, tools, etc. 17 18 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend