Re: [xsl] different first element in a list

Subject: Re: [xsl] different first element in a list
From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 24 Feb 2003 17:23:47 -0700 (MST)
Lorenzo De Tomasi wrote:
> I have some doubts:

Questions. You have questions, not doubts. Doubt implies skepticism.

> - why there is no need of <xsl:for-each> command?
> 
>     <xsl:for-each select=".">

well, you would select "data/element", not "." ... there's no difference 
between selecting "." and not having the xsl:for-each at all, because "." is
already the node being processed.

> - why if I use only one template it needs <xsl:for-each> command?

Templates are not instructions executed in the order they occur. They are like
function or subroutine definitions. Each template (with a match attribute)  
is declaring that it is a good match for processing a node that matches a 
certain pattern.

The XSLT processor starts by processing the root node of the source document.
The instructions in the best matching template for a root node are processed.
That would be the instructions in the template with match="/", most of the
time. Then, processing is done, and some kind of output is generated.

If one of the instructions was xsl:apply-templates or xsl:for-each, then it 
identifies some new nodes to be processed (in the select="...."). For 
apply-templates, the best matching templates for each of those nodes are found 
and their instructions are processed. For for-each, the content of the 
for-each is the template used for all of the selected nodes.

So, to answer your QUESTIONS ;) ...

  <xsl:template match="/">
    <xsl:apply-templates select="data/element">
  </xsl:template>

means "this template is a good match for a root node. if using it to process a 
node, select the element children of the data children of the current node, 
find the best matching templates for each of them, and instantiate their 
instructions." So you need a template that can be used to process those 
selected nodes:

  <xsl:template match="element">
    <!-- do something with each selected 'element' element -->
  </xsl:template>


...whereas

  <xsl:template match="/">
    <xsl:for-each select="data/element">
      <!-- do something with each selected 'element' element -->
    </xsl:for-each>
  </xsl:template>

has the same effect, but is only good if you want big templates and want to
process only 'element' elements and you want to process them the same way
every time. I try to avoid for-each.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread