Note: This content is accessible to all versions of every browser. However, this browser may not support basic Web standards, preventing the display of our site's design details. We support the mission of the Web Standards Project in the campaign encouraging users to upgrade their browsers.
Tuesday, August 02, 2005 14:04 // Portland, Oregon, USA // href
taught by Evan Lenz
XSLT is a language for processing XML documents. XSLT itself is written in XML. The output generated by an XSLT 'program' can be anything, but normally it is used for generating (X)HTML documents which can then be displayed by a browser.
XSLT uses the XPATH language for addressing 'nodes' in an XML document.
XPATH Expressions
An XPath expression is made up from several steps separated by /.
step/step/step
A step consists of three elements: The axis to identify a set of nodes relative to the current-context node. The node test to filter out relevant nodes from the set of nodes selected by the axis and finally any number of optional predicates to further filter which nodes get selected.
axis::node-test[predicate][predicate]
XPath expressions can return 4 types of data:
node-set which is a number of zero or more nodes without duplicates
number a floating point number
string a unicode string
boolean true or false
XPath knows seven different types of nodes:
Root - the toplevel node of a document is called "/"
Element - 'tags'
PI - Processing instructions lt?xml ...?gt
Comment - Comment tags
Text - Character data, including white space!
Attribute - lttag attrib="xxx"gt
Namespace - lt?xml-stylesheet ...?gt
When selecting XPath nodes, you can use different 14(!) axis. By default, you use the child:: axis. Default means, that you don't even have to mention it. So an expression like section is actually child::section. The other common axis is attribute:: it also has an abbreviation, called @ so instead of writing attribute::section you can write @section
The other 12 axis are: descendant-or-self:: which can be abbreviated as // and parent:: aka .. and self:: aka . the remaining axis do not have abbreviations: ancestor:: following-sibling:: preceding-sibling:: following:: preceding:: namespace:: descendant:: ancestor-or-self::
XPath example
XPATH looks at an XML document as if it was a filesystem. Like when navigating a filesystem, there is a context node where from where XPATH looks at the document.
ltarticlegt ltheadinggtHellolt/headindgt ltparagtParagraph ltemphgt1lt/emphgtlt/paragt ltparagtParagraph 2lt/paragt lt/articlegt
If the first para node is the current context node the expressions would return the following:
* - emph emph - emph .. - para ../* - heading,para,para ../para[2] - the second paragraph /article/* - heading,para,para
The XSLT Processing Model
The most important command in xslt is xsl:apply-templates. A lot of people do not use this function properly since they write only one big template, instead of writing many small ones for different purposes.
If you use multiple templates, xslt will invoke a conflict resolution protocol when several templates match for a particular element.
Selection happens by priority:
-0.5 match="" -0.25 match="xyz:" 0 match="name" .5 match="nameA/nameB"
You can override the priority of a template by setting the priority attribute explicitly.
Whenever XSLT processing is stared, the processor executes the template-rule for /. If you do not supply a template-rule the processor will use its built-in template rules (there is one for each of the seven node types).
Content © by Tobias Oetiker