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

melva
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat PowerPoint Presentation
Download Presentation
Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat

play fullscreen
1 / 12
Download Presentation
Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat
68 Views
Download Presentation

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat WebSite: http://www.cs.toronto.edu/~wael

  2. XML and XSL Combined XML Document XSL Tree XML Application/Tree / XML Parser XSL Parser DTD Document XSL Document HTML Document

  3. 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….

  4. 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>

  5. 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> </xsl:text> </xsl:template> </xsl:stylesheet>

  6. 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)"

  7. 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>

  8. 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>

  9. 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> </xsl:transform>

  10. Example 2: Generating Nicely Formatted HTML Using XSLT

  11. 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>

  12. Sites: • http://www.xslinfo.com • http://www.mulberrytech.com/xsl/xsl-list