Behavior of last()
-
Thursday, July 12, 2012 12:10 PM
Hi,
I have a question on using last() in XSL.
Say I have the XML like:
<Tables>
<Table>
<F1>1</F1>
<F2>2</F2>
</Table>
<Table>
<F1>3</F1>
<F2>4</F2>
</Table>
<Table>
<F1>5</F1>
<F2>6</F2>
</Table>
Now I always want to choose the values from the last set of Table.
So I write - \\Tables\Table\F1[last()] simillarly for F2 as well. This works correctly. But at times in the last set of Table F2 can be missing which means last Table will not have F2 at all and the last but one table might have F2. the above method to pull the F2 fails here - If the last set of Table does not have F2 and if the previous set of Table has - it pulls that value. How can I avoid that? Could you please help.
I want to pull the values only from the last set of Table.
I need this as a variable for each element within last set ot Table.
eg: <xsl:variable name="ValF1" select="\\Tables\Table\F1[last()]"/> and so on..
Rpaul
All Replies
-
Thursday, July 12, 2012 12:20 PM
Well first of all XPath uses forwards slashes "/" to separate steps, not backslashes.
And
//Tables/Table/F1[last()]
in my understand selects the last F1 child element of all Table child elements, as the predicate is applied on the step.
So basically if you want the last F1 element in the complete document you need
(/Tables/Table/F1)[last()]
If you want the F1 child element of the last Table element you need
/Tables/Table[last()]/F1
MVP Data Platform Development My blog
- Marked As Answer by RPaul18 Thursday, July 12, 2012 1:25 PM
-
Thursday, July 12, 2012 1:26 PMThanks very much Martin, your suggestion worked.
Rpaul

