I’m using the last() function to try to get the last entry in my xml as so:
//Name[last()]
however, it is returning the list to me rather than just the last name in the document… any suggestions?
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a common gotcha due to what “//” stands for. Many people assume it stands for the descendant axis, but it actually stands for
/descendant-or-self::node()/.//Name[last()]is short for/descendant-or-self::node()/Name[last()], which makes more obvious the fact that this expression actually contains two steps:/descendant-or-self::node(), which selects the context node and all its descendants, andName[last()], which selects the last<Name>child of the context node.Step #2 is evaluated once for each node returned by step 1. If step 1 returns 10 nodes, than step 2 (and thus the entire expression) can return up to 10 nodes as well.
If you instead encapsulate the expression with parentheses, you can use an “expression filter” rather than a “step filter”:
(//Name)[last()]This ensures that the entire expression will return no more than 1
<Name>element, specifically, the last one in document order among all the elements returned by the expression to its left.For more details and nuances, check out this email thread: http://markmail.org/thread/otd3iz5lag72emq4#query:+page:1+mid:qdsrmjrbbdvve3c3+state:results
And this article: http://developer.marklogic.com/blog/xpath-punctuation-part-5