Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat

Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat

Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat WebSite: http://www.cs.toronto.edu/~wael 1 XML and XSL Combined XML Document XSL Tree XML Application/Tree / XML Parser XSL Parser DTD Document XSL Document HTML Document 2 XSL: An Example Oriented Programming Language Ø XSL or XSLT? Ø What is an Example Oriented Programming Language? ØMain features of XSLT: § 50 formatting element types, 230 properties! § Template driven (vs function driven) § Full fledged programming language( loops, switch/case, variables,…) but not a general purpose one…. 3 Example 1: Generating CSV File Using XSLT <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0”> <xsl:output method="text“ /> <!-- This stylesheet outputs the book list as a CSV file --> <xsl:template match="BOOKLIST"> <xsl:apply-templates select="BOOKS"/> </xsl:template> <xsl:template match="BOOKS"> <xsl:text> Title,Author,Category </xsl:text> 4 Example 1: Generating CSV File Using XSLT <xsl:for-each select="ITEM"> <xsl:text> "<xsl:value-of select="TITLE"/>", "<xsl:value-of select="AUTHOR"/>","<xsl:value-of select="@CAT"/>( </xsl:text> <xsl:choose> <xsl:when test='@CAT="F"'>Fiction</xsl:when> <xsl:when test='@CAT="S"'>Science</xsl:when> <xsl:when test='@CAT="C"'>Computing</xsl:when> <xsl:when test='@CAT="X"'>Crime</xsl:when> <xsl:otherwise>Unclassified</xsl:otherwise> </xsl:choose> <xsl:text> )“ </xsl:text> </xsl:for-each> <xsl:text> 5 </xsl:text> </xsl:template> </xsl:stylesheet> Example1-Output: Generating CSV File Using XSLT books.csv Title,Author,Category "Number, the Language of Science","Danzig","S(Science)" "Tales of Grandpa Cat","Wardlaw, Lee","F(Fiction)" "Learn Java Now","Stephen R. Davis","C(Computing)" "Design Patterns","Erich Gamma, Richard Helm,JohnVlissides",“C(Computing)" 6 XSL-Example 2: Generating Nicely Formatted HTML Using XSLT <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:saxon="http://icl.com/saxon" exclude-result-prefixes="saxon" > <xsl:variable name="bgcolor">x00ffff</xsl:variable> <xsl:variable name="fontcolor" select="'xff0080'"/> <xsl:template match="/"> <HTML> <xsl:call-template name="header"> <xsl:with-param name="title" select="'Book List'"/> </xsl:call-template> <BODY BGCOLOR="{$bgcolor}"> <FONT COLOR="{$fontcolor}"> <xsl:apply-templates/> </FONT> </BODY> </HTML> </xsl:template> 7 Example 2: Generating Nicely Formatted HTML Using XSLT cont’d <xsl:template name="header" xml:space="preserve"> <xsl:param name="title" select="'Default Title'"/> <HEAD> <TITLE> <xsl:value-of select="$title"/></TITLE> </HEAD> </xsl:template> <xsl:template match="BOOKLIST"> <H2>A complete list of books, grouped by author</H2> <xsl:apply-templates select="child :: BOOKS" mode="by-author"/> </xsl:template> <xsl:template match="BOOKS" mode="by-author"> <div xsl:extension-element-prefixes="saxon"> <saxon:group select="ITEM" group-by="AUTHOR"> <xsl:sort select="AUTHOR" order="ascending"/> <xsl:sort select="TITLE" order="ascending"/> <h3>AUTHOR: <xsl:value-of select="AUTHOR"/> ( <xsl:value-of select="position()"/> of <xsl:value-of select="last()"/>)</h3> 8 Example 2: Generating Nicely Formatted HTML Using XSLT cont’d <TABLE> <saxon:item> <TR> <TD WIDTH="100" VALIGN="TOP"><xsl:number format="i"/></TD> <TD> TITLE: <xsl:value-of select="TITLE"/><BR/> CATEGORY:<xsl:value-of select="id(@CAT)/@DESC" /> (<xsl:value-of select="@CAT" />) </TD> </TR> </saxon:item> </TABLE> <HR/> </saxon:group> </div> </xsl:template> 9 </xsl:transform> Example 2: Generating Nicely Formatted HTML Using XSLT 10 XSL Programming Tips Ø Try this out: java -cp /u/csc309h/lib/saxon.jar com.icl.saxon.StyleSheet books.xml books.xsl > books.html ØCode Style: ØUse Good Indentation ØBe Explicit, e.g. do not write blahblahblah<xsl:text/> but instead write <xsl:text>blahblahblah</xsl:text> 11 Sites: Ø http://www.xslinfo.com Ø http://www.mulberrytech.com/xsl/xsl-list 12.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us