250 likes | 353 Views
XSLT. IST 421 Spring 2004 Lecture 7. XSL (eXtensible Stylesheet Language). CSS were designed to format content for display in a Web browser XSL style sheets provide presentation options for a wider range of output formats, including voice and print XSL is comprised of 2 major components:
E N D
XSLT IST 421 Spring 2004 Lecture 7
XSL (eXtensible Stylesheet Language) • CSS were designed to format content for display in a Web browser • XSL style sheets provide presentation options for a wider range of output formats, including voice and print • XSL is comprised of 2 major components: • XSL Formatting Objects (XML-FO) – document formatting vocabulary • XSL Transformations (XSLT) – transforms XML documents from one form to another
XSL Formatting Objects • XSL-FO – formatting objects that relate to various kinds of rectangular areas or their containers • Areas can contain text, blank space, and other formatting objects • Define the sequence in which content will be stored on various pages of an XSL document
XSL Transformations • XSLT – converts an XML document into other document formats including: • XML documents • HTML documents • PDF files
X Y Z aa Y bb XSLT Processor Source: an XML document Result: HTML Page X aa Z bb W cc
XSL Transformations • To use XSLT, you need: • A source XML file • An XSLT stylesheet • A program that supports XSLT Note: Since XSL is new, only the newest browser versions support XSL stylesheets.
XSL Transformations <?xml version="1.0"?> <invoice> <merchant_name>Giant Foods</merchant_name> <merchant_address> <street>123 Any Street</street> <city>Harrisburg</city> <state>PA</state> </merchant_address> <sales_date>04-10-03</sales_date> <item> <description>Milk</description> <quantity>1</quantity> <price>2.35</price> </item> <item> <description>Eggs</description> <quantity>1</quantity> <price>0.99</price> </item> </invoice>
SAXON • Application programming interface (API) • Conceived within the object oriented paradigm. • The Saxon XSLT Processor, developed by Michael Kay
Transforming XML to HTML Format • Involves analyzing contents of an XML document • Converts the format to a node tree, i.e. a hierarchical representation • XML Notepad, on the desktop or may be downloaded, offers an interface that graphically represents the tree structure of XML data • Looks for instructions on what to do with the nodes in the tree.
Patterns • Used by XSL to specify XML elements to which a template applies • The XML document is read by the processor (Saxon in our case) to see if there is a matching template. • Uses one or more templates or instructions • Templates contain patterns • Patterns match elements. • By applying patterns, we are adhering to a set of decision structures.
XSL Document First Step <?xml version = "1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns=“http://www.w3.org/TR/REC-html40” version=“1.0”> . . . </xsl:stylesheet>
Logic • First looks for the “root” element. A “/” normally identifies the root element. <xsl:template match="/"> • Inserts some very basic HTML markup and some text. • As each element is matched inserts a row in a table.
Explanation: Root template <xsl:template match="/"> . . </xsl:template> • The “/” is a pattern that matches root • The xsl: indicates a “namespace” and is essential here to distinguish XSLT from non XSLT elements.
HTML Script Between: (xsl:template match=“…….”) and (/xsl:template> <HTML> <HEAD> <TITLE>Product LIST</TITLE> </HEAD> <BODY> <xsl:apply-templates select=“invoice" /> </BODY> </HTML> Note: HTML code must adhere to XML format rules for well- formed documents
XSL Document: Adding template(s) <?xml version = “1.0” encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns=“http://www.w3.org/TR/REC-html40” version=“1.0”> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Product List</TITLE> </HEAD> <BODY> <xsl:apply-templates select=“*" /> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
XSL Document <xsl:template match="list"> <TABLE width="85%" BORDER="2" BGCOLOR="#94D6E7"> <xsl:for-each select="product[@soldout='yes']" > <TR> <TD WIDTH="50%" style=“color:white”> <xsl:value-of select="distributorName"/> </TD> </TR> </xsl:for-each> </TABLE><br/><hr/><br/> </xsl:template> </xsl:stylesheet>
Of Note: • Use of html within the xsl instructions • Use of a table to control screen width • xsl processing instructions: <xsl:template match=“invoice">….. </xsl:template> <xsl:for-each select=“*" > <xsl:value-of select=“description"/> . . </xsl:for-each>
The Process Processor will always look for the root of the XML document Typically will be: <xsl:template match=“/”> 3. Each template has a match attribute.
Transform the XML document to HTML with saxon.exe saxon.exe –o invoice.html invoice.xml invoice.xsl pause And – Save above in invoice.bat to cut down on your need for typing saxon.exe must be in same directory (or accommodate)