DOC PREVIEW
Duke CPS 116 - XSLT Lecture

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1XSLTCPS 116Introduction to Database Systems2Announcements (October 25) Homework #3 due in 1½ weeks Start early! Project milestone #2 due in 2 weeks3XSLT XML-to-XML rule-based transformation language Used most frequently as a stylesheet language An XSLT program is an XML document itself Current version is 2.0; W3C recommendation since January 2007XSLT processorXSLT programInput XML Output XMLActually, output does not need to be in XML in general4XSLT program An XSLT program is an XML document containing Elements in the <xsl:> namespace Elements in user namespace The result of evaluating an XSLT program on an input XML document =XML document =the XSLT document where each <xsl:> element has been replaced with the result of its evaluation Basic ideas Templates specify how to transform matching input nodes Structural recursion applies templates to input trees recursively Uses XPath as a sub-language5XSLT elements Element describing transformation rules <xsl:template> Elements describing rule execution control <xsl:apply-templates> <xsl:call-template> Elements describing instructions <xsl:if>, <xsl:for-each>, <xsl:sort>, etc. Elements generating output <xsl:value-of>, <xsl:attribute>, <xsl:copy-of>, <xsl:text>, etc.6XSLT example Find titles of books authored by “Abiteboul”<?xml version=“1.0”?><xsl:stylesheetxmlns:xsl=“http://www.w3.org/1999/XSL/Transform”version=“2.0”>[]Standard header of an XSLT document<xsl:template match=“book[author=‘Abiteboul’]”><booktitle><xsl:value-of select=“title”/></booktitle></xsl:template></xsl:stylesheet>Not quite; we will see why later27<xsl:template><xsl:template match=“book[author=‘Abiteboul’]”><booktitle><xsl:value-of select=“title”/></booktitle></xsl:template><xsl:template match=“match expr”>is the basic XSLT construct<xsl:template match=match_expr>is the basic XSLT construct describing a transformation rule match_expr is an XPath-like expression specifying which nodes this rule applies to <xsl:value-of select=“xpath_expr”/> evaluates xpath_exprwithin the context of the node matching the template, and converts the result sequence to a string <booktitle> and </booktitle> simply get copied to the output for each node match8Template in action<xsl:template match=“book[author=‘Abiteboul’]”><booktitle><xsl:value-of select=“title”/></booktitle></xsl:template> Example XML fragment<book ISBN=”ISBN-10” price=”80.00”>/Template applies<title>Foundations of Databases</title><author>Abiteboul</author><author>Hull</author><author>Vianu</author><publisher>Addison Wesley</publisher><year>1995</year><section>…</section>…</book><book ISBN=”ISBN-20” price=”40.00”><title>A First Course in Databases</title><author>Ullman</author><author>Widom</author><publisher>Prentice-Hall</publisher><year>2002</year><section>…</section>…</book>Te m p l a t e applies<booktitle>Foundations of Databases</booktitle>Template does not apply;default behavior is to process thenode recursively and print out alltext nodesA First Course in DatabasesUllmanWidomPrentice-Hall2002… …9Removing the extra output Add the following template:<xsl:template match=“text()|@*”/>This template matches all text and attributes XPath featurest t()id h h dtext()is a node test that matches any text node @* matches any attribute | means “or” in XPath Body of the rule is empty, so all text and attributes become empty string This rule effectively filters out things not matched by the other rule10<xsl:attribute> Again, find titles of books authored by “Abiteboul”; but make the output look like <book title=“booktitle”/>… …<xsl:template match=“book[author=‘Abiteboul’]”><book title=“{normalize-space(title)}”/></xsl:template>… … A more general method… …<xsl:template match=“book[author=‘Abiteboul’]”><book><xsl:attribute name=“title”><xsl:value-of select=“normalize-space(title)”/></xsl:attribute></book></xsl:template>… …<xsl:attribute name=“attr”>body</xsl:attribute>adds an attributed named attr with value body to theparent element in the output11<xsl:copy-of> Another slightly different example: return (entire) books authored by “Abiteboul”<?xml version=“1.0”?><xsl:stylesheetxmlns:xsl=“http://www.w3.org/1999/XSL/Transform”version=“2.0”><xsl:template match=“text()|@*”/><xsl:template match=“book[author=‘Abiteboul’]”><xsl:copy-of select=“.”/></xsl:template></xsl:stylesheet> <xsl:copy-of select=“xpath_expr”/> copies the entire contents (including tag structures) of the node-set returned by xpath_expr to the output12Formatting XML into HTML Example templates to Render a book title in italics in HTML Render the authors as a comma-separated list<xsl:template match=“book/title”><i><xsl:value-of select=“normalize-space(.)”/></i></xsl template></xsl:template><xsl:template match=“book/author[1]”><xsl:value-of select=“normalize-space(.)”/></xsl:template><xsl:template match=“book/author[position()>1]”><xsl:text>, </xsl:text><xsl:value-of select=“normalize-space(.)”/></xsl:template> <xsl:text> allows precise control of white space in output313<xsl:apply-templates> Example: generate a table of contents Display books in an HTML unordered list For each book, first display its title, and then display its sections in an HTML ordered list For each section, first display its title, and then display its subsections in an HTML ordered listsubsections in an HTML ordered list<xsl:template match=“title”><xsl:value-of select=“normalize-space(.)”/></xsl:template><xsl:template match=“section”><li><xsl:apply-templates select=“title”/><ol><xsl:apply-templates select=“section”/></ol></li></xsl:template>(Continue on next slide)<xsl:apply-templates select=“xpath_expr”/>applies templates recursively to the node-setreturned by xpath_expr14Example continued<xsl:template match=“book”><li><xsl:apply-templates select=“title”/><ol><xsl:apply-templates select=“section”/></ol></li></xsl:template><xsl:template match=“bibliography”><html>html<head><title>Bibliography</title></head><body><ul><xsl:apply-templates select=“book”/></ul></body></html></xsl:template> One problem remains Even if a book or a section has no sections, we will still generate an empty


View Full Document

Duke CPS 116 - XSLT Lecture

Documents in this Course
Part I

Part I

8 pages

XSLT

XSLT

4 pages

XSLT

XSLT

8 pages

Part I

Part I

8 pages

XSLT

XSLT

8 pages

Load more
Download XSLT Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view XSLT Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view XSLT Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?