|
December 29, 2001 Looping in XSLT Tips: December 2001
Yehuda Shiran, Ph.D.
|
|
The amazing capability of XSLT 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:
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:
Combining the opening-loop tag, the core of the loop, and the closing-loop tag looks like this:
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:
Here is how these column headers look like:
People who read this tip also read these tips: Look for similar tips by subject: |
| ||||||||||||||||||||