I want to find the number of occurences of the bookname node inside the first occurence of bookslist. I am using XPAth here. I used the syntax as $count = $xp->find('count(//bookslist[1]/bookname)'); It is returning the value as 4 instead of 2. But when trying with $count = $xp->find('count(//book[1]/bookstore)'); or $count = $xp->find('count(//bookstore[1]/bookslist)'); it is returning the value correctly. I dont understand this problem. I dont want to give the full path with the names of the ancestors like $count = $xp->find('count(/book/bookstore/bookslist[1]/bookname)');