Re: [xsl] xsl:for-each is not a loop <-- right?

Subject: Re: [xsl] xsl:for-each is not a loop <-- right?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 20 Feb 2025 22:20:51 -0000
On 20/02/2025 23:12, Roger L Costello costello@xxxxxxxxx wrote:
> Hi Folks,
>
> Suppose that I create a parameterized boolean function which tests its
parameter to see if it is greater than zero:
>
> <xsl:function name="f:GT_Zero" as="xs:boolean">
>      <xsl:param name="value"/>
>      <xsl:sequence select="$value gt 0"/>
> </xsl:function>
>
> I call it with a list of values:
>
> <xsl:sequence select="f:GT_Zero(1)"/>
> <xsl:sequence select="f:GT_Zero(2)"/>
> <xsl:sequence select="f:GT_Zero(3)"/>
>
> Here is a shorthand for that:
>
> <xsl:for-each select="(1,2,3)">
>      <xsl:sequence select=". gt 0"/>
> </xsl:for-each>
>
> xsl:for-each is not a loop.
>
> xsl:for-each is a shorthand for applying an expression to each item in a
list.
>
> In this example, the Boolean expression $value gt 0 is applied to each item
in the list (1, 2, 3).
>
> Do you agree?
>

Expression in the context of XSLT usually means XPath expression so I am
not sure where you get your description from.

https://www.w3.org/TR/xslt-30/#for-each says:

The|xsl:for-each|
<https://www.w3.org/TR/xslt-30/#element-for-each>instruction processes
each item in a sequence of items, evaluating thesequence constructor
<https://www.w3.org/TR/xslt-30/#dt-sequence-constructor>within
the|xsl:for-each|
<https://www.w3.org/TR/xslt-30/#element-for-each>instruction once for
each item in that sequence.

The|select|attribute isrequired; it contains anexpression
<https://www.w3.org/TR/xslt-30/#dt-expression>which is evaluated to
produce a sequence, called the input sequence. If there is an|xsl:sort|
<https://www.w3.org/TR/xslt-30/#element-sort>element present (see/13
Sorting/ <https://www.w3.org/TR/xslt-30/#sorting>) the input sequence is
sorted to produce a sorted sequence. Otherwise, the sorted sequence is
the same as the input sequence.

The|xsl:for-each|
<https://www.w3.org/TR/xslt-30/#element-for-each>instruction contains
asequence constructor
<https://www.w3.org/TR/xslt-30/#dt-sequence-constructor>. Thesequence
constructor <https://www.w3.org/TR/xslt-30/#dt-sequence-constructor>is
evaluated once for each item in the sorted sequence, with thefocus
<https://www.w3.org/TR/xslt-30/#dt-focus>set as follows:

  *

    Thecontext item <https://www.w3.org/TR/xslt-30/#dt-context-item>is
    the item being processed.

  *

    Thecontext position
    <https://www.w3.org/TR/xslt-30/#dt-context-position>is the position
    of this item in the sorted sequence.

  *

    Thecontext size <https://www.w3.org/TR/xslt-30/#dt-context-size>is
    the size of the sorted sequence (which is the same as the size of
    the input sequence).

For each item in the input sequence, evaluating thesequence constructor
<https://www.w3.org/TR/xslt-30/#dt-sequence-constructor>produces a
sequence of items (see/5.7 Sequence Constructors/
<https://www.w3.org/TR/xslt-30/#sequence-constructors>). These output
sequences are concatenated; if itemQfollows itemPin the sorted sequence,
then the result of evaluating the sequence constructor withQas the
context item is concatenated after the result of evaluating the sequence
constructor withPas the context item. The result of the|xsl:for-each|
<https://www.w3.org/TR/xslt-30/#element-for-each>instruction is the
concatenated sequence of items.



So there is more said than applying an expression to each item in a list
(which is not a term used in the spec).

Current Thread