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 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
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 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 output is an XML document or XML document fragment appears in the resulting output
Output Formats - Cont’d HTML 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 w3c 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
XML Parser MSXML This is the Parser that I used Free download from Microsoft Excellent Documentation Other Parsers - see chart on next slide Good understanding of XPath Used for XSLT expressions MSXML SDK - good Xpath documentation What tools will I need to use XSLT?
XML Parser Comparison Chart
To perform a transformation we need three things XML Data XSLT Stylesheet XML Parser Simple “Hello World” example
XML Data - Save as hello.xml Hello World
XSLT Stylesheet – Save as hello.xsl XSLT Example
Parsing – ColdFusion & MSXML Parser
//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"); 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); #output#
XSLT Stylesheet Structure The XSLT namespace has the URI Remember that an XSLT stylesheet is a well-formed XML document, therefore you must always include the corresponding closing tag. The tag is a synonym for 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 top- level element
Top Level Elements
Top Level Elements - cont’d...
Templates “Match” is a pattern, the pattern is expressed using Xpath Pattern describes which nodes in the source tree the template rule matches matches the root node matches the title node
Templates - cont’d matches the Title node that is a child to the Chapter node My Title
Templates - cont’d matches the title attribute of the chapter node <chapter title=”my title” …
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. ….instructions….
For each child of the current node Find the matching template rule Instantiate the template rule
Allows you to control the order that template rules are applied
Extract the required information from the node directly …
- Looping XSLT: … ColdFusion: …
- Conditional Logic XSLT:, ColdFusion: #name#, Result: Bob, Mary, John, Martha, Sue
- Conditional Logic XSLT: (small) (medium) (large) ColdFusion: (small) (medium) (large)
- Variables XSLT:... ColdFusion: #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 ColdFusion 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
Resources XSLT – Programmer’s Reference by Michael Kay (Wrox)* W3C* Microsoft MSXML SDK Documentation* us/xmlsdk/htm/sdk_intro_6g53.asp us/xmlsdk/htm/sdk_intro_6g53.asp 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.