last
Returns the number of items in the sequence of items currently being processed. Importantly, the sequence of items is determined by the current mapping context, as described in the example below.
Languages
XQuery, XSLT 2.0, XSLT 3.0.
Example
Let's suppose that you have the following source XML file:
<Articles> |
Your goal is to copy data to an XML file with a different schema. Also, the count of all items must be saved to the target XML file. This can be achieved by a mapping like the one below:
In the example above, the last function returns the position of the last node in the current parent context and populates the count attribute with value 3.
<items count="3"> |
Note that value 3 is the position of the last item (and thus the count of all items) in the mapping context created by the connection between Article and items. If this connection did not exist, items would still be copied to the target, but the last function would return value 1 incorrectly, because it would have no parent context to iterate over. (More precisely, it would use the default implicit context created between the root items of both components, which produces a sequence of 1 item, not 3 as expected).
It is generally advisable to use the count function from the core library instead of the last function, because the former has a parent-context argument, which enables you to alter the mapping context explicitly.