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 afor-each
constructor). - You can only drop the
for-each-group
as a parent in a target tree node (same as afor-each
constructor). - When you drop a
for-each-group
on a target node, a childgroup-by
attribute node is created. - The Delete node option is disabled for the
group-by
attribute node. - When you delete a
for-each-group
, thegroup-by
attribute node is also deleted. - If you are using an attribute other than
group-by
in code mode (such asgroup-adjacent
,group-starting-with
, orgroup-ending-with
), you cannot switch to designer mode.
Iterate Across Groups
- Click XSLT.
- In the upper right corner, click Toggle functions
.
- Expand XSL Constructors, and then
Flow Control.
For this use case, the mapper shows the following source and target elements.
- Drag a for-each-group to the target parent
book element. The element must be a repeating node.
The for-each-group constructor includes the group-by attribute.
- Connect the book source element to
for-each-group.
- Connect the price source element to
group-by to iterate on the basis of book price.
- Drag the source element firstName to the
target element firstName and the source element
lastName to the target element
lastName.
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.
- 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.
- Click Code
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.