I'm wondering is there any way to create a global counter in a XSL transformation..
The case is that currently I'm using a nested loop to group and loop over orders and items. Here is a sample input:
<shipment>
<shipmentid>1111222</shipmentid>
<context>TEST</context>
<delivery>
<deliveryItem>
<deliveryNumber>123</deliveryNumber>
<deliveryDate>...</deliveryDate>
<arrivalDate>...</arrivalDate>
<customerOrderNumber>X999</customerOrderNumber>
<customerItemCode>MC423</customerItemCode>
</deliveryItem>
<deliveryItem>
<deliveryNumber>123</deliveryNumber>
<deliveryDate>...</deliveryDate>
<arrivalDate>...</arrivalDate>
<customerOrderNumber>X877</customerOrderNumber>
<customerItemCode>AY642</customerItemCode>
</deliveryItem>
<deliveryItem>
<deliveryNumber>123</deliveryNumber>
<deliveryDate>...</deliveryDate>
<arrivalDate>...</arrivalDate>
<customerOrderNumber>X315</customerOrderNumber>
<customerItemCode>XY223</customerItemCode>
</deliveryItem>
<deliveryItem>
<deliveryNumber>123</deliveryNumber>
<deliveryDate>...</deliveryDate>
<arrivalDate>...</arrivalDate>
<customerOrderNumber>X315</customerOrderNumber>
<customerItemCode>MM647</customerItemCode>
</deliveryItem>
</delivery>
</shipment>
The expected output should look somewhat like this (not taking in any of the details):
<Order>
<ControlHeader>HL</ControlHeader>
<HL01>1</HL01>
</Order>
<Item>
<ItemHierarchicalLevel>
<ControlHeader>HL</ControlHeader>
<HL01>2</HL01>
</ItemHierarchicalLevel>
</Item>
<Item>
<ItemHierarchicalLevel>
<ControlHeader>HL</ControlHeader>
<HL01>3</HL01>
</ItemHierarchicalLevel>
</Item>
<Item>
<ItemHierarchicalLevel>
<ControlHeader>HL</ControlHeader>
<HL01>4</HL01>
</ItemHierarchicalLevel>
</Item>
<Order>
<ControlHeader>HL</ControlHeader>
<HL01>5</HL01>
</Order>
<Item>
<ItemHierarchicalLevel>
<ControlHeader>HL</ControlHeader>
<HL01>6</HL01>
</ItemHierarchicalLevel>
</Item>
And currently this is the structure for the nested for-each I'm using, to group the orders and output line details:
<xsl:for-each-group select="/shipment/delivery/deliveryItem" group-by="customerOrderNumber">
<tns:Order>
<tns:ControlHeader>HL</tns:ControlHeader>
<tns:HL01>
</tns:HL01>
</tns:Order>
<xsl:for-each select="current-group()">
<tns:Item>
<tns:ItemHierarchicalLevel>
<tns:ControlHeader>HL</tns:ControlHeader>
<tns:HL01>
</tns:HL01>
</tns:ItemHierarchicalLevel>
</tns:Item>
</xsl:for-each>
</xsl:for-each-group>
Is there any way to create a numeral sequence I could use in the HL01 field, that is the same for both Order and Item loop and continues incrementing with each iteration never resetting? The position() function can not be used , since it is a different value in each of the loops.
To answer this on a slightly different level: remember that XSLT is a functional language without global mutable variables, so the concept of a "global counter" is completely alien to the language. The mechanisms for numbering therefore rely on functional mechanisms:
xsl:numberexamines the position of a given node with the input tree,position()returns the position of an item within the sequence of items you are processing. So the simple answer to your question is "no"; you have to find a different solution to your requirement.