120 likes | 204 Views
Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat WebSite: http://www.cs.toronto.edu/~wael. XML and XSL Combined. XML Document. XSL Tree. XML Application/Tree. /. XML Parser. XSL Parser. DTD Document. XSL Document. HTML Document.
E N D
Programming on the Web(CSC309F) Tutorial 2: XSL & XSLT TA:Wael Abouelsaadat WebSite: http://www.cs.toronto.edu/~wael
XML and XSL Combined XML Document XSL Tree XML Application/Tree / XML Parser XSL Parser DTD Document XSL Document HTML Document
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….
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>
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>
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)"
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>
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>
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>
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>
Sites: • http://www.xslinfo.com • http://www.mulberrytech.com/xsl/xsl-list