XSLT Design Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT Design Patterns

Description:

The template looks largely like a standard HTML file, sprinkled with a few extra ... Publisher Kidder Publishing Co. /Publisher /Book /BookCatalog Booklists.css ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 17
Provided by: ranimikk
Learn more at: https://www.cse.scu.edu
Category:
Tags: xslt | design | kidder | patterns

less

Transcript and Presenter's Notes

Title: XSLT Design Patterns


1
XSLT Design Patterns
2
Stylesheet Design PatternsRef XSLT by Michael
KayISBN1-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 ltxslfor-eachgt tags.

4
Example 1
  • Catalog.xml
  • lt?xml version"1.0"?gt
  • lt?xml-stylesheet type"text/css"
    href"booklists.css" ?gt
  • ltBookCataloggt
  • ltBookgt
  • ltTitlegtBillions Of Starslt/Titlegt
  • ltAuthorgtSusan Boggslt/Authorgt
  • ltDategt1983lt/Dategt
  • ltISBNgt1-5555-555-2lt/ISBNgt
  • ltPublishergtAnderson-Wellslt/Publish
    ergt
  • lt/Bookgt
  • ltBookgt
  • ltTitlegtAdventures Of Freddie the
    Froglt/Titlegt
  • ltAuthorgtJohn Smithlt/Authorgt
  • ltDategt1977lt/Dategt
  • ltISBNgt0-444-4444-4lt/ISBNgt
  • ltPublishergtKidder Publishing
    Co.lt/Publishergt
  • Booklists.css
  • BookCatalog displayblockbordersolid blue
  • Book displayblock
  • Title displayblock font-familyArialcolorRed
  • Author displayblock
  • Date,ISBN displaynone
  • Publisher display block font-styleitalic

5
Example 2 - Office.xml
  • lt?xml version"1.0"?gt
  • ltofficepersonnelgt
  • ltpersongt
  • ltnamegt Mary Jones lt/namegt
  • lttitlegt CEO lt/titlegt
  • ltreportsgt
  • ltpersongt
  • ltnamegt Susan Mills lt/namegt
  • lttitlegt Director lt/titlegt
  • ltreportsgt
  • ltpersongt
  • ltnamegt John Deen lt/namegt
  • lttitlegt Engineer lt/titlegt
  • lt/persongt
  • lt/reportsgt
  • lt/persongt
  • ltpersongt
  • ltnamegtDavid Smith lt/namegt
  • lttitlegt Director lt/titlegt

6
Example2 - Office.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform"
  • version"1.0"gt
  • ltxsltemplate match"/"gt
  • lthtmlgtltheadgtlttitlegt Management lt/titlegtlt/headgt
  • ltbodygt
  • lttable border"2" cellpadding"5"gt
  • lttrgt
  • ltthgtNamegtlt/thgt ltthgtTitlelt/thgt ltthgt Reports
    To lt/thgt
  • lt/trgt
  • ltxslfor-each select"//person"gt
  • lttrgt
  • lttdgtltxslvalue-of select"name"/gtlt/tdgt
  • lttdgtltxslvalue-of select"title"/gtlt/tdgt
  • lttdgtltxslvalue-of select"ancestorperson1/n
    ame"/gtlt/tdgt
  • lt/trgt
  • lt/xslfor-eachgt
  • lt/tablegt
  • lt/bodygt

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.xml
  • lt?xml version"1.0"?gt
  • lt?xml-stylesheet type"text/xsl"
    href"011231.xsl"?gt
  • ltsalesgt
  • ltsummarygt
  • ltheadinggtBook Storelt/headinggt
  • ltsubheadgtSales Reportlt/subheadgt
  • ltdescriptiongtSales Report for two
    monthslt/descriptiongt lt/summarygt
  • ltdatagt
  • ltmonthgtltnamegtJanuary 2002lt/namegt ltweek number"1"
    books_sold"1000" /gt
  • ltweek number"2" books_sold"2000" /gt
  • ltweek number"3" books_sold"3000" /gt
  • ltweek number"4" books_sold"4000" /gt
  • lt/monthgt
  • ltmonthgt
  • ltnamegt April 2002lt/namegt
  • ltweek number"1" books_sold"700" /gt
  • ltweek number"2" books_sold"2000" /gt
  • ltweek number"3" books_sold"1000" /gt
  • BookSales.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsloutput method"html"/gt
  • ltxsltemplate match"/"gt
  • ltHTMLgt
  • ltBODYgt
  • ltxslvariable name"Space" select"' '"/gt
  • ltxslfor-each select"//data/month"gt
  • ltbr/gtltxslvalue-of select"name"/gt
  • ltxslvalue-of select"Space"/gt
  • ltxslvalue-of select"format-number(sum(week/_at_boo
    ks_sold), ',')"/gt
  • lt/xslfor-eachgt
  • lt/BODYgt
  • lt/HTMLgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

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
  • lt?xml version"1.0"?gt
  • ltcataloggt
  • ltproduct code"1234" category"tools"gt
  • ltdescriptiongtHammerlt/descriptiongt
  • ltweight units"lbs"gt0.5lt/weightgt
  • ltpricegt12.00lt/pricegt
  • lt/productgt
  • ltproduct code"0000" category"tools"gt
  • ltdescriptiongtNutlt/descriptiongt
  • ltweight units"lbs"gt0.1lt/weightgt
  • ltpricegt2.00lt/pricegt
  • lt/productgt
  • ltproduct code"7777" category"tools"gt
  • ltdescriptiongtBoltlt/descriptiongt
  • ltweight units"lbs"gt0.1lt/weightgt
  • ltpricegt1.00lt/pricegt
  • lt/productgt
  • ltbookgt
  • lttitlegtCosmoslt/titlegt

13
BookCatalog.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform"
  • version"1.0"gt
  • ltxsloutput method"xml"/gt
  • ltxsltemplate match"/"gt
  • ltxslapply-templates/gt
  • lt/xsltemplategt
  • ltxsltemplate match"catalog"gt
  • ltxslelement name"BookCatalog"gt
  • ltxslapply-templates/gt
  • lt/xslelementgt
  • lt/xsltemplategt
  • ltxsltemplate match"book"gt
  • ltxslelement name"book"gt
  • ltxslapply-templates/gt
  • lt/xslelementgt
  • lt/xsltemplategt
  • ltxsltemplate match"title"gt
  • ltxslelement name"title"gt
  • ltxslvalue-of select"."/gt
  • lt/xslelementgt
  • lt/xsltemplategt
  • ltxsltemplate match"author"gt
  • ltxslelement name"author"gt
  • ltxslvalue-of select"."/gt
  • lt/xslelementgt
  • lt/xsltemplategt
  • ltxsltemplate match"text()"gt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

14
BookCatalog.xml
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltBookCataloggtltbookgtlttitlegtCosmoslt/titlegtltauthorgtSa
    ganlt/authorgtlt/bookgtltbookgtlttitlegtXML Made
    Easylt/titlegtltauthorgtJoe Bloggslt/authorgtlt/bookgt
  • lt/BookCataloggt

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
Examplecalc.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform"
  • version"1.0"gt
  • ltxsloutput method"html"/gt
  • ltxsltemplate match"/"gt
  • lthtmlgtltheadgtlttitlegtCalculationlt/titlegtlt/he
    adgt
  • ltbodygt
  • 16 / 2
  • ltxslvariable name"result"gt
  • ltxslcall-template
    name"NumDiv2"gt
  • ltxslwith-param name"N"
    select"16"/gt
  • lt/xslcall-templategt
  • lt/xslvariablegt
  • ltxslvalue-of select"result"/gt
  • lt/bodygt
  • lt/htmlLgt
  • lt/xsltemplategt
  • ltxsltemplate name"NumDiv2"gt
Write a Comment
User Comments (0)
About PowerShow.com