tags:

views:

73

answers:

2

Hello,

I'm trying to figure out how to know if a node is the last child of another, or not. I know how to retrieve the parent's lastChild name ($node->parentNode->lastChild->nodeName), but as sometimes there's many same node names within the same parent, the name is definitly not the way to find the last child.

I'm looking for some kind of iterator which can retrieve the number of childs, and depending of the position of the current node, tell if the current node is the last child.

Thanks!

+1  A: 

In pure DOM terms, if the child is the last child of a parent, its nextSibling property will be null.

Javascript example (not that you're looking for Javascript specifically):

if (!child.nextSibling) {
    // It's the last child
}
T.J. Crowder
yeah that's a good solution, thank you very much :)
Bacon
@Bacon: Cool! Sorry if I misread the client-side thing, but at least the DOM is the DOM regardless...
T.J. Crowder
You could also do _$node === $node->parentNode->lastChild_
chris
A: 

Since this is a PHP question, I'll leave a PHP answer (using DOM) for the posterity:

if ($node->isSameNode($node->parentNode->lastChild))
{
    //...
}

And if you're using SimpleXML, just convert your node with dom_import_simplexml().

Josh Davis
sorry for the late coment, but I'd like to thank you for the tips ;)
Bacon