Java tutorial
//package com.java2s; import java.util.Iterator; import java.util.List; public class Main { /** * Appends the specified path token to the provided buffer followed by the * position specification of the target node in its siblings list (if * needed). * * @param node * the target node for the XPath expression. * @param siblings * the siblings of the target node. * @param pathToken * the path token identifying the target node. * @param buffer * the buffer to which appending the XPath sub-expression or * <code>null</code> if the method shall allocate a new buffer. * @return the XPath sub-expression to select the target node among its * siblings. */ private static StringBuilder getPositionPath(Object node, List<?> siblings, String pathToken, StringBuilder buffer) { buffer.append(pathToken); if (siblings != null) { int position = 0; final Iterator<?> i = siblings.iterator(); while (i.hasNext()) { position++; if (i.next() == node) break; } if (position > 1 || i.hasNext()) { // the item is not at the first location, ot there are more // locations. in other words, indexing is required. buffer.append('[').append(position).append(']'); } } return buffer; } }