if (cmp > 0) {
if (cmp > 0 && candidate.getNodeId().getTreeLevel() <= reference.getNodeId().getTreeLevel()) {
    public static NodeSet selectPrecedingSiblings(NodeSet candidates,            NodeSet references, int contextId) {        if (references.isEmpty() || candidates.isEmpty())            return NodeSet.EMPTY_SET;        NodeSet result = new ExtArrayNodeSet();        NodeSetIterator iReferences = references.iterator();        NodeSetIterator iCandidates = candidates.iterator();        NodeProxy reference = (NodeProxy) iReferences.next();        NodeProxy candidate = (NodeProxy) iCandidates.next();        NodeProxy firstCandidate = null;        while (true) {            // first, try to find nodes belonging to the same doc            if (reference.getDocument().getDocId() < candidate.getDocument()                    .getDocId()) {                firstCandidate = null;                if (iReferences.hasNext())                    reference = (NodeProxy) iReferences.next();                else                    break;            } else if (reference.getDocument().getDocId() > candidate                    .getDocument().getDocId()) {                firstCandidate = null;                if (iCandidates.hasNext())                    candidate = (NodeProxy) iCandidates.next();                else                    break;            } else {                // same document: check if the nodes have the same parent                int cmp = candidate.getNodeId().getParentId().compareTo(reference.getNodeId().getParentId());                if (cmp > 0) {                    // wrong parent: proceed                    firstCandidate = null;                    if (iReferences.hasNext())                        reference = (NodeProxy) iReferences.next();                    else                        break;                } else if (cmp < 0) {                    // wrong parent: proceed                    firstCandidate = null;                    if (iCandidates.hasNext())                        candidate = (NodeProxy) iCandidates.next();                    else                        break;                } else {                    if (firstCandidate == null)                        firstCandidate = candidate;                                        // found two nodes with the same parent                    // now, compare the ids: a node is a following sibling                    // if its id is greater than the id of the other node                    cmp = candidate.getNodeId().compareTo(reference.getNodeId());                    if (cmp < 0) {                        // found a preceding sibling                        NodeProxy t = result.get(candidate);                        if (t == null) {                            if (Expression.IGNORE_CONTEXT != contextId) {                                if (Expression.NO_CONTEXT_ID == contextId) {                                    candidate.copyContext(reference);                                } else {                                    candidate.addContextNode(contextId,                                            reference);                                }                            }                            result.add(candidate);                        } else if (contextId > Expression.NO_CONTEXT_ID){                            t.addContextNode(contextId, reference);                        }                        if (iCandidates.hasNext())                            candidate = (NodeProxy) iCandidates.next();                        else                            break;                    } else if (cmp > 0) {                        // found a following sibling                        if (iCandidates.hasNext())                            // TODO : break ?                            candidate = (NodeProxy) iCandidates.next();                        else                            break;                        // equal nodes: proceed with next node                    } else {                        if (iReferences.hasNext()) {                            reference = (NodeProxy) iReferences.next();                            iCandidates.setPosition(firstCandidate);                            candidate = (NodeProxy) iCandidates.next();                        } else                            break;                    }                }            }        }        return result;    }