XSLT 1.0 Tutorial
XSLT 1.0 Tutorial
XSLT 1.0 Tutorial
http://www.pms.informatik.uni-muenchen.de/lehre/markupsemistrukt/02ss
Dan Olteanu
Dan.Olteanu@pms.informatik.uni-muenchen.de
Data Conversion
This is an
em
example
This is an
em
This is an
example
example
This is an
em
example
Push Processing
How is working? a template rule for each kind of node. apply templates for children. use built-in templates if needed. Application: similar structure for input and output. Example Chapter 2 from XSLT Programmers Reference, M. Kay. http://www.wrox.com XML Source: books.xml XSLT StyleSheet: books.xsl
Pull Processing
How is working? explicitly select and process the required nodes. <xsl:value-of select=pattern/> <xsl:apply-templates select=pattern/> <xsl:for-each select=pattern/> greater control over which nodes are to be processed. Application: very dierent structure for input and output. Example (Chapter 1) XML Source: books.xml XSLT StyleSheet: books_pull.xsl
Processing Modes
for processing the same node in the source tree more than once, but in dierent ways. another (not general) possibility: push and pull processing for the same node. example: handling the section headings of a book in two dierent ways for the table of contents (mode toc). <xsl:apply-templates select=heading mode=toc/> <xsl:template match=heading mode=toc/> inside the body of the document (mode body). <xsl:apply-templates select=heading mode=body/> <xsl:template match=heading mode=body/> Example Formatting the XML Specication Chapter 10 from XSLT Programmers Reference, M. Kay. http://www.wrox.com XML Source: REC-xml-19980210.xml XSLT StyleSheets: xmlspec.xsl, xpath.xsl, xslt.xsl
Built-in Templates
<xsl:apply-templates> is invoked to process a node, and there is no template rule in the stylesheet that matches that node. built-in template rule for each type of node. Node type root element attribute text commment pi namespace Built-in template rule call <xsl:apply-templates> to process its children. call <xsl:apply-templates> to process its children. copy the attribute value to the result tree. copy the text to the result tree. do nothing. do nothing. do nothing.
free of side-eects, i.e. obtain the same result regardless of the order/number of execution of the statements. Benets Useful for progressive rendering of large XML documents. In practice a value of a variable can not be updated.
processing described as a set of independent pattern matching rules. Benets XSLT - a declarative language. similar to CSS, but much more powerful. In practice a rule species what output should be produced when particular patterns occur in the input. dynamically-typed language. types are associated with values rather than with variables, like JavaScript.
XPath Expressions
evaluated in a context, consisting of a static and dynamic context. static context - depends on where the expression appears. set of namespace declarations in force at the point where the expression is written. set of variable declarations in scope at the point where the expression is written. set of functions available to be called. base URI of the stylesheet element containing the expression. for document() function. dynamic context - depends on the processing state at the time of expression evaluation. current values of the variables in scope. current location in the source tree, i.e. current node - the node currently being processed. context node - dierent from previous only for qualiers inside expressions. context position - position in the current node list. context size - size of the current node list.
Stylesheet Structure
<xsl:stylesheet> and <xsl:transform> elements. the outermost elements of any stylesheet. <?xsl:stylesheet?> processing instruction. used within an XML source to identify the stylesheet that should be used to process it. stylesheet modules, using <xsl:include> - textual inclusion of the referenced stylesheet module. Example( Chapter 03): sample.xml, principal.xsl, date.xsl, copyright.xsl <xsl:import> - the denitions in the imported module have lower import precedence. embedded stylesheets - inluded within another XML document, typically the document whose style it is dening.
XSLT Elements
dene template rules and control the way they are invoked: <xsl:template>, <xsl:apply-templates>, <xsl:call-template> dene the structure of a stylesheet: <xsl:stylesheet>, <xsl:include>, <xsl:import> generate output: <xsl:value-of>, <xsl:element>, <xsl:attribute>, <xsl:text>, <xsl:comment>, <xsl:processing-instruction> dene variables and parameters: <xsl:variable>, <xsl:param>, <xsl:with-param> copy information from the source to the result: <xsl:copy>, <xsl:copy-of> conditional processing and iteration: <xsl:if>, <xsl:choose>, <xsl:when>, <xsl:otherwise>, <xsl:for-each> sort and number: <xsl:sort>, <xsl:number> control the nal output format: <xsl:output>, <xsl:document>
Fill-in-the-blanks Stylesheets
the template looks like a standard HTML le. addition of extra tags used to retrieve variable data. useful for non-programmers with HTML authoring skills. useful when the stylesheet has the same structure as the desired output. xed content included as text or literal result elements. variable content included by means of <value-of> instructions, that extract the relevant data from the source. similar to a wide variety of proprietary templating languages. Example: orgchart.xml, orgchart.xsl (Chapter 9). table with one row per person, with three columns for persons name, title, and the name of the boss.
Navigational Stylesheets
still essentially output-oriented. use named templates as subroutines to perform commonly-needed tasks. use variables to calculate values needed in more than one place. looks very like a conventional procedural program with variables, conditional statements, loops, and subroutine calls. often used to produce reports on data-oriented XML, where the structure is regular and predictable. Example: booklist.xml, booksales.xsl (Chapter 9). report on the total number of sales for each publisher.
Rule-based Stylesheets
primarily consists of template rules, describing how dierent informations from the source should be processed. represents the principal way that it is intended to be used. is not structured according to the desired output layout. like an inventory of components that might be encountered in the source, in arbitrary order. good for sources with exible or unpredictable structure. natural evolution of CSS, with reacher pattern language and actions. Example: scene2.xml, scene.xsl (Chapter 9). HTML format for Scene 2 from Shakespeares Othello.
Computational Stylesheets
for generating nodes in the result tree that do not correspond directly to nodes in the source, e.g. there is structure in the source document that is not explicit in markup. complex aggregation of data. based heavily on functional programming paradigma no side-eects, i.e. no assignment instructions recursion instead of iteration Example: number-list.xml, number-total.xsl (Chapter 9). totaling a list of numbers.
3 2 1 0