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

16
1 XSLT Design Patterns

Transcript of 1 XSLT Design Patterns. 2 Stylesheet Design Patterns Ref: XSLT by Michael Kay ISBN:1-961005-06-7...

Page 1: 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.

1

XSLT Design Patterns

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

2

Stylesheet Design PatternsRef: 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

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

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.

Page 4: 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.

4

Example 1

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 }

Page 5: 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.

5

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>

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

6

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>

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

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.

Page 8: 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.

8

Example

• Example on slide 9

• The example shows BookSales.xml and BookSales.xsl that generates an HTML output showing book sales by month.

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

9

Example

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>

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

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.

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

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.

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

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>

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

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

13

BookCatalog.xsl

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

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

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>

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

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.

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

16

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>