Learning XSLT

5,525 views

Published on

This is a slide presentation I gave at XML 2004 in Washington, DC. It covers the basics of XSLT.

Published in: Technology
  • Be the first to comment

Learning XSLT

  1. 1. Learning XSLT A Tutorial Mike Fitzgerald Wy’east Communications
  2. 2. Quick Start: What Is XSLT? <ul><li>Extensible Stylesheet Language or XSLT </li></ul><ul><li>XML Path Language or XPath is a companion specification to XSLT </li></ul><ul><li>Both recommendations published by the World Wide Web Consortium (W3C) in November 1999 </li></ul>
  3. 3. Quick Start: Templates <ul><li>XSLT stylesheets are driven by templates </li></ul><ul><li>Documents processed on a template model </li></ul><ul><li>Templates can have: (1) innate priority or (2) assigned assigned priority (rare) </li></ul><ul><li>Templates can have modes </li></ul><ul><li>Can name templates, pass them parameters </li></ul><ul><li>Built-in templates </li></ul><ul><li>Attribute value templates </li></ul>
  4. 4. Quick Start: XPath <ul><li>XPath is a non-XML syntax for addressing nodes with location paths </li></ul><ul><li>XPath data model describes XML documents with seven node types </li></ul><ul><li>Types are root (document in 2.0), element, attribute, text, namespace, comment, processing instruction </li></ul>
  5. 5. Quick Start: More XPath <ul><li>Expressions in attribute values </li></ul><ul><li>Patterns, subset of expressions </li></ul><ul><li>Predicates, method for filtering nodes </li></ul><ul><li>Axes, forward and reverse </li></ul><ul><li>Arithmetic in expressions </li></ul><ul><li>Functions in expressions (XSLT adds functions XPath set of functions) </li></ul>
  6. 6. Quick Start: Output <ul><li>Output methods: text, XML, or HTML (XHTML in 2.0) </li></ul><ul><li>Literal result elements </li></ul><ul><li>Instruction elements </li></ul><ul><li>Control over things like indentation, XML declarations, document type declarations, media types, and more </li></ul>
  7. 7. Quick Start: Processing <ul><li>Several methods of copying nodes across </li></ul><ul><li>Variables and parameters </li></ul><ul><li>Can pass parameters in from outside stylesheet, and outside a template </li></ul><ul><li>Can process nodes conditionally </li></ul><ul><li>Sorting and numbering of lists </li></ul>
  8. 8. Quick Start: Keys <ul><li>Keys are mainly a performance hike </li></ul><ul><li>You can use more than one key </li></ul><ul><li>You can use parameters with keys </li></ul><ul><li>You can cross-reference with keys </li></ul>
  9. 9. Quick Start: Multiple Documents <ul><li>Multiple stylesheets through inclusion </li></ul><ul><li>Multiple stylesheets through importation </li></ul><ul><li>Multiple source documents </li></ul><ul><li>Multiple output documents (through extension in 1.0, standard in 2.0) </li></ul>
  10. 10. Quick Start: Alternative Stylesheets <ul><li>Normal stylesheets </li></ul><ul><li>Literal result stylesheets </li></ul><ul><li>Embedding stylesheets </li></ul><ul><li>Namespace aliasing </li></ul><ul><li>Namespace exclusion </li></ul>
  11. 11. Quick Start: Extensions <ul><li>XSLT designed for extension </li></ul><ul><li>Can use processor-specific extensions, such as those from Xalan, Saxon, EXSLT </li></ul><ul><li>Can extend XSLT yourself </li></ul><ul><li>XSLT 2.0 provides a native way to write extension functions in XML syntax </li></ul>
  12. 12. Main Presentation <ul><li>A brief introduction to XSLT and XPath </li></ul><ul><li>Focus on version 1.0, with asides to 2.0 </li></ul><ul><li>Most features covered, though not all </li></ul><ul><li>Basic understanding of XML assumed </li></ul><ul><li>Example files available on CD (in DOS format, so use conv –U for Unix line endings) </li></ul>
  13. 13. W3C Specifications <ul><li>XSLT 1.0 specification </li></ul><ul><ul><li>http://www.w3.org/TR/xslt </li></ul></ul><ul><li>XPath 1.0 specification </li></ul><ul><ul><li>http://www.w3.org/TR/xpath </li></ul></ul><ul><li>Working drafts of version 2.0 editions </li></ul><ul><ul><li>http://www.w3.org/TR/xslt20 </li></ul></ul><ul><ul><li>http://www.w3.org/TR/xpath20 </li></ul></ul>
  14. 14. What Is XSL-FO? <ul><li>Originally, XSLT was included in Extensible Stylesheet Language or XSL </li></ul><ul><li>XSL deals with appearance, formatting </li></ul><ul><li>XSL is commonly called XSL-FO </li></ul><ul><li>Specs parted company in April 1999 </li></ul>
  15. 15. What Does XSLT Do? <ul><li>A language in XML syntax that defines and describes transformations </li></ul><ul><li>Transforms XML to: </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>HTML </li></ul></ul><ul><ul><li>XHTML (2.0) </li></ul></ul><ul><ul><li>plain text </li></ul></ul>
  16. 16. From Source to Result <ul><li>Source XML document becomes source tree </li></ul><ul><li>Source document could be a file or a stream </li></ul><ul><li>Result document could be a file or stream </li></ul><ul><li>A result tree can be serialized as an XML, XHTML, HTML, or text document </li></ul><ul><li>Text documents could be C++, Java, Python, SQL, or what have you </li></ul>
  17. 17. Process Flow Source Style- sheet Result Processor Inputs Processor Output
  18. 18. Templates <ul><li>The template is the heart of XSLT </li></ul><ul><li>Templates match patterns in source trees </li></ul><ul><li>A pattern is generally a location in the structure of the source tree </li></ul><ul><li>A template is invoked to search for a pattern </li></ul><ul><li>A template that finds a pattern can be instantiated and create a result tree for output </li></ul>
  19. 19. Stylesheet Basics <ul><li>A document element in XSLT can be stylesheet or transform </li></ul><ul><li>version is required attribute </li></ul><ul><li>Namespace declaration is required </li></ul><ul><ul><li>http://www.w3.org/1999/XSL/Transform </li></ul></ul><ul><li>Conventional prefix is xsl </li></ul>
  20. 20. ch01/msg.xml & ch01/msg.xsl <ul><li>ch01/msg.xml just an empty document element </li></ul><ul><li>ch01/msg.xsl , document element is stylesheet , version attribute, namespace </li></ul><ul><li>Top-level element output , and method attribute with value of text </li></ul><ul><li>Top-level element template matches the pattern msg using the match attribute </li></ul><ul><li>Literal result text Found it! </li></ul>
  21. 21. Some XSLT Processors <ul><li>Instant Saxon ( http://saxon.sourceforge.net ) </li></ul><ul><li>saxon msg.xml msg.xsl </li></ul><ul><li>Saxon ( http://saxon.sourceforge.net ): </li></ul><ul><li>java –jar saxon7.jar msg.xml msg.xsl </li></ul><ul><li>Xalan C++ ( http://xml.apache.org ): </li></ul><ul><li>xalan msg.xml msg.xsl </li></ul><ul><li>XRay ( http://www.architag.com ) </li></ul><ul><li>xmlspy ( http://www.xmlspy.com ) </li></ul>
  22. 22. A Simple Transformation <ul><li>Transform the document with the stylesheet: </li></ul><ul><li>xalan msg.xml msg.xsl </li></ul><ul><li>Yields this result: </li></ul><ul><li>Found it! </li></ul><ul><li>How did that happen? </li></ul>
  23. 23. ch01/msg-pi.xml <ul><li>An XML stylesheet processing instruction is similar to the HTML element LINK </li></ul><ul><li>Points to a stylesheet: </li></ul><ul><li><?xml-stylesheet href= &quot; msg.xsl &quot; type= &quot; text/xsl &quot; ?> </li></ul><ul><li>Some processors accept text/xslt </li></ul><ul><li>A W3C specification: </li></ul><ul><li>http://www.w3.org/TR/xml-stylesheet </li></ul>
  24. 24. XSLT & Browsers <ul><li>Several browsers support client-side XSLT transformations, such as IE 6.0, Netscape 7.1, Mozilla 1.7, Firebird 0.9 </li></ul><ul><li>Load ch01/msg-pi.xml in a browser </li></ul><ul><li>See the results in that browser </li></ul>
  25. 25. ch01/message.xml <ul><li>XML declaration with version information </li></ul><ul><li>An XML declaration is not a processing instruction </li></ul><ul><li>Document element message </li></ul><ul><li>Attribute priority with value low </li></ul><ul><li>Text content (#PCDATA in DTD terms) </li></ul>
  26. 26. value-of Instruction Element <ul><li>Children of top-level elements are usually instruction elements </li></ul><ul><li>value-of ’s required select attribute </li></ul><ul><li>Returns a string value </li></ul>
  27. 27. ch01/message.xsl <ul><li>stylesheet element with version and namespace </li></ul><ul><li>output element, method is text </li></ul><ul><li>template matches message </li></ul><ul><li>value-of selects text content of message </li></ul><ul><li>text() is a node test for text nodes </li></ul>
  28. 28. Another Transformation <ul><li>Transform the document with the stylesheet: </li></ul><ul><li>xalan message.xml message.xsl </li></ul><ul><li>Yields this result: </li></ul><ul><li>Hey, XSLT isn’t so hard after all! </li></ul><ul><li>What happened? </li></ul>
  29. 29. Applying Templates <ul><li>Templates processes children of the matched node </li></ul><ul><li>The apply-templates finds other templates that match those children </li></ul><ul><li>The select attribute of apply-templates finds a specific template that matches a child </li></ul>
  30. 30. XPath 1.0 Data Model <ul><li>XPath sees XML documents as a set of one or more nodes of seven types: </li></ul><ul><ul><li>Root (or document in 2.0) </li></ul></ul><ul><ul><li>Element </li></ul></ul><ul><ul><li>Attribute </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><ul><li>Namespace </li></ul></ul><ul><ul><li>Comment </li></ul></ul><ul><ul><li>Processing instruction </li></ul></ul>
  31. 31. ch04/nodes.xml via ch04/tree-view.xsl
  32. 32. Root Node <ul><li>The root node is the root of the XML document </li></ul><ul><li>Called the document node in XPath 2.0 </li></ul><ul><li>Has at most one element node child, that is, the root or document element </li></ul><ul><li>Location path: / </li></ul>
  33. 33. Element Nodes <ul><li>Elements are the building blocks of structured XML documents </li></ul><ul><li>A location path: message/body/line </li></ul><ul><li>All elements: * </li></ul><ul><li>All elements of a given namespace (name test): rng:* </li></ul>
  34. 34. Attribute Nodes <ul><li>Attributes effectively modify elements </li></ul><ul><li>Location path: message/@priority </li></ul><ul><li>All attributes: @* </li></ul><ul><li>xmlns , with or without a prefix (as in xmlns:rng ), is not treated as an normal attribute </li></ul>
  35. 35. Other Nodes <ul><li>Match text nodes with text() node test </li></ul><ul><li>Match namespace nodes with namespace::* axis or namespace-uri() function </li></ul><ul><li>Match comments with comment() node test </li></ul><ul><li>Match processing instructions with processing-instruction() node test </li></ul>
  36. 36. Built-in Template Rules <ul><li>Hidden magic that is sometimes confusing </li></ul><ul><li>Automatically kick in when an explicit matching template is not found, but a node matching the built-in template is present </li></ul><ul><li>In absence of appropriate templates, built-in templates are used </li></ul>
  37. 37. Built-in Templates... <ul><li>Match patterns </li></ul><ul><li>Match the root node and all element nodes, including modes </li></ul><ul><li>Match text and attributes </li></ul><ul><li>No-ops on comments, processing instructions, namespace nodes </li></ul><ul><li>Try ch10/built-in.xsl </li></ul>
  38. 38. Template Rules <ul><li>Templates have priority when more than one template matches the same node </li></ul><ul><li>The template element has a priority attribute which takes a real number </li></ul><ul><li>Stylesheets can be included and imported </li></ul><ul><li>Imported templates are governed by import precedence rules </li></ul>
  39. 39. Literal Result Elements <ul><li>You can literally add text, elements, and attributes to a result tree </li></ul><ul><li>Can use attribute value templates ( {} ) in the attribute values of literal result elements </li></ul><ul><li>Namespaces apply to literal result elements </li></ul><ul><li>Markup in stylesheet subject to well-formedness constraints </li></ul>
  40. 40. Literal Examples <ul><li>Examples from ch02 ... </li></ul><ul><li>Literal text: </li></ul><ul><ul><li>xalan text.xml txt.xsl </li></ul></ul><ul><li>Literal result element: </li></ul><ul><ul><li>xalan literal.xml literal.xsl </li></ul></ul><ul><li>Literal HTML markup: </li></ul><ul><ul><li>xalan literal.xml html.xsl </li></ul></ul><ul><li>Literal XHTML markup (with 1.0): </li></ul><ul><ul><li>xalan doc.xml doc.xsl </li></ul></ul>
  41. 41. Instruction Elements <ul><li>element creates an element </li></ul><ul><li>attribute creates an attribute </li></ul><ul><li>attribute-set creates a reusable set of attributes (top-level element) </li></ul><ul><li>text creates text </li></ul><ul><li>comment creates comments </li></ul><ul><li>processing-instruction creates a PI </li></ul>
  42. 42. ch02/final.xsl <ul><li>attribute-set is a top-level element that can contain zero or more attribute elements </li></ul><ul><li>processing-instruction creates an XML stylesheet PI for a CSS document </li></ul><ul><li>comment adds a comment to the document prolog before document element </li></ul>
  43. 43. More on ch02/final.xsl <ul><li>Literal result elements </li></ul><ul><li>element creates elements </li></ul><ul><li>attribute value is derived the from source using an absolute location path </li></ul><ul><li>text element adds some text to the result without adding unintended whitespace </li></ul>
  44. 44. output Element <ul><li>Top-level element (zero or more) </li></ul><ul><li>Controls output methods: xml , html , text or a QName (qualified name) </li></ul><ul><li>xhtml method available in XSLT 2.0 </li></ul><ul><li>Structural indentation with the indent attribute ( yes or no ) </li></ul><ul><li>encoding attribute has values such as US-ASCII , UTF-8 , or ISO-8859-1 </li></ul>
  45. 45. XML Declaration & output <ul><li>omit-xml-declaration attribute ( yes or no ) </li></ul><ul><li>version attribute sets version information </li></ul><ul><li>standalone attribute sets standalone declaration ( yes or no ) </li></ul>
  46. 46. More on output <ul><li>media-type attribute sets media type as in text/xml or application/xml </li></ul><ul><li>cdata-section-elements sets result tree elements that contain CDATA sections </li></ul><ul><li>doctype-system and doctype-public output document type declarations using SYSTEM or PUBLIC identifiers </li></ul><ul><li>See examples in ch03 </li></ul>
  47. 47. Expressions <ul><li>Defined by XPath </li></ul><ul><li>Expressions in select attributes </li></ul><ul><li>Expressions can contain: </li></ul><ul><ul><li>patterns (next slide) </li></ul></ul><ul><ul><li>boolean and relational logic ( and , or , != , > ) </li></ul></ul><ul><ul><li>arithmetic ( + , - , * , / , mod ) </li></ul></ul><ul><ul><li>functions ( name() , last(), count()) </li></ul></ul><ul><ul><li>variable references ( $var ) </li></ul></ul>
  48. 48. Patterns <ul><li>Defined by XSLT as subset of an expression </li></ul><ul><li>Patterns most often in match attributes </li></ul><ul><li>Patterns often contain: </li></ul><ul><ul><li>location steps ( date/year ) </li></ul></ul><ul><ul><li>/ (root) or // (descendant) or | (union) </li></ul></ul><ul><ul><li>Node test ( * , rng:* , comment() , text() ) </li></ul></ul><ul><ul><li>id() or key() functions </li></ul></ul><ul><ul><li>child:: or attribute:: axes </li></ul></ul><ul><ul><li>Predicate ( state/name[.='Oregon'] ) </li></ul></ul>
  49. 49. Axes <ul><li>Abbreviated and unabbreviated syntax </li></ul><ul><li>Double colon separator ( attribute::priority ) </li></ul><ul><li>Forward or reverse axes: </li></ul><ul><ul><li>Forward: attribute, child, descendant, descendant-or-self, following, following-sibling, namespace, parent, self </li></ul></ul><ul><ul><li>Reverse: ancestor, ancestor-or-self, preceding, and preceding-sibling </li></ul></ul><ul><li>See examples in ch04 </li></ul>
  50. 50. Functions <ul><li>Part of expressions, so always called in attribute values that contain expressions (usually select ) </li></ul><ul><li>Node-set, number, boolean, string functions </li></ul><ul><li>XPath functions examples: position() , concat() , not() , sum() </li></ul><ul><li>XSLT function examples: document() , current() , key() , format-number() </li></ul><ul><li>See examples in ch05 </li></ul>
  51. 51. Copying Nodes <ul><li>Shallow copy versus deep copy </li></ul><ul><li>copy element performs a shallow copy: element nodes and namespace nodes </li></ul><ul><li>copy-of element performs deep copy: element and child nodes, attribute nodes, namespace nodes </li></ul><ul><li>See examples in ch06 </li></ul>
  52. 52. Variables & Parameters <ul><li>variable and param elements, both top-level or instruction elements </li></ul><ul><li>Referenceable value ( $var ) with scope </li></ul><ul><li>Can’t change variable value </li></ul><ul><li>Result tree fragment or temporary tree in XSLT 2.0 (see with extensions) </li></ul><ul><li>See examples in ch07 </li></ul>
  53. 53. More on Variables & Parameters <ul><li>param value can change, have default </li></ul><ul><li>Can pass parameters to a stylesheet </li></ul><ul><li>Can pass parameters to a template using with-param </li></ul>
  54. 54. Sorting Nodes <ul><li>The sort element sorts nodes </li></ul><ul><li>Child of apply-templates or for-each </li></ul><ul><li>Ascending or descending </li></ul><ul><li>Alphabetical or numerical </li></ul><ul><li>If alphabetical, by uppercase or lowercase </li></ul><ul><li>See examples in ch08 </li></ul>
  55. 55. Numbering Nodes <ul><li>The number element inserts formatted numbers into result tree as for a list </li></ul><ul><li>Numerical, alphabetical, Roman numerals </li></ul><ul><li>Used with format-number() function </li></ul><ul><li>Also used with decimal-format top-level element </li></ul><ul><li>See examples in ch09 </li></ul>
  56. 56. Named Templates <ul><li>A template element can have a name attribute </li></ul><ul><li>Can have match and name attributes together, but if no match , must have name </li></ul><ul><li>Can call templates by name with the call-template element, with parameters ( with-param ) </li></ul><ul><li>Displaced by XSLT 2.0 element function </li></ul><ul><li>See examples in ch10 </li></ul>
  57. 57. Modes <ul><li>Can’t apply template that matches a given node more than once </li></ul><ul><li>Overcome by modes </li></ul><ul><li>Matching mode attribute on template and apply-templates </li></ul>
  58. 58. Keys <ul><li>Can match nodes based on a key value </li></ul><ul><li>Define keys at the top-level using the key element </li></ul><ul><li>Later called using the key() function in patterns or expressions </li></ul><ul><li>See examples in ch11 </li></ul>
  59. 59. Conditional Processing <ul><li>Test a single condition with an expression using an if instruction element </li></ul><ul><li>Test multiple conditions with expressions using when instruction elements in choose (if-then statement) </li></ul><ul><li>choose can contain optional otherwise element (if-then-else statement) </li></ul><ul><li>See examples in ch12 </li></ul>
  60. 60. Including Stylesheets <ul><li>Can include external stylesheet(s) with include element </li></ul><ul><li>Include effectively merges stylesheets </li></ul><ul><li>See examples in ch13 </li></ul>
  61. 61. Importing Stylesheets <ul><li>Can also import external stylesheet(s) with import element </li></ul><ul><li>Import builds import tree with precedence based on sequence of imports </li></ul><ul><li>apply-imports can trump normal precedence </li></ul><ul><li>See examples in ch13 </li></ul>
  62. 62. Alternative Stylesheets <ul><li>Literal result element stylesheet uses a literal result element as the document element </li></ul><ul><li>Embedded stylesheets allow you to combine source tree and stylesheet into one document by using a fragment identifier with an id attribute </li></ul><ul><li>See examples in ch14 </li></ul>
  63. 63. Extensions <ul><li>XSLT 1.0 can’t satisfy everyone, so extensions possible </li></ul><ul><li>Can write namespace-qualified extension elements, functions, and attributes </li></ul><ul><li>Can define your own extensions or use extensions already defined </li></ul><ul><li>Can define fallback behavior using fallback and message elements </li></ul><ul><li>See examples in ch15 </li></ul>
  64. 64. XSLT 2.0 Highlights <ul><li>xhtml output method </li></ul><ul><li>Multiple result trees ( result-document ) </li></ul><ul><li>Regular expressions ( analyze-string ) </li></ul><ul><li>Define stylesheet functions ( function ) </li></ul><ul><li>Validation with XML Schema </li></ul><ul><li>Format dates ( date-format , format-date() ) </li></ul><ul><li>Grouping ( for-each-group ) </li></ul><ul><li>Character maps ( character-map , output-character ) </li></ul>
  65. 65. XPath 2.0 Highlights <ul><li>Many new functions (177 versus 27) </li></ul><ul><li>Strongly typed, relying on XML Schema datatypes </li></ul><ul><li>Kind tests ( document-node() , element(), attribute() ) </li></ul><ul><li>Casting ( cast , castable ) </li></ul><ul><li>for and if statements in expressions </li></ul><ul><li>See examples in ch16 </li></ul>
  66. 66. Ox Documentation Tool <ul><li>Syntax-related XSLT documentation on the command line </li></ul><ul><li>XML vocabulary items, too </li></ul><ul><li>Example: </li></ul><ul><ul><li>java –jar ox.jar xsl:text </li></ul></ul><ul><li>See: </li></ul><ul><ul><li>http://www.wyeast.net/ox.html </li></ul></ul>
  67. 67. So Long and Thanks! Mike Fitzgerald Wy’east Communications

×