Xsl template name
Keywords: xsl template name
Description: The <xsl:template> and <xsl:apply-templates> elements both have a mode attribute. Modes are handy when you need to process the same section of XML more than once. It allows you to create
The <xsl:template> and <xsl:apply-templates> elements both have a mode attribute. Modes are handy when you need to process the same section of XML more than once. It allows you to create these templates and only call the one you want.
When you create the template, you specify the mode attribute, giving it a name to describe this mode. To call the template, use <xsl:apply-templates>, but with the addition of the mode attribute, specifying the same mode.
Now, in addition to matching the templates against the source tree, the XSL processor will also make sure that the only templates called are ones which are part of this mode. For example, if we have the following XSLT:
only the first template will be instantiated, even though all of these templates match against the same section of the source tree.
Keep in mind that the built-in templates also apply when modes are being used. So, along with the default template which matches any elements or the document root
where "mode" refers to the current mode. In other words, if an XSLT processor is currently working in "TOC" mode, and comes across an element that has no templates defined for it, the processor will apply a template like the following:
One common example is to use modes to transform an XML document into a web page which includes a table of contents with internal links to sections lower on the same page. A template running under one mode can be used for the table of contents. A template matching against the same section of the document can be run under another mode for the actual body of the document.
The snippets of text are enough to demonstrate what we're doing - transform it to HTML, with a table of contents we can use to navigate to any section.
The position() function returns a node's numeric position within a node-set; that is, for the first node in a node-set, position() would return "1", and for the second "2".
The concat() function takes two strings, and ties them together. In this case, we're taking the string "#section", and adding it to the value returned from the position() function, to create strings similar to "#section1", "#section2", etc.
The next effect creates an HTML list item for each section in the body, using the position() function to make sure that every section is uniquely identified.
This template creates an <h2> heading, with the title of the section, and wraps it in an <a> to identify it, so that the links from the table of contents will work. Then <xsl:apply-templates> is called, to take care of the rest of the work in creating this section.
The first transforms the <paragraph> elements to HTML <p> elements, and the <important> elements to HTML <b> elements:
The last template matches against any PCDATA which is a direct child of a <section> element; that is, its title. This is done so that these titles won't make it into the body of the section itself.
[The problem we're avoiding here has to do with the built-in templates, which all XSL processors implement. One of these default templates matches against any text that isn't matched by any other template, and outputs that text to the result tree. In our example, when we call <xsl:apply-templates> from within the body template, the PCDATA which is a direct child of a <section> element would be inserted into the result tree as well. The last template will hide these titles.]