XSLT Design Patterns 1 Stylesheet Design Patterns Ref

  • Slides: 16
Download presentation
XSLT Design Patterns 1

XSLT Design Patterns 1

Stylesheet Design Patterns Ref: XSLT by Michael Kay ISBN: 1 -961005 -06 -7 •

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 2

Stylesheet Design Patterns • Fill-in-Blank Stylesheets: • The template looks largely like a standard

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

Example 1 Catalog. xml Booklists. css <? xml version="1. 0"? > <? xml-stylesheet type="text/css"

Example 1 Catalog. xml Booklists. css <? xml version="1. 0"? > <? xml-stylesheet type="text/css" href="booklists. css" ? > Book. Catalog {display: block; border: solid blue} Book {display: block} <Book. Catalog> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN>1 -5555 -2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0 -4444 -4</ISBN> <Publisher>Kidder Publishing Co. </Publisher> </Book. Catalog> Title { display: block; font-family: Arial; color: Red } Author {display: block } Date, ISBN { display: none } Publisher { display: block; font-style: italic } 4

Example 2 - Office. xml <? xml version="1. 0"? > <officepersonnel> <person> <name> Mary

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> <name>David Smith </name> <title> Director </title> <reports> <person> <name> Joan Mist </name> <title> Engineer </title> </person> </reports> </person> </officepersonnel> 5

Example 2 - Office. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http:

Example 2 - Office. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. 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> 6

Stylesheet Design Patterns • Navigational Stylesheets: • Is essentially output-oriented. • Named templates can

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

Example • Example on slide 9 • The example shows Book. Sales. xml and

Example • Example on slide 9 • The example shows Book. Sales. xml and Book. Sales. xsl that generates an HTML output showing book sales by month. 8

Example Book. Sales. xml <? xml version="1. 0"? > <? xml-stylesheet type="text/xsl" href="011231. xsl"?

Example Book. Sales. 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> <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" /> Book. Sales. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XS L/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="formatnumber(sum(week/@books_sold), '###, ###')"/> </xsl: for-each> </BODY> </HTML> </xsl: template> </xsl: stylesheet> 9

Stylesheet Design Patterns • Rule-based Stylesheets: • Consists primarily of rules describing how different

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

Example • Example shown on slides 12, 13 and 14. • In this example,

Example • Example shown on slides 12, 13 and 14. • In this example, the stylesheet, Book. Catalog. xsl generates a file called Book. Catalog. xml (slide 14) from the input file, Product. Catalog. xml (on slide 12), using template rules. 11

Example – Product. Catalog. xml <? xml version="1. 0"? > <catalog> <product code="1234" category="tools">

Example – Product. Catalog. 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> <title>XML Made Easy</title> <author >Joe Bloggs</author> </book> </catalog> 12

Book. Catalog. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w

Book. Catalog. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. 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="Book. Catalog"> <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> 13

Book. Catalog. xml <? xml version="1. 0" encoding="utf-8"? > <Book. Catalog><book><title>Cosmos</title><author>Sagan</author></book><b ook><title>XML Made Easy</title><author>Joe

Book. Catalog. xml <? xml version="1. 0" encoding="utf-8"? > <Book. Catalog><book><title>Cosmos</title><author>Sagan</author></book><b ook><title>XML Made Easy</title><author>Joe Bloggs</author></book> </Book. Catalog> 14

Stylesheet Design Patterns • Computational Stylesheets: • Are the most complex of the four

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

Example calc. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w

Example calc. xsl <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. 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="Num. Div 2"> <xsl: with-param name="N" select="16"/> </xsl: call-template> </xsl: variable> <xsl: value-of select="$result"/> </body> </html. L> </xsl: template> <xsl: template name="Num. Div 2"> <xsl: param name="N"/> <xsl: value-of select="$N div 2"/> </xsl: template> 16