XSLT for Data Manipulation By April Fleming What
XSLT for Data Manipulation By: April Fleming
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 Cold. Fusion point-of-view
What is XSLT • e. Xstensible Stylesheet Language: • • Transformations Transformation Language High-Level data manipulation language A language for transforming the structure of an XML document Primarily designed by the W 3 C for transforming one XML document into another
What is XSLT - cont’d • 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”
What is XSLT - cont’d • These “rules” have two parts üPattern • Matched against nodes in the source tree üTemplate • Can be instantiated to form part of the result tree
What is XSLT - cont’d • The result tree is separate from the source tree • The result tree can be in a completely different form from the source tree
Output Formats • <xsl: output> ü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
Output Format - cont’d • Three types of output üXML • <xsl: output method=”xml” /> • output is an XML document or XML document fragment • <? xml version="1. 0"? > appears in the resulting output
Output Formats - Cont’d üHTML • <xsl: output method=”html” version=” 4. 0” /> • Output is an HTML document üText • Allows output in many other text-based formats • Comma-seperated values • RTF • PDF • EDI • SQL • Javascript
Why would I use XSLT • XSLT is a supported w 3 c standard. • XSLT Provides the traditional language conventions that we are used to when processing data üLooping üConditional Logic üExpressions üVariables
Why would I use XSLT - cont’d • Dynamic • Efficient • Facilitate separation of Data and Presentation
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.
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
How I used XSLT and Why - cont’d • 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
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
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
XML Parser Comparison Chart
Simple “Hello World” example • To perform a transformation we need three things üXML Data üXSLT Stylesheet üXML Parser
XML Data - Save as hello. xml • <? xml version="1. 0" ? > • <output>Hello World</output>
XSLT Stylesheet – Save as hello. xsl <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match=”/”> <html> <head> <title> XSLT Example</title> </head> <body> <xsl: value-of select=”output” /> </body> </html> </xsl: template> </xsl: stylesheet>
Parsing – Cold. Fusion & MSXML Parser <!---Create two instances of the XML Parser, one for the XML and one for the XSL---> <cfobject type = "COM" action = "create" class = "MSXML 2. DOMDOCUMENT. 4. 0" name = "xml"> <cfobject type = "COM" action = "create" class = "MSXML 2. DOMDOCUMENT. 4. 0" name = "xsl">
<cfscript> //do not validate the xml. validate. On. Parse=false; xml. async = false; //load the xml file into the xml instance of the parser temp = xml. load("c: inetpubwwwroothello. xml"); </cfscript> <cfscript> xsl. async=false; //load the xsl file into the xsl instance of the parser xsl. load("c: inetpubwwwroothello. xsl"); output = xml. transform. Node(xsl); </cfscript> <!---output the transformed result----> <cfoutput>#output#</cfoutput> <!---set com objects to null---> <cfset xml = ""> <cfset xsl = "">
XSLT Stylesheet Structure · The XSLT namespace has the URI http: //www. w 3. org/1999/XSL/Transform. <xsl: stylesheetxmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform" version="1. 0"> • Remember that an XSLT stylesheet is a well-formed XML document, therefore you must always include the corresponding closing tag. • </xsl: stylesheet> • The tag <xsl: transform> is a synonym for <xsl: stylesheet> either one is acceptable
XSLT Stylesheet Structure - cont’d · 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 xsl: stylesheet element is called a toplevel element
Top Level Elements • • • <xsl: import href=". . . "/> <xsl: include href=". . . "/> <xsl: strip-space elements=". . . "/> <xsl: preserve-space elements=". . . "/> <xsl: output method=". . . "/> <xsl: key name=". . . " match=". . . " use=". . . "/> • <xsl: decimal-format name=". . . "/>
Top Level Elements - cont’d • <xsl: namespace-alias stylesheet-prefix=". . . " • • • result-prefix=". . . "/> <xsl: attribute-set name=". . . "></xsl: attributeset> <xsl: variable name=". . . ">. . . </xsl: variable> <xsl: param name=". . . ">. . . </xsl: param> <xsl: template match=". . . "></xsl: template> <xsl: template name=". . . "></xsl: template>
Templates • <xsl: template match=””> • “Match” is a pattern, the pattern is expressed using Xpath • Pattern describes which nodes in the source tree the template rule matches • <xsl: template match=”/” > ümatches the root node • <xsl: template match=”Title” > ümatches the title node
Templates - cont’d • <xsl: template match=”Chapter/Title”> ümatches the Title node that is a child to the Chapter node <chapter> <title>My Title</title> </chapter>
Templates - cont’d • <xsl: template match=”Chapter/@title”> ümatches the title attribute of the chapter node <chapter title=”my title” … </chapter>
Templates - cont’d • 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. <xsl: template match=”/”> …. instructions…. </xsl: template>
<xsl: apply-templates> • For each child of the current node üFind the matching template rule üInstantiate the template rule <xsl: template match=“/”> <xsl: apply-templates/> </xsl: template>
<xsl: apply-templates select=“”> • Allows you to control the order that template rules are applied <xsl: template match=“/”> <xsl: apply-templates select=“apples”/> <xsl: apply-templates select=“oranges”/> <xsl: apply-templates select=“banannas”/> </xsl: template>
<xsl: value-of> • Extract the required information from the node directly <xsl: template match=“/”> <html> <head>…</head> <body> <xsl: value-of select=“output”/> </body> </html> </xsl: template>
<xsl: for-each> - Looping XSLT: Cold. Fusion: <xsl: for-each select=“oranges”> … </xsl: for-each> <cfloop list=“#oranges#”> … </cfloop>
<xsl: if> - Conditional Logic XSLT: Cold. Fusion: <xsl: template match="name"> <xsl: value-of select=“. ”/> <xsl: if test="position()!=last()"> , </xsl: if> <cfset pos=0> <cfloop list=“names”Index=“name”> <cfset pos=increment. Value(pos)> #name# <cfif pos neq listlen(names)> , </cfif> </cfloop> </xsl: template> Result: Bob, Mary, John, Martha, Sue
<xsl: choose> - Conditional Logic XSLT: <xsl: template match="order"> <xsl: choose> <xsl: when test="total < 5"> (small) </xsl: when> <xsl: when test="total < 10"> (medium) </xsl: when> <xsl: otherwise> (large) </xsl: otherwise> </xsl: choose> </xsl: template> Cold. Fusion: <cfswitch expression=“order”> <cfcase value=“ 1, 2, 3, 4”> (small) </cfcase> <cfcase value=“ 5, 6, 7, 8, 9”> (medium) </cfcase> <cfdefaultcase> (large) </cfdefaultcase> </cfswitch>
<xsl: variable> - Variables XSLT: <xsl: variable name="n" select="2"/>. . . <xsl: value-of select="item[$n]"/> Cold. Fusion: <cfset n=“ 2”> #listgetat(list, n)# • Variables may be defined Globally or Locally • Local variables can be defined within a template body
Datatypes • Variables are not statically typed, rather they • • • take whatever type of value is assigned to them, just as Cold. Fusion does String Number Boolean Node-Set üA set of nodes in the source tree Tree
Expressions • Syntax of expressions is defined by the XPath Recommendation • Used as attribute values for many XSLT elements <xsl: value-of select=“($x + $y) *2”/>
Resources • XSLT – Programmer’s Reference by Michael Kay (Wrox)* • W 3 C* ü http: //www. w 3 c. org • Microsoft MSXML SDK Documentation* ü http: //msdn. microsoft. com/library/default. asp? url=/library/enus/xmlsdk/htm/sdk_intro_6 g 53. asp • XSL-List ü http: //www. mulberrytech. com/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.
- Slides: 44