code: public static String replaceDynamicNamespaces(String expression, Map<String, String> namespaces) {
          String expressionResult = expression;
          
          for (Entry<String, String> namespaceEntry : namespaces.entrySet()) {
              if (expressionResult.contains(DYNAMIC_NS_START + namespaceEntry.getValue() + DYNAMIC_NS_END)) {
                  expressionResult = expressionResult.replaceAll("\\" + DYNAMIC_NS_START + namespaceEntry.getValue().replace(".", "\\.") + "\\" + DYNAMIC_NS_END, 
                          namespaceEntry.getKey() + ":");
              }
          }
          
          return expressionResult;
      }
docs: Replaces all dynamic namespaces in a XPath expression with respective prefixes
  in namespace map.
  
  XPath: <code>/{http://sample.org/foo}foo/{http://sample.org/bar}bar</code>
  results in <code>/ns1:foo/ns2:bar</code> where the namespace map contains ns1 and ns2.
  
  @param expression
  @param namespaces
  @return