1 / 19

XSLT

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:

lydiai
Télécharger la présentation

XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XSLT IST 421 Spring 2004 Lecture 7

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

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

  4. XSL Transformations • XSLT – converts an XML document into other document formats including: • XML documents • HTML documents • PDF files

  5. X Y Z aa Y bb XSLT Processor Source: an XML document Result: HTML Page X aa Z bb W cc

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

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

  8. SAXON • Application programming interface (API) • Conceived within the object oriented paradigm. • The Saxon XSLT Processor, developed by Michael Kay

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

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

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

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

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

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

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

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

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

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

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

More Related