Sudha Rani
Sudha Rani

Reputation: 89

Are there any function to access the first nodes in the xml while using group by

I have a xml where same employee id can appear multiple times but in my output file I just need the person to show once.In addition to that I have a field in the xml called "YTD gross " for which I need the value to be picked from the first occurnece of that person record in the xml. I know that if I need to access the value from last occurence of that person record in the xml I need to use the last function. Likewise is there anyfunction that will pick the value only form the first occurmence of that person in the record ? Example of the usage of the last function is below, I need some function to pick the first occurence..

<xsl:value-of select='os:formatAmount1(current-group()[last()]/YTD_Gross)'/>

Upvotes: 1

Views: 128

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117003

is there anyfunction that will pick the value only form the first occurmence of that person

Within xsl:for-each-group, the context item is the initial item of the relevant group - so to get some value from the first (in document order) item in the current group you can write shortly:

<xsl:value-of select="SomeValue"/>

Which is the equivalent of:

<xsl:value-of select="current-group()[1]/SomeValue"/>

Upvotes: 2

dret
dret

Reputation: 549

last() simply returns the position of the last item of the current context, as an integer.

https://www.w3.org/TR/xpath-functions/#func-last

This means that by using 1 instead of last(), you can achieve what you want: select the first item from the context.

Technically, what happens when a predicate (the [...] part) evaluates to a number, it gets compared to the context position of each of the context items, and only those where the numbers match get selected. This is the reason why 1 will get you the first item, and why last() gets you the last one.

https://www.w3.org/TR/xpath/#predicates (third paragraph)

Upvotes: 1

Related Questions