xsl:sort
xsl:value-of
last function
position function
ELEMENT: xsl:for-each
<xsl:for-each
select="expression"
/>
</xsl:for-each>
The
xsl:for-each
element loops through each node in a node set in itsr order of occurrence and applies the same template to each node. A node set is simply the collection of all of the same XML tags (nodes) in an XML file. This process is also referred to as iterating over a set of nodes. The template is contained inside the
xsl:for-each
element between the opening and closing element. The syntax is:
<xsl:for-each>
code ...
</xsl:for-each>
In a formal definition, the template is said to be instantiated once to each node in the node set.
The default is to iterate through the nodes in the order in which they occur. This is referred to as document order. However, you may prefer to rearrange the order by using the
xsl:sort
element. This element defines the sort key upon which the sort (reorder) will be based.
This element is not self-closing. The separate closing element is mandatory.
select="expression"
The mandatory
select
attribute provides an expression that specifies which node set is to be processed by the loop. This can simply be a string that is the name of the node. Only one node set can be defined. However, by selecting a parent node, you can access the values of any child node via the template. You can use the
position
function to return the position number of the node currently being processed (the numbering starts at one). You can use the
last
function to return the total number of nodes being processed.
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_foreach.xsl"?>
and we rename it: xslt_example_foreach.xml
Code for xslt_example_foreach.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<
xsl:for-each select="devguru_staff/programmer"
>
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
<
/xsl:for-each
>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Click to view output in separate window
- requires Internet Explorer
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information