Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applying eXtensible Style Sheets (XSL)

Similar presentations


Presentation on theme: "Applying eXtensible Style Sheets (XSL)"— Presentation transcript:

1 Applying eXtensible Style Sheets (XSL)
Ellen Pearlman Eileen Mullin Programming the Web Using XML

2 Learning Objectives Learn how XSL can transform XML documents into other formats such as HTML Examine how an XSL style sheet consists of a set of rules called templates Discover the three components that comprise XSL: XSLT, XPath, and XSL Formatting Objects Use XSLT to output an XML source document into an XML result document. Use XPath expressions in matching patterns to locate parts of an XML document.

3 CSS and XSL You can use XSL to transform documents – for example, to transform XML data into an HTML document with a CSS stylesheet on a Web server. In this way, XSL and CSS complement each other and can even be used together. You can also use XSL to format data based its value – for example, to display negative numbers in a financial report in red.

4 Understanding XSL XSL consists of three parts:
XSLT, a language for transforming XML documents XPath, a language that defines parts of an XML document XSL Formatting Objects, a vocabulary for styling XML documents

5 How XSL Works XSL can filter and sort XML data using the criteria you define, as well as format its display based on the value of the data itself. When it’s output, you can use XSL to send your XML data to various devices, including handhelds, print, or voice output.

6 How XSL Works (2) The XSLT processor reads an XML file and an XSLT stylesheet, then outputs another file based on the instructions found in the stylesheet.

7 Using XSLT to Transform XML Documents with XSL
XSLT is the part of XSL that can turn one XML document into another. XSLT is a template-based programming language. An XSLT template can also introduce new XML elements into the output document it creates, or remove others. XSLT lets different kinds of software applications exchange XML-enabled data with one another.

8 Using XSLT to Convert an XML Source Tree Into an XML Result Tree

9 How XSLT Works You need two starting documents for an XSLT transformation – an XML “source” document and an XSLT stylesheet – used to create a single "result" document. The XSLT stylesheet is applied to an XML document’s source tree to generate a result tree that is typically saved as an XML or HTML document. Once a match is found, XSLT transforms the matching part of the source document into the result document.

10 Learning the Details of XSL Stylesheets
Every XSL stylesheet needs to identify the XSL namespace (abbreviated as xslns) so that the parser knows what version of XSLT to use. Accordingly, an XSLT stylesheet statement defines a root element called <xsl:stylesheet> or <xsl:transform>. This might appear as: <xsl:stylesheet version="1.0" xmlns:xsl=" or: <xsl:transform version="1.0" xmlns:xsl="

11 Example: gamecatalog.xml
<?xml version="1.0" encoding="ISO "?> <catalog> <game> <title>Escape from Monkey Island</title> <manufacturer>Lucas Arts</manufacturer> <genre>Adventure</genre> <platform>Windows 95/98/ME</platform> <country>USA</country> <cost>9.99</cost> <year>2002</year> </game> . </catalog>

12 Creating An XSL Stylesheet: gamecatalog.xsl
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My Game Auctions</h2> <table border="1"> <tr bgcolor="#ffffcc"> <th align="left">Title</th> <th align="left">Manufacturer</th> </tr> <xsl:for-each select="catalog/game"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="manufacturer"/></td> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>

13 Adding an XSL Stylesheet Reference to the gamecatalog.xml File
<?xml version="1.0" encoding="ISO "?> <?xml-stylesheet type="text/xsl" href="gamecatalog.xsl"?> <catalog> <game> <title>Escape from Monkey Island</title> <manufacturer>Lucas Arts</manufacturer> <genre>Adventure</genre> <platform>Windows 95/98/ME</platform> <country>USA</country> <cost>9.99</cost> <year>2002</year> </game> . </catalog>

14 Viewing the gamecatalog.xml File

15 Filtering You can filter the output from an XML document by setting a criterion for the select attribute in the <xsl:for-each> element. For example, change: <xsl:for-each select="catalog/game"> to: <xsl:for-each select="catalog/game[manufacturer='Activision']]"> then the results listing would show only games that had been released from Activision.

16 Filtering (2) You can filter your data using several operators, not just the equal (=) sign. These include: * = (equal) *! = (not equal) * < (less than) * > (greater than)

17 Sorting To sort your output at the same time it’s generated, you add an <xsl:sort> element within the <xsl:for-each> element in your XSL stylesheet: <xsl:for-each select="catalog/game"> <xsl:sort select="manufacturer"/>

18 Adding Sorting Capabilities to gamecatalog.xsl
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My Game Auctions</h2> <table border="1"> <tr bgcolor="#ffffcc"> <th align="left">Title</th> <th align="left">Manufacturer</th> </tr> <xsl:for-each select="catalog/game"> <xsl:sort select="title"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="manufacturer"/></td> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>

19 Displaying the gamecatalog.xml File, Sorted by Title

20 Creating Conditional Statements
A conditional statement in an XSL stylesheet. tests your file’s content with a straightforward true-false condition, applying a template only if that specified condition is true. You can use conditional statements to limit the display of data. The format looks like the following: <xsl:if test="cost<'30'"> . </xsl:if>

21 Adding Conditional Testing to gamecatalog.xsl
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My Game Auctions</h2> <table border="1"> <tr bgcolor="#ffffcc"> <th align="left">Title</th> <th align="left">Manufacturer</th> </tr> <xsl:for-each select="catalog/game"> <xsl:if test="cost<'30'"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="manufacturer"/></td> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>

22 The Output of the gamecatalog
The Output of the gamecatalog.xml file, Showing Games That Cost Less Than $30

23 Adding a Multiple Conditional Statement to gamecatalog.xsl
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My Game Auctions</h2> <table border="1"> <tr bgcolor="#ffffcc"> <th align="left">Title</th> <th align="left">Manufacturer</th> </tr> <xsl:for-each select="catalog/game"> <tr> <xsl:choose> <xsl:when test="year>'2002'"> <td><xsl:value-of select="title"/><img src="new.gif"></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="title"/></td> </xsl:otherwise> </xsl:choose> <td><xsl:value-of select="manufacturer"/></td> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

24 Applying a Multiple Conditional Statement to Indicate Which Items Are the Most Recent

25 Applying Templates <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="essay"> <h2><xsl:apply-templates select="subhead"/></h2> </xsl:template> </xsl:stylesheet>

26 Debugging XSLT In the same way that a word processor’s spell-checker can ensure there are no obvious misspellings in your writings, validators and debuggers are essential tools for programmers in reducing or eliminating errors in their code. Often debuggers are built into suites of authoring tools, but you shouldn’t need something that complex to check your own errors. For example, Macromedia’s Dreamweaver is a sophisticated suite of tools for Web designers and developers.

27 Using the XSLT Debugging Feature in Altova’s XMLSpy Program

28 The Free XSLDebugger Program

29 The End


Download ppt "Applying eXtensible Style Sheets (XSL)"

Similar presentations


Ads by Google