Altova XMLSpy 2025 Enterprise Edition

Collections and foreach

Home Prev Top Next

Collections and iterators

A collection contains multiple objects - like a ordinary array. Iterators solve the problem of storing and incrementing array indexes when accessing objects.

 

Syntax:

 

foreach iterator in collection

 statements

next

 

Example:

 

[foreach $class in $classes

 if not $class.IsInternal

         ]        class [=$class.Name];

[        endif

next]

 

Example 2:

 

[foreach $i in 1 To 3

      Write "// Step " & $i & "\n"

      ‘ Do some work

next]

 

 

In the first line:

 

$classes is the global object of all generated types. It is a collection of single class objects.

 

Foreach steps through all the items in $classes, and executes the code following the instruction, up to the next statement, for each of them.

 

In each iteration, $class is assigned to the next class object. You simply work with the class object instead of using, classes[i]->Name(), as you would in C++.

 

All collection iterators have the following additional properties:

 

Index

The current index, starting with 0

IsFirst

true if the current object is the first of the collection (index is 0)

IsLast

true if the current object is the last of the collection

Current

The current object (this is implicit if not specified and can be left out)

 

Example:

 

[foreach $enum in $facet.Enumeration

 if not $enum.IsFirst

         ], [

 endif

 ]"[=$enum.Value]"[

next]

© 2018-2024 Altova GmbH