Top Nav

Loops In XSL

1. Multiple looping within a report. Example: multiple UPS tracking numbers
to print in the footer of an invoice after detail lines have looped.

Loops are accomplished with xsl:apply-templates statements.

At the point in the document that you want the loop, insert an xsl:apply-templates tag with the select attribute set to the node that you want to loop on.

For example if you have tracking numbers in //report/tracking elements then use:

lt;xsl:apply-templates select=”tracking” />

At the end of the document you’ll want to define a template to render the tracking numbers. For example:

<xsl:template match=”//report/tracking” >
<fo:block><xsl:value-of select=”@number” /></fo:block>
</xsl:template>

Here’s what the xml might look like:

<report>
<tracking number=”123″ />
<tracking number=”456″ />
<tracking number=”789″ />
</report>

So for each loop, create a template that renders the looped item and add an apply-templates element at the point where you want the template inserted.

There is also ans xsl:for-each looping construct. It works inline without the need for an additional template. with the example data above we could do:

<xsl:for-each select=”//report/tracking”>
<fo:block><xsl:value-of select=”@number” /></fo:block>
</xsl:for-each>

You can have as many loops as you want.

2. Sub loops within loops. Example: each line item on an invoice could have
multiple lot numbers that need to be printed with everything that’s
associated with those lots (Country of Origin, Date of Mfg, Manufacturer…)

Loops as describe above can be nested. For-each element can be nested inside of each other. An xsl-template can contain calls to xsl:apply-templates.

Email
Print