public static String getXPathFor ( final Node aNode ) { List < String > nodeNameList = new ArrayList < String > ( ) ; for ( Node current = aNode ; current != null ; current = current . getParentNode ( ) ) { final String currentNodeName = current . getNodeName ( ) ; final String nameAttribute = DomHelper . getNameAttribute ( current ) ; if ( currentNodeName . toLowerCase ( ) . endsWith ( "enumeration" ) ) { // We should print the "value" attribute here. nodeNameList . add ( currentNodeName + "[@value='" + getValueAttribute ( current ) + "']" ) ; } else if ( nameAttribute == null ) { // Just emit the node's name. nodeNameList . add ( current . getNodeName ( ) ) ; } else { // We should print the "name" attribute here. nodeNameList . add ( current . getNodeName ( ) + "[@name='" + nameAttribute + "']" ) ; } } StringBuilder builder = new StringBuilder ( ) ; for ( ListIterator < String > it = nodeNameList . listIterator ( nodeNameList . size ( ) ) ; it . hasPrevious ( ) ; ) { builder . append ( it . previous ( ) ) ; if ( it . hasPrevious ( ) ) { builder . append ( "/" ) ; } } return builder . toString ( ) ; }
Retrieves the XPath for the supplied Node within its document .