1XSLTCPS 116Introduction to Database Systems2Announcements (October 21) Midterm has been graded Sample solution is available Please verify your score on Blackboard Homework #2 should be graded by Thursday Homework #3 will be handed out on Thursday Project milestone #2 due in 3 weeks Email feedback on milestone #1 by this weekend3XSLT 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 general24XSLT 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 later37<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 dtext()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 rule410<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 output513<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
View Full Document