Hi Jukka Iterating is what I am trying to avoid as it seems to be *expensive*. Honestly, I don't need to the node itself just the fact if it is last.
I was experimenting with some Xpaht queries and something like /jcr:root/library/image/banner/*[last()] Seems to work pretty good.... Now the question is - what is *faster* - iterating or xpath? What do you think? I do agree 2.0 should have support for sibling methods (isLast, isFirst, isOnly) Thanks Amir > -----Original Message----- > From: Jukka Zitting [mailto:[EMAIL PROTECTED] > Sent: Wednesday, August 08, 2007 5:42 AM > To: [email protected] > Subject: Re: Finding out if node is a last child of a parent > > Hi, > > On 8/8/07, Amir Mistric <[EMAIL PROTECTED]> wrote: > > - What is the *best* way to find out if a child node is the > last child > > of its parent (based on certain node type)? > > - Is there an API that already does this or would I have to > create my > > own method. > > - If I have to create my own method, what would you recommend? > > Do an Xpath query using last() or get a count of children > and compare > > last child node with passed in one? > > JCR doesn't have methods like Node.getNext() or > Node.getPrevious() that would allow you to do sideways traversal. > > I guess the best way to achieve your goal would be: > > Node node = ... > NodeIterator iterator = node.getParent().getNodes(); > while (iterator.hasNext()) { > if (node.isSame(iterator.nextNode())) { > if (iterator.hasNext()) { > return /* this is not the last sibling */; > } else { > return /* this is the last sibling */; > } > } > } > > This approach is obviously not too efficient, but can be used > to implement your use case as well as the "next"/"previous" > links discussed in another post a while ago. > > Perhaps we should ask for JSR 283 to consider adding support > for sibling access methods? > > BR, > > Jukka Zitting >
