public function extractNamespacesFromQuery($query)
    {
        preg_match_all('/\<([a-zA-Z\\\.\/\-\#\:0-9]+)\>/', $query, $matches);

        $uris = [];

        // only use URI until the last occurence of one of these chars: # /
        foreach ($matches[1] as $match) {
            $hashPos = strrpos($match, '#');

            // check for last #
            if (false !== $hashPos) {
                $uri = substr($match, 0, $hashPos + 1);
            } else {
                if (7 < strlen($match)) {
                    $slashPos = strrpos($match, '/', 7);

                    // check for last /
                    if (false !== $slashPos) {
                        $uri = substr($match, 0, $slashPos + 1);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }

            $uris[$uri] = $uri;
        }

        $uris = array_values($uris);
        $prefixes = [];
        $uriSet = false;
        $prefixNumber = 0;

        foreach ($uris as $uri) {
            $uriSet = false;
            // go through common namespaces and try to find according prefix for
            // current URI
            foreach ($this->commonNamespaces as $prefix => $_uri) {
                if ($uri == $_uri) {
                    $prefixes[$prefix] = $uri;
                    $uriSet = true;
                    break;
                }
            }
            // in case, it couldnt find according prefix, generate one
            if (false === $uriSet) {
                $prefixes['ns-'.$prefixNumber++] = $uri;
            }
        }

        return $prefixes;
    }