[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: different first element in a list

Subject: Re: different first element in a list
From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 24 Feb 2003 17:23:47 -0700 (MST)
xsl for each first element
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.