XSLT and ColdFusion - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

XSLT and ColdFusion

Description:

Web application developer for i2 Technologies ... color Fawn /color weight 325 pounds /weight feedingTime 14:00 /feedingTime /animal ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 26
Provided by: mattwo
Category:
Tags: coldfusion | xslt

less

Transcript and Presenter's Notes

Title: XSLT and ColdFusion


1
XSLT and ColdFusion
  • Whipping Your XML Data Into Shape

Matt Woodward Dallas/Fort Worth CFUG September 7,
2004
2
Who Am I?
  • Web application developer for i2 Technologies
  • Consulting/contract work through my company Sixth
    Floor Software
  • Member of Team Macromedia for ColdFusion
  • Working with CF since 1996
  • Also develop in PHP and Java

3
What Ill Be Covering
  • Extensible Stylesheet Language Transformations
    (XSLT) Introduction and basic concepts
  • Manipulating XML data using XSLT
  • Support for and use of XSLT in ColdFusion MX
  • Based on article appearing in August 2004 edition
    of ColdFusion Developers Journal

4
XML Its Everywhere
  • Like it or not, XML is everywhere these days (web
    services, news service and blog datafeeds,
    application configuration files)
  • If you havent had to deal with XML yet, dont
    worry you will
  • XML is plain text, but it is infinitely more
    usable than comma or tab-delimited text data

5
Why Use XML?
  • Data exchange, particularly when security is a
    concern
  • Because I said so XML may be the only way
    youll be able to receive or send data (Wal-Mart,
    HIPPA, FERPA)
  • May make better sense than traditional RDBMS in
    some cases

6
XML A Brief Example
lt?xml version"1.0" encoding"utf-8"?gt ltanimalsgt
ltanimal species"Feline" subspecies"Lion"gt
ltnamegtRoarylt/namegt
ltsexgtFemalelt/sexgt ltcolorgtFawnlt/colorgt
ltweightgt325 poundslt/weightgt
ltfeedingTimegt1400lt/feedingTimegt lt/animalgt
ltanimal species"Reptile" subspecies"Tortoise"gt
ltnamegtShellylt/namegt lttypegtBox
Turtlelt/typegt ltsexgtFemalelt/sexgt
ltcolorgtGreenlt/colorgt ltweightgt20
poundslt/weightgt ltfeedingTimegt1500lt/feedin
gTimegt lt/animalgt lt/animalsgt
7
Transforming XML
  • XML data is of limited use by itself
  • Web services you may find one that gives you the
    data you want, but not in the right format
  • Data exchange different companies require
    different formats (schemas) of XML data
  • Data reuse presenting data in different forms to
    different users or devices

8
Whats XSLT?
  • Extensible Stylesheet Language Transformations
    (XSLT) is designed to transform XML data from one
    form into another
  • Uses stylesheets to define how XML data is to
    be transformed
  • XSLT is itself a flavor of XML, meaning XSLT
    stylesheets meet all the requirements of XML
    (well-formed, etc.)
  • XSLT is a language, so it has many familiar
    language constructs (conditionals, loops)

9
Basic XSLT Concepts
  • XSLTs core purpose modifying XML documents
    based on pattern matching
  • Pattern matching is defined in XSLT stylesheets
    using XPath syntax
  • XSLT stylesheets are typically just a set of
    transformation rules
  • XSLT is processed by an XSLT processor (in CFMXs
    case this is Apache Xalan)
  • Similar to regular expressions with plain text,
    only far more flexible, elegant, and powerful

10
How XSLT is Processed
Source http//www.objectsbydesign.com/projects/xm
i_to_html.html
11
The Sample Scenario
  • Youre the web developer for your local zoo
  • You need to display a list of animals as HTML
  • The catch the only way the DBA will give you the
    data is as XML
  • What do you do???

12
One Option Be Lazy
  • Output the XML directly to the browser

13
A Better Option XSLT
  • Use XSLT to transform the XML data into HTML
  • Many modern browsers support in-browser XML/XSLT
    transformations, but compatibility may vary even
    if the functionality is available (IE 5 is
    terrible, IE 6 is slightly better, Netscape 6 is
    OK, and Netscape 7 fully supports the W3C specs)
  • Better to do the transformation on the server
    where you have more control

14
Anatomy of a Stylesheet
  • XSLT is based on pattern matching
  • Our task create a stylesheet and give it
    instructions to execute when it finds matching
    patterns in the XML data
  • For now, well present the data in the order its
    received (well do some other cool stuff later)

15
Our First Stylesheet
  • animalsHtml.xsl
  • ltxslstylesheetgt - required declaration
  • ltxsloutputgt - tells processor how to output data
    (available options vary by processor, but W3C
    standard options are xml, html, and text)
  • ltxsltemplate match/animalsgt - tells XSLT
    processor to start at the top of the XML and find
    the ltanimalsgt element
  • XSLT instructions are defined using XPath, which
    according to the W3C is a language for
    addressing parts of an XML document

16
Retrieving the Details
  • ltxslfor-each selectanimalgt - looping
    instruction instructions are executed for each
    ltanimalgt element
  • could have used another ltxslapply-templatesgt
    instruction here, but this isnt compatible with
    what were doing to be doing momentarily
  • ltxslvalue-ofgt - retrieves particular pieces of
    data _at_ used to indicate an attribute as opposed
    to an element

17
Functional Comparison
CFML query output
ltcfoutput querygetAnimalsgt lttrgt
lttdgtspecieslt/tdgt lttdgtsubspecieslt/tdgt
lttdgtnamelt/tdgt lt!--- etc. ---gt
lt/trgt lt/cfoutputgt
XSLT for-each
ltxslfor-each selectanimalgt lttrgt
lttdgtltxslvalue-of select_at_species /gtlt/tdgt
lttdgtltxslvalue-of select_at_subspecies /gtlt/tdgt
lttdgtltxslvalue-of selectname /gtlt/tdgt
lt!-- etc. --gt lt/trgt lt/xslfor-eachgt
18
Now for the easy part ...
  • Use CF to apply the XSLT stylesheet to the XML
    data and output the results in three easy steps

1. Read in the XML data ltcffile actionread
fileExpandPath(.)/animals.xml
variableanimalsXml /gt 2. Read in the XSLT
stylesheet ltcffile actionread
fileExpandPath(.)/animalsHtml.xsl
variableanimalsXsl /gt 3. Use XmlTransform()
to transform the data ltcfoutputgtXmlTransform(anim
alsXml, animalsXsl)lt/cfoutputgt
19
A More Sophisticated Transformation
  • Were outputting XML as HTML, but we can make
    this data more useful
  • Next step create a feline-only listing, ordered
    by feeding time

20
Stylesheet 2
  • ltxslsort selectfeedingTime /gt - tells the
    XSLT processor to perform an ascending sort on
    the elements within the for-each loop based on
    the ltfeedingTimegt element
  • ltxslif test_at_speciesFelinegt - if species
    Feline, output otherwise ignore
  • no ltxslelsegt command, but ltxslchoosegt,
    ltxslwhengt and ltxslotherwisegt can be used to
    create a switch

21
Things to Know ...
  • XSLT is based on the design of functional
    languages such as Lisp, Scheme, and Haskell
    biggest point here is that variables are
    immutable
  • Loops in XSLT arent really loops, its
    actually either iteration (do something for each
    instance of something) or recursion (function
    calls itself until a condition is met)

22
Uses of XSLT
  • Presenting the same XML data different ways to
    different users or clients (e.g. PC web browser,
    WAP/wireless device, etc.)
  • Data exchange (can manipulate data from one
    schema to another, system compatibility isnt an
    issue)
  • Great way to keep data presented on the web 100
    separate from presentation
  • Great way to reuse data (same data can be used
    for online display, sent to printing devices,
    etc.)

23
XSLT Is/Is Not ...
  • XSLT IS a powerful companion to XML, allowing for
    easy searching, manipulation, and transformation
    of XML data. If youre using XML and youre not
    using XSLT, youre probably doing things the hard
    way!
  • XSLT is NOT a replacement for CSS--these two
    technologies were designed to serve two
    completely different purposes

24
Resources
  • Official W3C spec http//www.w3.org/TR/xslt
  • xml.com Introduction to XSLT http//www.xml.com/p
    ub/a/2000/08/holman/
  • Online XSLT Reference/API http//www.zvon.org/xxl
    /XSLTreference/Output/
  • Basic Tutorial http//cozan.com/books/books/xml/x
    slt/ch01_01.htm
  • XSLT by Doug Tidwell (OReilly)
  • XSLT Cookbook by Sal Mangano (OReilly)
  • XSLT 2.0 Programmers Reference by Michael Kay
    (Wrox)

25
QA
Thanks!
Write a Comment
User Comments (0)
About PowerShow.com