Iterate Across Groups with a for-each-group Constructor

You can iterate across a series of groups with a for-each-group constructor in designer mode in the mapper. The grouping criteria is specified by the attributes. The required select attribute identifies the elements to sort and group. The group-by attribute describes how to group the elements.

Capabilities

  • You can only map repeated source elements to the for-each-group node (same as a for-each constructor).
  • You can only drop the for-each-group as a parent in a target tree node (same as a for-each constructor).
  • When you drop a for-each-group on a target node, a child group-by attribute node is created.
  • The Delete node option is disabled for the group-by attribute node.
  • When you delete a for-each-group, the group-by attribute node is also deleted.
  • If you are using an attribute other than group-by in code mode (such as group-adjacent, group-starting-with, or group-ending-with), you cannot switch to designer mode.

Iterate Across Groups

  1. Click XSLT.
  2. In the upper right corner, click Toggle functions Mapper functions icon.
  3. Expand XSL Constructors, and then Flow Control.

    For this use case, the mapper shows the following source and target elements.


    The Sources, Mapping canvas, and Target sections are shown. In the Target section, XSL Functions, and then Flow Control are expanded to show the for-each-group construct. In the Target tree, the book element is selected.

  4. Drag a for-each-group to the target parent book element. The element must be a repeating node.


    The Target section is shown. In the Target section, XSL Functions, and then Flow Control are expanded to show the for-each-group construct being dragged to the book element.

    The for-each-group constructor includes the group-by attribute.


    The Target tree shows the for-each-group and group-by elements above the book element.

  5. Connect the book source element to for-each-group.


    The Sources, Mapping canvas, and Target sections are shown. The source book element has been connected to the for-each-group target element.

  6. Connect the price source element to group-by to iterate on the basis of book price.


    The Sources, Mapping canvas, and Target sections are shown. The source book element is connected to the for-each-group target element. The source price element is connected to the target group-by element.

  7. Drag the source element firstName to the target element firstName and the source element lastName to the target element lastName.


    The Sources, Mapping canvas, and Target sections are shown. The source book element is connected to the target for-each-group element. The source price element is connected to the target group-by element. The source firstName element is connected to the target firstName element. The source lastName element is connected to the target lastName element.

    Two supporting functions provided for use with for-each-group are available under the Advanced category of Functions:
    • current-group: Returns the group currently being processed for each group instruction.
    • current-grouping-key: Returns the grouping key of the group currently being processed for each group instruction.

    This use case provides an example of using current-grouping-key.

  8. Drag current-grouping-key to price. This function takes no arguments. For this use case, it returns the price of the group currently being processed.

    The Sources, Mapping canvas, and Target sections are shown. The source book element is connected to the target for-each-group element. The source price element is connected to the target group-by element. The source firstName element is connected to the target firstName element. The source lastName element is connected to the target lastName element. The current-grouping-key function has been dragged to the target price element. At the bottom, the Expression Builder shows the current-grouping-key() function.

  9. Click Code Mapper code icon to view the code of your mappings. You can edit the code, as necessary.
    <xsl:for-each-group xml:id="id_87" select="$stageReadEntire/nsmpr1:ReadResponse/ns26:bookstore/ns26:book" group-by="ns26:price">
          <ns22:book xml:id="id_68">
                <ns22:title xml:id="id_69" lang=""/>
                <ns22:price xml:id="id_73">
                      <xsl:value-of xml:id="id_96" select="fn:current-grouping-key ()"/>
                </ns22:price>
                <ns22:author xml:id="id_70">
                      <ns22:firstName xml:id="id_72">
                            <xsl:value-of xml:id="id_94" select="ns26:author/ns26:firstName"/>
                      </ns22:firstName>
                      <ns22:lastName xml:id="id_71">
                            <xsl:value-of xml:id="id_95" select="ns26:author/ns26:lastName"/>
                      </ns22:lastName>
                </ns22:author>
          </ns22:book>
    </xsl:for-each-group>

When you exit and return to the mapper, note that the for-each-group is no longer visible in designer view. You must select XSLT to see the for-each-group construct.