spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column100


Web Services, Part V: XML and XSLT Programming

Looping in XSLT

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

One of XSLT's advantages is its ability to loop through data. You can direct XSLT to search for a specific tag, and then loop through all this tag's children. You start an XSLT loop with the <xsl:for-each> tag. You specify the data element to loop through by the select attribute of this tag. Suppose the data is as shown below, and you want to loop through the four different weeks of the first-month record. You will do it by writing:

<xsl:for-each select="//data/month[1]/week">

Besides looping, you probably want to do some useful stuff while at it. Suppose you want to prepare headers for four columns. The first column will be named W1 (for Week 1), the second column is W2, the third is W3, and the last one is W4. You can print the character W and concatenate the number of the week to it. You can pick the serial number of the week from the number attribute, through the <xsl:value-of/> tag:

<xsl:value-of select="@number"/>

Combining the opening-loop tag, the core of the loop, and the closing-loop tag looks like this:

<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
</xsl:for-each>

Let's add two more columns. The first column will be the string "Month\Week", and the last one will be the string "Total". As you have probably already noticed, we also format the columns in an HTML TABLE:

<TABLE>
<TR>
<TH>Month\Week</TH>
<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
</xsl:for-each>
<TH>Total</TH>
</TR>
</TABLE> 

Here is what these column headers will look like:

Here is the XML file again:

<?xml version="1.0" ?>
  <sales>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales report for January, February, 
	           and March of 2001</description>
    </summary>
    <data>
      <month>
        <name>January 2001</name>
        <week number="1" dvds_rented="12000" />
        <week number="2" dvds_rented="15000" />
        <week number="3" dvds_rented="18000" />
        <week number="4" dvds_rented="11000" />		  
      </month>
      <month>
        <name>February 2001</name>
        <week number="1" dvds_rented="11000" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11200" />		  
      </month>
      <month>
        <name>March 50</name>
        <week number="1" dvds_rented="11000" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11200" />		  
      </month>
    </data>
  </sales>

Next: How to format numbers by XSLT

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: December 31, 2001
Revised: December 31, 2001

URL: http://www.webreference.com/js/column100/5.html