public List<Element> selectElementsMatchingXPath(String xpath,Element toSearch){
  NodeList nodelist;
  try {
    String xpathWithoutNamespace=xpath;
    Map<String,String> namespaces=namespaceContext.getNamespaces();
    if (namespaces != null && !namespaces.isEmpty() && !namespaceAware) {
      for (      Map.Entry<String,String> namespace : namespaces.entrySet()) {
        String key=namespace.getKey() + ":";
        while (xpathWithoutNamespace.contains(key)) {
          xpathWithoutNamespace=xpathWithoutNamespace.replace(key,"");
        }
      }
    }
    XPathExpression xPathExpr=xPath.compile(xpathWithoutNamespace);
    nodelist=(NodeList)xPathExpr.evaluate(toSearch,XPathConstants.NODESET);
  }
 catch (  Exception e) {
    throw new CargoException("Cannot evaluate XPath: " + xpath,e);
  }
  List<Element> result=new ArrayList<Element>(nodelist.getLength());
  for (int i=0; i < nodelist.getLength(); i++) {
    result.add((Element)nodelist.item(i));
  }
  return result;
}

public List < Element > selectElementsMatchingXPath ( String xpath , Element toSearch ) { NodeList nodelist ; try { String xpathWithoutNamespace = xpath ; Map < String , String > namespaces = namespaceContext . getNamespaces ( ) ; if ( namespaces != null && ! namespaces . isEmpty ( ) && ! namespaceAware ) { for ( Map . Entry < String , String > namespace : namespaces . entrySet ( ) ) { String key = namespace . getKey ( ) + ":" ; while ( xpathWithoutNamespace . contains ( key ) ) { xpathWithoutNamespace = xpathWithoutNamespace . replace ( key , "" ) ; } } } XPathExpression xPathExpr = xPath . compile ( xpathWithoutNamespace ) ; nodelist = ( NodeList ) xPathExpr . evaluate ( toSearch , XPathConstants . NODESET ) ; } catch ( Exception e ) { throw new CargoException ( "Cannot evaluate XPath: " + xpath , e ) ; } List < Element > result = new ArrayList < Element > ( nodelist . getLength ( ) ) ; for ( int i = 0 ; i < nodelist . getLength ( ) ; i ++ ) { result . add ( ( Element ) nodelist . item ( i ) ) ; } return result ; }
the following will search the given element for the specified xpath and return a list of nodes that match .
