1 <while> Statements

'While' is used to create iterative statements.

<while>
    <expr> !
        <cond> !
    </expr>
    <do> !
        <instructions> +
        <while.exit /> ?
        <while.continue /> ?
    </do>
</while>

The statement must be composed of a condition (<expr>) and a set of actions to be performed while that condition is met (<do>).

Example
Copy
<xsql-script name='while_sample1'>
<body>
    <set name='word'>word</set>
    <set name='i'>0</set>
    <set name='a'>0</set>
    <while>
        <expr>
            <lt>
                <i/>
                <string.length><word/></string.length>
            </lt>
        </expr>
        <do>
            <if>
                <expr>
                    <eq>
                        <string.substring>
                            <word/>
                            <i/>
                            <add><i/>1</add>
                        </string.substring>
                        <string>a</string>
                    </eq>
                </expr>
                <then>
                    <set name='a'><add><a/>1</add></set>
                </then>
            </if>
            <set name='i'><add><i/>1</add></set>
        </do>
    </while>
    <println>The word <word/> contains <a/> 'a' vowel.</println>
</body>
</xsql-script>

The loop makes a tour of the word contained in the 'word' variable, counting the number of times the vowel 'a' appears. On the other hand, the counter variable, i, is smaller (<lt>) than the length (<string.length>) of the word which the 'while' statement is iterating. For each round, the system checks that the letter in question is equal (<eq>) to 'a', in which case the corresponding counter is increased by 1 unit: <set name='a'><add><a/>1</add></set>

2 <for> Statements

'For' is used to create another type of iterative statement.

<for
    name='name'
    start='start'
    end='end'
    end-lt='end-lt'
    step='step'
>
    <do> !
        <action> !
        <for.exit /> *
    </do>
</for>

This iterative statement must be used when the number of times a block of statements is executed is known.

-The end and end-lt attributes indicate the final value of the counter. The difference between the two is that end includes the indicated value, and end-lt does not. For example (increasing by 1 unit each time, as is default):

Copy
start='0' end='2'    --> 3 rounds (0 1 2).
start='0' end-lt='2' --> 2 rounds (0 1)

Using the sign '-' in the step attribute specifies that the counter will decrease rather than increase.

Copy
<xsql-script name='for_sample1'>
    <body>

        <println></println>

        <!--
            i: starts at 6, ends at 0.

            end: while i >= 0

            6
            4
            2
            0
        -->
        <println>Start for A start='6' end='0' step='-2'</println>
        <println>------------------------------------------</println>

        <for name='i' start='6' end='0' step='-2'>
            <do>
                <println>for A: <i/></println>
            </do>
        </for>

        <!--
            i: starts in 6, ends in 0.

            end-lt: while i > 0

            6
            4
            2
        -->

        <println></println>
        <println>Start for B start='6' end-lt='0' step='-2'</println>
        <println>------------------------------------------</println>
        <for name='i' start='6' end-lt='0' step='-2'>
            <do>
                <println>for B: <i/></println>
            </do>
        </for>


        <!--
Here is an example situation where no loop is given because the end condition is not met: while i> = 0
Since it starts at 1, the condition is not fulfilled from the beginning, and the loop is never entered.
-->

        <println></println>
        <println>Start for C start='1' end='0'</println>
        <println>------------------------------------------</println>
        <for name='i' start='1' end='0'>
            <do>
                <println>for C: <i/></println>
            </do>
        </for>

        <println></println>

    </body>
</xsql-script>

3 <foreach> sentence

Cursor.

<foreach autocommit='true|false'>
    <union prefix='prefix'> ?
        <select prefix='prefix'> ?
            <select> !
        </select>
    </union>
    <nativesql /> ?
    <in prefix='prefix'> ?
        <value> !
    </in>
    <before> ?
        <instructions> !
    </before>
    <before-group of='of'> *
        <instructions> !
    </before-group>
    <after> ?
        <instructions> !
    </after>
    <after-group of='of'> *
        <instructions> !
    </after-group>
    <do> !
        <instructions> !
        <foreach.exit /> *
        <foreach.continue /> *
    </do>
</foreach>

remarks

The logs that iterate through can be obtained from:
An SQL query (native or XML syntax) that the UNION clause can contain.
Tag in with prefix (optional): Calls to
Tag in with prefix (optional): Virtual tables

The set of actions to be performed must be inside the <do> tag.
Example

Use a foreach to run the result of an SQL query.

Copy
<xsql-script name='foreach_code1'>
    <body>
        <foreach>
            <select prefix='clevels_'>
                <columns>code, name</columns>
                <from table='clevels' />
            </select>
            <do>
                <println>CODE: <clevels_code />, NAME: <clevels_name /></println>
            </do>
        </foreach>
    </body>
</xsql-script>
Example

Use a foreach to run the result of an SQL query.

Copy
<xsql-script name='test_foreach_soapsqlresponse'>
    <body>
        <set name='m_soap_sql_response'>
            <soap.call
                url='http://localhost:8080/soap/servlet/rpcrouter'
                uri='urn:SOAPSQLServer'
                method='executeSQL'
                user='demo'
                password='abcdemo'
                >
                <parameters>
                   <parameter name='database'>demo_sports1</parameter>
                   <parameter name='sqlstmt'>SELECT * FROM cdiarios</parameter>
                </parameters>
            </soap.call>
        </set>

        <println>
            <m_soap_sql_response />
        </println>

        <foreach>
            <in>
                <m_soap_sql_response />
            </in>
            <do>
                <println><code /></println>
            </do>
        </foreach>
    </body>
</xsql-script>
Example

Copy
<xsql-script name='test_before_group'>
<body>

    <!-- ==================================================================== -->
    <!-- Virtual table with requested articles, ordered by branch             -->
    <!-- and department.                                                      -->
    <!-- ==================================================================== -->
    <vtable name='v_art_solic'>
        <column name="delega"   type='string' />
        <column name="depart"   type='string' />

        <column name="codart"   type='string' />
        <column name="varlog"   type='string' />
        <column name="cansol"   type='decimal' />
    </vtable>

    <vtable.insert name='v_art_solic'>
        <matrix>
            { "D01", "P01", "024099", "0", "12" },
            { "D01", "P01", "024099", "0", "20" },
            { "D01", "P01", "026147", "0", "10" },
            { "D01", "P01", "026147", "0", "20" },
            { "D01", "P01", "026147", "2", "15" },
            { "D01", "P01", "026147", "2", "18" },
            { "D02", "0",   "024099", "0", "12" },
            { "D02", "0",   "024099", "0", "20" },
            { "D02", "0",   "024099", "0", "25" },
            { "D02", "0",   "024099", "0", "10" },
            { "D02", "0",   "026147", "0", "10" },
            { "D02", "0",   "026147", "3", "20" }
        </matrix>
    </vtable.insert>

    <!-- ======================================================================== -->
    <!-- Virtual table with totals by branch and article                          -->
    <!-- ======================================================================== -->
    <vtable name='v_tot_solic'>
        <column name="delega"   type='string' />
        <column name="codart"   type='string' />

        <column name="cantot"   type='decimal' />
    </vtable>

    <!-- ======================================================================== -->
    <!-- Sums up. When the branch and/or article changes, the accumulated         -->
    <!-- amount is initialized to 0.                                              -->
    <!-- ======================================================================== -->
    <foreach>
        <in prefix='m_'>

            <v_art_solic />
        </in>

        <before-group of='m_delega'>
            <set name='m_cantot'>0</set>
        </before-group>
        <before-group of='m_codart'>
            <set name='m_cantot'>0</set>

        </before-group>

        <after-group of='m_codart'>
            <vtable.insert name='v_tot_solic'>
                <column name='delega'><m_delega /></column>
                <column name='codart'><m_codart /></column>
                <column name='cantot'><m_cantot /></column>
            </vtable.insert>
        </after-group>

        <do>
            <set name='m_cantot'><add><m_cantot /><m_cansol /></add></set>
        </do>
    </foreach>

    <println><v_tot_solic /></println>

    <!--
      +------+------+------------+
      |delega|codart|cantot      |
      +------+------+------------+
      |D01   |024099|   32,000000|
      |D01   |026147|   63,000000|
      |D02   |024099|   67,000000|
      |D02   |026147|   30,000000|
      +------+------+------------+
    -->
</body>
</xsql-script>

4 <iterator> Statements

Iterator statements are used to traverse maps, arrays, lists (ArrayList, AbstractList, LinkedList, etc.), sets (AbstractSet, HashSet, LinkedHashSet, TreeSet), object arrays, or iterators.

<iterator
    name='name'
    type='key|entry'
    index='index'
>
    <in> !
        <value> !
    </in>
    <do> !
        <instructions> !
        <iterator.exit /> *
        <iterator.continue /> *
    </do>
</iterator>
Example

This example traverses a string array.

Copy
<xsql-script name='test_iterator'>
    <body>
        <array name = 'm_provinces'>
           <string>madrid</string>
           <string>barcelona</string>
           <string>granada</string>
           <string>sevilla</string>
           <string>burgos</string>
           <string>pontevedra</string>
        </array>
        <iterator name = 'm_provinc'>
          <in>
            <m_provinces/>
          </in>
          <do>
              <println><m_provinc/></println>
          </do>          
        </iterator>
    </body>
</xsql-script>