Download presentation
Presentation is loading. Please wait.
1
XSLT Design Patterns
2
Stylesheet Design Patterns Ref: XSLT by Michael Kay ISBN:1-961005-06-7
Four common design patterns for XSLT Stylesheets The majority of stylesheets fall into four major categories: Fill-in-Blank Stylesheets Navigational stylesheets Rule-based stylesheets Computational stylesheets
3
Stylesheet Design Patterns
Fill-in-Blank Stylesheets: The template looks largely like a standard HTML file, sprinkled with a few extra control statements. Extra tags are used to retrieve variable data and insert it at a particular point in the HTML data page. The stylesheet has the same structure as the desired output. Fixed content is included directly in the stylesheet as text. Repeated sections (like rows in a table) can be enclosed by <xsl:for-each> tags.
4
Example 1 Catalog.xml Booklists.css <?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="booklists.css" ?> <BookCatalog> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN> </ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN> </ISBN> <Publisher>Kidder Publishing Co.</Publisher> </BookCatalog> Booklists.css BookCatalog {display:block;border:solid blue} Book {display:block} Title { display:block; font-family:Arial;color:Red } Author {display:block } Date,ISBN { display:none } Publisher { display: block; font-style:italic }
5
Example 2 - Office.xml <?xml version="1.0"?>
<officepersonnel> <person> <name> Mary Jones </name> <title> CEO </title> <reports> <name> Susan Mills </name> <title> Director </title> <name> John Deen </name> <title> Engineer </title> </person> </reports> <name>David Smith </name> <name> Joan Mist </name> </officepersonnel>
6
Example2 - Office.xsl <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:template match="/"> <html><head><title> Management </title></head> <body> <table border="2" cellpadding="5"> <tr> <th>Name></th> <th>Title</th> <th> Reports To </th> </tr> <xsl:for-each select="//person"> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="ancestor::person[1]/name"/></td> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
7
Stylesheet Design Patterns
Navigational Stylesheets: Is essentially output-oriented. Named templates can be used as subroutines to perform some of the commonly-needed tasks. Looks more like a conventional procedural program with variables, conditional statements, loops and subroutine calls. Often used to produce reports on data-oriented XML documents, where the structure of the source document is regular and predictable.
8
Example Example on slide 9
The example shows BookSales.xml and BookSales.xsl that generates an HTML output showing book sales by month.
9
Example BookSales.xsl <?xml version="1.0"?>
BookSales.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href=" xsl"?> <sales> <summary> <heading>Book Store</heading> <subhead>Sales Report</subhead> <description>Sales Report for two months</description> </summary> <data> <month><name>January 2002</name> <week number="1" books_sold="1000" /> <week number="2" books_sold="2000" /> <week number="3" books_sold="3000" /> <week number="4" books_sold="4000" /> </month> <month> <name> April 2002</name> <week number="1" books_sold="700" /> <week number="3" books_sold="1000" /> <week number="4" books_sold="5000" /> </data> </sales> BookSales.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <HTML> <BODY> <xsl:variable name="Space" select="' '"/> <xsl:for-each select="//data/month"> <br/><xsl:value-of select="name"/> <xsl:value-of select="$Space"/> <xsl:value-of '###,###')"/> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
10
Stylesheet Design Patterns
Rule-based Stylesheets: Consists primarily of rules describing how different features of the source document should be processed. The stylesheet is not structured according to the desired output layout. Makes minimal assumptions about the structure of the source or the result documents. Are most useful when processing source documents whose structure is flexible or unpredictable, or which may change a lot in the future.
11
Example Example shown on slides 12,13 and 14.
In this example, the stylesheet, BookCatalog.xsl generates a file called BookCatalog.xml (slide 14) from the input file, ProductCatalog.xml (on slide 12), using template rules.
12
Example – ProductCatalog.xml
<?xml version="1.0"?> <catalog> <product code="1234" category="tools"> <description>Hammer</description> <weight units="lbs">0.5</weight> <price>12.00</price> </product> <product code="0000" category="tools"> <description>Nut</description> <weight units="lbs">0.1</weight> <price>2.00</price> <product code="7777" category="tools"> <description>Bolt</description> <price>1.00</price> <book> <title>Cosmos</title> <author >Sagan</author> </book> <title>XML Made Easy</title> <author >Joe Bloggs</author> </catalog>
13
BookCatalog.xsl <xsl:template match="title">
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="catalog"> <xsl:element name="BookCatalog"> </xsl:element> <xsl:template match="book"> <xsl:element name="book"> <xsl:template match="title"> <xsl:element name="title"> <xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="author"> <xsl:element name="author"> <xsl:template match="text()"> </xsl:stylesheet>
14
BookCatalog.xml <?xml version="1.0" encoding="utf-8"?>
<BookCatalog><book><title>Cosmos</title><author>Sagan</author></book><book><title>XML Made Easy</title><author>Joe Bloggs</author></book> </BookCatalog>
15
Stylesheet Design Patterns
Computational Stylesheets: Are the most complex of the four design patterns. They are used when there is a need to generate nodes in the result tree that do not correspond directly to nodes in the source tree. Examples: A comma-separated list of items in the source are to be displayed as bulleted list of output. Using complex aggregation of data Use functional programming concepts and recursion to accomplish the tasks.
16
Example calc.xsl <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Calculation</title></head> <body> 16 / 2 = <xsl:variable name="result"> <xsl:call-template name="NumDiv2"> <xsl:with-param name="N" select="16"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$result"/> </body> </htmlL> </xsl:template> <xsl:template name="NumDiv2"> <xsl:param name="N"/> <xsl:value-of select="$N div 2"/> </xsl:stylesheet>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.