1 / 44

XSLT for Data Manipulation

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 ColdFusion point-of-view. What is XSLT.

mulan
Download Presentation

XSLT for Data Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

  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 - cont’d • 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 - 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

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

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

  17. XML Parser Comparison Chart

  18. Simple “Hello World” example • To perform a transformation we need three things • XML Data • XSLT Stylesheet • XML Parser

  19. XML Data - Save as hello.xml • <?xml version="1.0" ?> • <output>Hello World</output>

  20. XSLT Stylesheet – Save as hello.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.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>

  21. Parsing – ColdFusion & MSXML Parser<!---Create two instances of the XML Parser, one for the XML and one for the XSL---><cfobject type = "COM" action = "create" class = "MSXML2.DOMDOCUMENT.4.0" name = "xml"> <cfobject type = "COM" action = "create" class = "MSXML2.DOMDOCUMENT.4.0" name = "xsl">

  22. <cfscript> //do not validate the xml xml.validateOnParse=false; xml.async = false; //load the xml file into the xml instance of the parser temp = xml.load("c:\inetpub\wwwroot\hello.xml");</cfscript><cfscript> xsl.async=false; //load the xsl file into the xsl instance of the parser xsl.load("c:\inetpub\wwwroot\hello.xsl"); output = xml.transformNode(xsl);</cfscript><!---output the transformed result----><cfoutput>#output#</cfoutput><!---set com objects to null---><cfset xml = ""><cfset xsl = "">

  23. XSLT Stylesheet Structure · The XSLT namespace has the URI http://www.w3.org/1999/XSL/Transform. <xsl:stylesheetxmlns:xsl="http://www.w3.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 synonymfor <xsl:stylesheet> either one is acceptable

  24. 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 top-level element

  25. 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="..."/>

  26. Top Level Elements - cont’d • <xsl:namespace-alias stylesheet-prefix="..." result-prefix="..."/> • <xsl:attribute-set name="..."></xsl:attribute-set> • <xsl:variable name="...">...</xsl:variable> • <xsl:param name="...">...</xsl:param> • <xsl:template match="..."></xsl:template> • <xsl:template name="..."></xsl:template>

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

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

  29. Templates - cont’d • <xsl:template match=”Chapter/@title”> • matches the title attribute of the chapter node <chapter title=”my title” … </chapter>

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

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

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

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

  34. XSLT: <xsl:for-each select=“oranges”> … </xsl:for-each> ColdFusion: <cfloop list=“#oranges#”> … </cfloop> <xsl:for-each> - Looping

  35. XSLT: <xsl:template match="name"> <xsl:value-of select=“.”/> <xsl:if test="position()!=last()"> , </xsl:if> </xsl:template> ColdFusion: <cfset pos=0> <cfloop list=“names”Index=“name”> <cfset pos=incrementValue(pos)> #name# <cfif pos neq listlen(names)> , </cfif> </cfloop> <xsl:if> - Conditional Logic Result: Bob, Mary, John, Martha, Sue

  36. XSLT: <xsl:template match="order"> <xsl:choose> <xsl:when test="total &lt; 5"> (small) </xsl:when> <xsl:when test="total &lt; 10"> (medium) </xsl:when> <xsl:otherwise> (large) </xsl:otherwise> </xsl:choose> </xsl:template> ColdFusion: <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:choose> - Conditional Logic

  37. <xsl:variable> - Variables XSLT: <xsl:variable name="n" select="2"/> ...<xsl:value-of select="item[$n]"/> ColdFusion: <cfset n=“2”> #listgetat(list,n)# • Variables may be defined Globally or Locally • Local variables can be defined within a template body

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

  39. 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”/>

  40. Resources • XSLT – Programmer’s 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.

More Related