Re: [xsl] XSLT Concepts

Subject: Re: [xsl] XSLT Concepts
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 01 Oct 2010 14:37:23 +0100
On 01/10/2010 1:50 PM, chirag matkar wrote:
Hello Friends,
I am an Amateur Developer in Xsl transformations.
I work on Research Article XML conversions.
I just want to be clear on some Concepts of XSLT.
Welcome.

1)When we apply a Template<xsl:apply-templates/>  the whole text within
the tags in input file data gets applied and new  element  are
processed as we declare them.

xsl:apply-templates, in the absence of a select attribute, selects the immediate children of the context node, and for each one, it finds the best matching template rule, and applies that rule. If there's no explicit rule for a node in the stylesheet, it will apply the built-in rule (this often happens for text nodes, where the built-in rule copies the text unchanged to the output).


Note I'm using the terminology of trees and nodes, rather than tags and input files. If you want to understand XSLT concepts, you need to learn to think of the input XML as a tree (the tree that comes out of the XML parser), not the lexical angle-bracket stuff that went into the parser.

An individual call on xsl:apply-templates only causes the children to be processed, not all the descendants. However, the default action for the children is to recurse downwards to their own children, so the default processing does a complete tree walk.
So when we use<xsl:value of select=""/>  ,additional data is
processed.This results into extra junk data as i have noticed.How to
avoid such circumstances.?

You should (usually!) process text nodes using ether xsl:apply-templates or xsl:value-of, but not both.


2)Also Suppose we tag the input data with new elements,the tags appear
in the sequence of initial text in the data.How can we sequentially
keep changing order of the data as we wish and apply templates
accordingly.
Example -  we need to tag article title which appears at end in the
input file but we wish to have it tagged first in the output file.ie
change order of tags according to our requirements.

This is where the select attribute of xsl:apply-templates comes in. Normally the children are processed in (input) document order. If you want a different order, or if you want to process the children selectively, you can do:


<xsl:apply-templates select="author, abstract, citations, body"/>

(That's XSLT 2.0 syntax: in 1.0 you need four sucessive calls on xsl:apply-templates, each to process one kind of child element).

3)Can we template match a tag more than one time and process nodes within it sequentially?


Yes. If you want different processing on different occasions, use modes. The usual example is a table of contents:

<xsl:apply-templates select="*" mode="table-of-contents"/>
<xsl:apply-templates select="*" mode="article-body"/>

The xsl:template rule itself has a mode attribute, and the template will only be activated if its mode matches the requested mode.

Michael Kay
Saxonica

Current Thread