#Richard H. Tingstad
_xpath_complete() {
    [ 2 -ne $COMP_CWORD ] && return
    local cur=${COMP_WORDS[COMP_CWORD]} path words
    if [ "" == "$cur" ];then
       path="/*";
    elif egrep -q '/$' <<< "$cur" ;then
       path="$cur*"
    else
        path=$( sed -r 's|/[^/]+$|/*|' <<< "$cur" )
    fi
    words=$( xpath ${COMP_WORDS[1]} "$path" "' '" | cut -f1 | sed 's|$|/|' )
    COMPREPLY=( $( compgen -W "$words" -- "$cur" ) )
}
complete -F _xpath_complete -o default -o nospace xpath
