The piece of code where most of programming is performed is xsl:template element. Real life stylesheets may contain tens or hundreds xsl:template elements and without thorough understanding of their usage you can never become a proficient XSLT programmer.
Any xsl:template element must contain either match or name attribute. Templates with match are the basic building blocks of XSLT stylesheets. Templates with name attribute have their important usage, but we can safely forget about them at this stage, we will return to them when "code reuse" will be discussed.
Value of match attribute is an XPath expression identifying nodes in the source document. "/" means the root node of the source document. As every XML document must have a root node, this stylesheet can be used for processing of any XML document.
Instructions inside xsl:template element are used for specification of output. The text inside xsl:template states: "Hello, world!" and these words appear in the output when stylesheet is being processed by an XSLT processor.
The template does not use any information from the source XML document. Therefore, if you apply it to any XML document, the output will be always the same. We will learn in following examples how to extract information from XML source documents.
|
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="/">Hello, world!</xsl:template> </xsl:stylesheet> |
|
|
XML
<aaa/> |
Output
Hello, world!
|
| Previous chapter: | - - - |
| Next chapter: | Sequences |
| Previous page: | The first stylesheet |
| Next page: | Element xsl:text |