XSLT for Data Manipulation - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT for Data Manipulation

Description:

Free download from Microsoft. Excellent Documentation. http://msdn.microsoft.com/xml/default.asp ... Microsoft MSXML SDK Documentation ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 45
Provided by: michael118
Category:

less

Transcript and Presenter's Notes

Title: XSLT for Data Manipulation


1
XSLT for Data Manipulation
  • By April Fleming

2
What We Will Cover
  • The What, Why, When, and How of XSLT
  • What tools you will need to get started
  • A sample Hello World application
  • Enough XSLT Constructs to get started, given from
    a ColdFusion point-of-view

3
What is XSLT
  • eXstensible Stylesheet Language Transformations
  • Transformation Language
  • High-Level data manipulation language
  • A language for transforming the structure of an
    XML document
  • Primarily designed by the W3C for transforming
    one XML document into another

4
What is XSLT - contd
  • The XSLT language is expressed as a well-formed
    XML document
  • The XSLT language belongs to the XSLT Namespace
  • An XSLT transformation describes rules for
    transforming a source tree into a result tree.
    This set of rules is called a stylesheet

5
What is XSLT - contd
  • These rules have two parts
  • Pattern
  • Matched against nodes in the source tree
  • Template
  • Can be instantiated to form part of the result
    tree

6
What is XSLT - contd
  • The result tree is separate from the source tree
  • The result tree can be in a completely different
    form from the source tree

7
Output Formats
  • ltxsloutputgt
  • Optional
  • Parser will default the output type if the tag is
    omitted.
  • Top-Level Element used to define the output type
    desired
  • Enables you to specify encoding for output

8
Output Format - contd
  • Three types of output
  • XML
  • ltxsloutput methodxml /gt
  • output is an XML document or XML document
    fragment
  • lt?xml version"1.0"?gt appears in the resulting
    output

9
Output Formats - Contd
  • HTML
  • ltxsloutput methodhtml version4.0 /gt
  • Output is an HTML document
  • Text
  • Allows output in many other text-based formats
  • Comma-seperated values
  • RTF
  • PDF
  • EDI
  • SQL
  • Javascript

10
Why would I use XSLT
  • XSLT is a supported w3c standard.
  • XSLT Provides the traditional language
    conventions that we are used to when processing
    data
  • Looping
  • Conditional Logic
  • Expressions
  • Variables

11
Why would I use XSLT - contd
  • Dynamic
  • Efficient
  • Facilitate separation of Data and Presentation

12
How I used XSLT and Why
  • Distributed Search Application
  • I needed to write a search engine that could
    function independent of the various search
    interfaces that we had.
  • I needed the engine to be completely independent
    of the search interfaces or the search result
    displays.

13
How I used XSLT and Why
  • I chose XML as the data format for I/O to the
    engine
  • Uniform structure of Data regardless of the
    source
  • Data could be easily parsed using XML parser

14
How I used XSLT and Why - contd
  • I chose XSLT to process the XML data going into
    and out of the search engine
  • With XSLT I could easily transform the data
    defintion output packets from the engine and
    create an html search form
  • I also found XSLT to be a great way to take the
    search results and transform them into the
    various display formats we offered to our users

15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
When would I use XSLT?
  • When you have XML data from a source (external or
    internal) with no other way to process that data
  • When the XSLT transformations are faster than the
    current way you have to process your XML data.
  • Example XSLT vs. CF processing
  • When you need more power and flexibility than
    your current XML processing mechanism provides
  • Example CF MX XML Processing Tags/Functions

19
What tools will I need to use XSLT?
  • XML Parser
  • MSXML
  • This is the Parser that I used
  • Free download from Microsoft
  • Excellent Documentation
  • http//msdn.microsoft.com/xml/default.asp
  • Other Parsers - see chart on next slide
  • Good understanding of XPath
  • Used for XSLT expressions
  • MSXML SDK - good Xpath documentation

20
  • XML Parser Comparison Chart

21
Simple Hello World example
  • To perform a transformation we need three
    things
  • XML Data
  • XSLT Stylesheet
  • XML Parser

22
(No Transcript)
23
  • XML Data - Save as hello.xml
  • lt?xml version"1.0" ?gt
  • ltoutputgtHello Worldlt/outputgt

24
  • XSLT Stylesheet Save as hello.xsl
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsltemplate match/gt
  • lthtmlgt
  • ltheadgt
  • lttitlegt XSLT Examplelt/titlegt
  • lt/headgt
  • ltbodygt
  • ltxslvalue-of selectoutput /gt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

25
Parsing ColdFusion MSXML Parserlt!---Create
two instances of the XML Parser, one for the XML
and one for the XSL---gtltcfobject type "COM"
action "create" class "MSXML2.DOMDOCUMENT.4.
0" name "xml"gt ltcfobject type "COM"
action "create" class "MSXML2.DOMDOCUMENT.4.
0" name "xsl"gt
26
ltcfscriptgt //do not validate the
xml xml.validateOnParsefalse xml.async
false //load the xml file into the xml instance
of the parser temp xml.load("c\inetpub\wwwroot
\hello.xml")lt/cfscriptgtltcfscriptgt xsl.asyncfa
lse //load the xsl file into the xsl instance
of the parser xsl.load("c\inetpub\wwwroot\hello.
xsl") output xml.transformNode(xsl)lt/cfscr
iptgtlt!---output the transformed
result----gtltcfoutputgtoutputlt/cfoutputgtlt!---se
t com objects to null---gtltcfset xml ""gtltcfset
xsl ""gt
27
  • XSLT Stylesheet Structure
  • The XSLT namespace has the URI
    http//www.w3.org/1999/XSL/Transform.
  • ltxslstylesheetxmlnsxsl"http//www.w3.org/1999/X
    SL/Transform" version"1.0"gt
  • Remember that an XSLT stylesheet is a well-formed
    XML document, therefore you must always include
    the corresponding closing tag.
  • lt/xslstylesheetgt
  • The tag ltxsltransformgt is a synonym for
    ltxslstylesheetgt either one is acceptable

28
XSLT Stylesheet Structure - contd
  • Can use any prefix, provided that there is a
    namespace declaration that binds the prefix to
    the URI of the XSLT namespace
  • An element occurring as a child of an
    xslstylesheet element is called a top-level
    element

29
Top Level Elements
  • ltxslimport href"..."/gt
  • ltxslinclude href"..."/gt
  • ltxslstrip-space elements"..."/gt
  • ltxslpreserve-space elements"..."/gt
  • ltxsloutput method"..."/gt
  • ltxslkey name"..." match"..." use"..."/gt
  • ltxsldecimal-format name"..."/gt

30
Top Level Elements - contd
  • ltxslnamespace-alias stylesheet-prefix"..."
    result-prefix"..."/gt
  • ltxslattribute-set name"..."gtlt/xslattribute-setgt
  • ltxslvariable name"..."gt...lt/xslvariablegt
  • ltxslparam name"..."gt...lt/xslparamgt
  • ltxsltemplate match"..."gtlt/xsltemplategt
  • ltxsltemplate name"..."gtlt/xsltemplategt

31
Templates
  • ltxsltemplate matchgt
  • Match is a pattern, the pattern is expressed
    using Xpath
  • Pattern describes which nodes in the source tree
    the template rule matches
  • ltxsltemplate match/ gt
  • matches the root node
  • ltxsltemplate matchTitle gt
  • matches the title node

32
Templates - contd
  • ltxsltemplate matchChapter/Titlegt
  • matches the Title node that is a child to the
    Chapter node
  • ltchaptergt
  • lttitlegtMy Titlelt/titlegt
  • lt/chaptergt

33
Templates - contd
  • ltxsltemplate matchChapter/_at_titlegt
  • matches the title attribute of the chapter node
  • ltchapter titlemy title
  • lt/chaptergt

34
Templates - contd
  • When the template is instantiated (a match
    condition is met), the instructions within the
    template tags are executed and the resulting data
    is copied to the result tree.
  • ltxsltemplate match/gt
  • .instructions.
  • lt/xsltemplategt

35
ltxslapply-templatesgt
  • For each child of the current node
  • Find the matching template rule
  • Instantiate the template rule
  • ltxsltemplate match/gt
  • ltxslapply-templates/gt
  • lt/xsltemplategt

36
ltxslapply-templates selectgt
  • Allows you to control the order that template
    rules are applied
  • ltxsltemplate match/gt
  • ltxslapply-templates selectapples/gt
  • ltxslapply-templates selectoranges/gt
  • ltxslapply-templates selectbanannas/gt
  • lt/xsltemplategt

37
ltxslvalue-ofgt
  • Extract the required information from the node
    directly
  • ltxsltemplate match/gt
  • lthtmlgt
  • ltheadgtlt/headgt
  • ltbodygt
  • ltxslvalue-of selectoutput/gt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt

38
ltxslfor-eachgt - Looping
  • XSLT
  • ltxslfor-each selectorangesgt
  • lt/xslfor-eachgt
  • ColdFusion
  • ltcfloop listorangesgt
  • lt/cfloopgt

39
ltxslifgt - Conditional Logic
  • XSLT
  • ltxsltemplate match"name"gt
  • ltxslvalue-of select./gt
  • ltxslif test"position()!last()"gt
  • ,
  • lt/xslifgt
  • lt/xsltemplategt
  • ColdFusion
  • ltcfset pos0gt
  • ltcfloop listnamesIndexnamegt
  • ltcfset posincrementValue(pos)gt
  • name
  • ltcfif pos neq listlen(names)gt
  • ,
  • lt/cfifgt
  • lt/cfloopgt

Result Bob, Mary, John, Martha, Sue
40
ltxslchoosegt - Conditional Logic
  • XSLT
  • ltxsltemplate match"order"gt
  • ltxslchoosegt
  • ltxslwhen test"total lt 5"gt
  • (small)
  • lt/xslwhengt
  • ltxslwhen test"total lt 10"gt (medium)
  • lt/xslwhengt
  • ltxslotherwisegt
  • (large)
  • lt/xslotherwisegt
  • lt/xslchoosegt
  • lt/xsltemplategt
  • ColdFusion
  • ltcfswitch expressionordergt
  • ltcfcase value1,2,3,4gt
  • (small)
  • lt/cfcasegt
  • ltcfcase value5,6,7,8,9gt
  • (medium)
  • lt/cfcasegt
  • ltcfdefaultcasegt
  • (large)
  • lt/cfdefaultcasegt
  • lt/cfswitchgt

41
ltxslvariablegt - Variables
  • XSLT
  • ltxslvariable name"n" select"2"/gt ...ltxslv
    alue-of select"itemn"/gt
  • ColdFusion
  • ltcfset n2gt
  • listgetat(list,n)
  • Variables may be defined Globally or Locally
  • Local variables can be defined within a template
    body

42
Datatypes
  • Variables are not statically typed, rather they
    take whatever type of value is assigned to them,
    just as ColdFusion does
  • String
  • Number
  • Boolean
  • Node-Set
  • A set of nodes in the source tree
  • Tree

43
Expressions
  • Syntax of expressions is defined by the XPath
    Recommendation
  • Used as attribute values for many XSLT elements
  • ltxslvalue-of select(x y) 2/gt

44
Resources
  • XSLT Programmers Reference by Michael Kay
    (Wrox)
  • W3C
  • http//www.w3c.org
  • Microsoft MSXML SDK Documentation
  • http//msdn.microsoft.com/library/default.asp?url
    /library/en-us/xmlsdk/htm/sdk_intro_6g53.asp
  • XSL-List
  • http//www.mulberrytech.com/xsl/xsl-list
  • I have relied heavily on these resources not
    only for the application development I have done,
    but also in preparation for this presentation.
    Many of the information and examples come from
    one of these sources.
Write a Comment
User Comments (0)
About PowerShow.com