Understanding Common XSLT Stylesheet Design Patterns
Discover the four main design patterns in XSLT stylesheets: Fill-in-Blank, Navigational, Rule-based, and Computational. Each pattern illustrates a unique approach to structuring XSLT for various output needs. Fill-in-Blank styles offer a simple structure resembling HTML with embedded control statements for dynamic content. Navigational stylesheets resemble procedural programming, facilitating complex data reporting tasks from XML documents. Dive deeper into examples and best practices for efficient XSLT design that enhances transform processes.
Understanding Common XSLT Stylesheet Design Patterns
E N D
Presentation Transcript
Stylesheet Design PatternsRef: XSLT by Michael KayISBN: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
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.
Catalog.xml <?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>1-5555-555-2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0-444-4444-4</ISBN> <Publisher>Kidder Publishing Co.</Publisher> </Book> </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 } Example 1
Example 2 - Office.xml <?xml version="1.0"?> <officepersonnel> <person> <name> Mary Jones </name> <title> CEO </title> <reports> <person> <name> Susan Mills </name> <title> Director </title> <reports> <person> <name> John Deen </name> <title> Engineer </title> </person> </reports> </person> <person> <name>David Smith </name> <title> Director </title> <reports> <person> <name> Joan Mist </name> <title> Engineer </title> </person> </reports> </person> </reports> </person> </officepersonnel>
Example2 - Office.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="ancestor::person[1]/name"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
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.
Example • Example on slide 9 • The example shows BookSales.xml and BookSales.xsl that generates an HTML output showing book sales by month.
BookSales.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="011231.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="2" books_sold="2000" /> <week number="3" books_sold="1000" /> <week number="4" books_sold="5000" /> </month> </data> </sales> BookSales.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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 select="format-number(sum(week/@books_sold), '###,###')"/> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet> Example
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.
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.
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> <product code="7777" category="tools"> <description>Bolt</description> <weight units="lbs">0.1</weight> <price>1.00</price> </product> <book> <title>Cosmos</title> <author >Sagan</author> </book> <book> <title>XML Made Easy</title> <author >Joe Bloggs</author> </book> </catalog>
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="catalog"> <xsl:element name="BookCatalog"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="book"> <xsl:element name="book"> <xsl:apply-templates/> </xsl:element> </xsl:template> <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:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="text()"> </xsl:template> </xsl:stylesheet> BookCatalog.xsl
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>
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.
Examplecalc.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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:template> </xsl:stylesheet>