Returns all the values of a list of nodes under a given node.
The subnodes are searched by using a XPATH query relative to the document containing the $node.

@param DOMNode $node
@param string $xpath_query
@param array $namespaces
@return array of string
334653
public static function getAllValuesByXpath($node, $xpath_query, $namespaces = array())
    {
        $node_list = XMLTool :: getAllElementsByXpath($node, $xpath_query, $namespaces);

        $values = array();

        if (isset($node_list))
        {
            foreach ($node_list as $node_found)
            {
                $values[] = $node_found->nodeValue;
            }
        }

        return $values;
    }
749687
protected function buildNamespaceTree(\DOMDocument $dom)
    {
        $xpath = new \DOMXPath($dom);
        $namespaces = array();
        $qry = $xpath->query('//@namespace');
        for ($i = 0; $i < $qry->length; $i++) {
            if (isset($namespaces[$qry->item($i)->nodeValue])) {
                continue;
            }

            $namespaces[$qry->item($i)->nodeValue] = true;
        }

        $namespaces = $this->generateNamespaceTree(array_keys($namespaces));
        $this->generateNamespaceElements($namespaces, $dom->documentElement);
    }
417567
public function getPreloadedNodes(bool $andClear = true, string $namespace = NcrPreloader::DEFAULT_NAMESPACE): array
    {
        $nodes = $this->ncrPreloader->getNodes(null, $namespace);
        if ($andClear) {
            $this->ncrPreloader->clear($namespace);
        }

        return $nodes;
    }
593943
private function ExtractFunctions(\DOMDocument $document, $namespace = "")
    {
        $xpath = new DOMXPath($document);

        $entries = $xpath->evaluate(
            "//compound[@kind='file'] | //compound[@kind='namespace']", 
            $document
        );
        
        // First we store all functions and their overloads, after
        // we use this array to create FunctionElement objects and add
        // them to the symbols object.
        $functions = array();

        for($i = 0; $i < $entries->length; $i++)
        {
            $kind = $entries->item($i)->getAttribute("kind");
            $refid = $entries->item($i)->getAttribute("refid");
            $name = $entries->item($i)->childNodes->item(0)->nodeValue;

            if($kind == "namespace")
                $namespace = str_replace("::", "\\", $name);
            else
                $namespace = "\\";

            $file_doc = new DOMDocument();
            $file_doc->load($this->definitions_path . "/$refid.xml");

            $file_xpath = new DOMXPath($file_doc);

            $file_members = $file_xpath->evaluate(
                "//memberdef[@kind='function']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $location = str_replace(
                    $this->headers_path, "", str_replace(
                        "\\", 
                        "/", 
                        $file_xpath->evaluate(
                            "location", 
                            $file_members->item($member)
                        )->item(0)->getAttribute("file")
                    )
                );

                $function_name = $file_xpath->evaluate(
                    "name", 
                    $file_members->item($member)
                )->item(0)->nodeValue;

                $function_type = $file_xpath->evaluate(
                    "type", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $function_description = trim(
                    $file_xpath->evaluate(
                        "briefdescription", 
                        $file_members->item($member)
                    )->item(0)->nodeValue
                );
                
                $function_type = str_replace(
                    array(" *", " &"), 
                    array("*", "&"), 
                    $function_type
                );

                $function_overload = new \Peg\Lib\Definitions\Element\Overload(
                    $function_description
                );
                
                $function_overload->SetReturnType(
                    new \Peg\Lib\Definitions\Element\ReturnType(
                        $function_type
                    )
                );
                
                // Retrieve all function parameters
                $function_parameters = $file_xpath->evaluate(
                    "param", 
                    $file_members->item($member)
                );

                $parameters = array();

                for(
                    $function_parameter = 0; 
                    $function_parameter < $function_parameters->length; 
                    $function_parameter++
                )
                {
                    // Default generated param name in case it doesn't have one.
                   $param_name = "param" . $function_parameter;

                    if(is_object($file_xpath->evaluate("declname", $function_parameters->item($function_parameter))->item(0)))
                    {
                        $param_name = $file_xpath->evaluate(
                            "declname", 
                            $function_parameters->item(
                                $function_parameter
                            ))->item(0)->nodeValue
                        ;
                    }
                    else
                    {
                        $this->SendMessage(t("Skipping:") . " " . t("function") 
                            . " '" . $function_name . "' " 
                            . t("seems to be a macro with undocumented parameter types.") 
                        );

                        continue 2;
                    }

                    $param_type = str_replace(
                        array(" *", " &"), 
                        array("*", "&"), 
                        $file_xpath->evaluate(
                            "type", 
                            $function_parameters->item($function_parameter)
                        )->item(0)->nodeValue
                    );

                    // Check if parameter is array
                    $param_array = false;
                    if($file_xpath->evaluate("array", $function_parameters->item($function_parameter))->length > 0)
                    {
                        $array_value = $file_xpath->evaluate(
                            "array", 
                            $function_parameters->item(
                                $function_parameter)
                            )->item(0)->nodeValue
                        ;

                        if($array_value == "[]")
                        {
                            $param_array = true;
                        }
                        else
                        {
                            //$parameters[$function_parameter]["extra"] = $array_value;
                        }
                    }

                    // Get default value
                    $param_value = "";
                    if($file_xpath->evaluate("defval", $function_parameters->item($function_parameter))->length > 0)
                    {
                        $param_value = $file_xpath->evaluate(
                            "defval", 
                            $function_parameters->item(
                                $function_parameter)
                            )->item(0)->nodeValue
                        ;
                    }
                    
                    $parameter = new \Peg\Lib\Definitions\Element\Parameter(
                        $param_name, 
                        $param_type, 
                        $param_value
                    );
                    
                    $parameter->is_array = $param_array;
                    
                    $function_overload->AddParameter($parameter);
                }

                $functions[$function_name][] = [
                    $location,
                    $namespace,
                    $function_overload
                ];
            }
        }
        
        foreach($functions as $function_name=>$function_overloads)
        {
            $function = new \Peg\Lib\Definitions\Element\FunctionElement(
                $function_name
            );
            
            $header = "";
            $namespace = "";
            
            foreach($function_overloads as $overload)
            {
                if(!$header)
                {
                    $header = $overload[0];
                    $namespace = $overload[1];
                }
                
                $function->AddOverload($overload[2]);
            }
            
            $this->symbols->AddFunction(
                $function, 
                $header,
                $namespace
            );
        }
    }
624625
public function ElementsByNamespaces( $namespaces )
	{
		if ( is_string( $namespaces ) )
			$namespaces = array_map( 'trim', explode( ",", $namespaces ) );

		if ( ! is_array( $namespaces ) )
			throw new Exception( 'InstanceElementsFilter::ElementsByName $namespaces must be a string or array' );

		$namespaces = array_flip( array_map( 'strtolower', $namespaces ) );

		$instance_namespaces = $this->instance->getInstanceNamespaces();

		// In PHP 7.0 $this is passed to closure functions automatically but not in earlier versions
		$elements = &$this->elements;

		$filtered = array_reduce( array_keys( $this->elements ), function( $carry, $elementName ) use( &$elements, $instance_namespaces, $namespaces ) {

			$namespaceElements = array_filter( $elements[ $elementName ], function( $entry ) use( &$elements, $instance_namespaces, $namespaces ) {
				$namespace = isset( $instance_namespaces[ $entry['namespace'] ] ) ? $instance_namespaces[ $entry['namespace'] ] : $entry['namespace'];
				return isset( $namespaces[ strtolower( $namespace ) ] );
			} );

			if ( ! $namespaceElements ) return $carry;

			$carry[ $elementName ] = $namespaceElements;

			return $carry;

		}, array() );

		return new InstanceElementsFilter( $this->instance, $filtered );
	}
586993
public function getListNodes($nodes)
    {
        $xPath = new DOMXPath($this->getDomXml());
        $nodeList = $xPath->query("//SUBMENU[@cl='" . implode("' or @cl='", $nodes) . "']");

        return ($nodeList->length) ? $nodeList : null;
    }
455379
public function getNamespaceMappings()
	{
		$dom = (new XmlDocumentBuilder())->buildDocument($this);
		$xpath = new \DOMXPath($dom);
		
		$namespaces = [];
		
		foreach($xpath->query('namespace::*', $dom->documentElement) as $result)
		{
			$namespaces[(string)$result->prefix] = (string)$result->nodeValue;
		}
		
		return $namespaces;
	}
612594
private function ExtractTypeDefinitions(\DOMDocument $document, $namespace = "")
    {
        $xpath = new DOMXPath($document);

        $entries = $xpath->evaluate(
            "//compound[@kind='file'] | //compound[@kind='namespace']", 
            $document
        );

        for($i = 0; $i < $entries->length; $i++)
        {
            $kind = $entries->item($i)->getAttribute("kind");
            $refid = $entries->item($i)->getAttribute("refid");
            $name = $entries->item($i)->childNodes->item(0)->nodeValue;

            if($kind == "namespace")
                $namespace = str_replace("::", "\\", $name);
            else
                $namespace = "\\";

            $file_doc = new DOMDocument();
            $file_doc->load($this->definitions_path . "/$refid.xml");

            $file_xpath = new DOMXPath($file_doc);

            $file_members = $file_xpath->evaluate(
                "//memberdef[@kind='typedef']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $location = str_replace(
                    $this->headers_path, 
                    "", 
                    str_replace(
                        "\\", 
                        "/", 
                        $file_xpath->evaluate(
                            "location", 
                            $file_members->item($member)
                        )->item(0)->getAttribute("file")
                    )
                );

                $this->includes[$location] = true;

                $typedef_name = $file_xpath->evaluate(
                    "name", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $typedef_type = $file_xpath->evaluate(
                    "type", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $typedef_description = $file_xpath->evaluate(
                    "briefdescription", 
                    $file_members->item($member)
                )->item(0)->nodeValue;

                $typedef = new \Peg\Lib\Definitions\Element\TypeDef(
                    $typedef_name, 
                    $typedef_type, 
                    $typedef_description
                );
                
                $this->symbols->AddTypeDef(
                    $typedef, 
                    $location, 
                    $namespace
                );
            }
        }
    }
169488
public static function remove_namespace(\DOMDocument $dom, string $namespace) {
		if (! $namespace) {
			return false;
		}

		$xpath = new \DOMXPath($dom);
		$nodes = $xpath->query("//*[namespace::{$namespace} and not(../namespace::{$namespace})]");
		for ($x = 0; $x < $nodes->length; ++$x) {
			$node = $nodes->item($x);
			$node->removeAttributeNS(
				$node->lookupNamespaceURI($namespace),
				$namespace
			);
		}

		return true;
	}
1247289
protected function generateNamespaceTree($namespaces)
    {
        sort($namespaces);

        $result = array();
        foreach ($namespaces as $namespace) {
            if (!$namespace) {
                $namespace = 'global';
            }

            $namespace_list = explode('\\', $namespace);

            $node = &$result;
            foreach ($namespace_list as $singular) {
                if (!isset($node[$singular])) {
                    $node[$singular] = array();
                }

                $node = &$node[$singular];
            }
        }

        return $result;
    }
374178
public function query($css_selector) {
		$xpath_selector	= $this->_css_toxpath_selector($css_selector);
		$nodelist		= $this->xpath->query($xpath_selector);
		return $nodelist;
	}
425825
public function query( $path, $node = NULL )
	{
		if( !$this->xPath )
			throw new Exception( 'No XML loaded yet.' );
		if( $node )
			$nodeList	= $this->xPath->query( $path, $node );
		else
			$nodeList	= $this->xPath->query( $path );
		return $nodeList;
	}
1148764
public function findXPath(string $xpath): NodeList {
        $results = $this->newNodeList();

        if ($this->isRemoved()) {
            return $results;
        }

        $domxpath = new \DOMXPath($this->document());

        foreach ($this->collection() as $node) {
            $results = $results->merge(
                $node->newNodeList($domxpath->query($xpath, $node))
            );
        }

        return $results;
    }
970942
public static function query($dom, $query, $context = null)
    {
        $xpath = new DOMXPath($dom);
        $xpath->registerNamespace('samlp', OneLogin_Saml2_Constants::NS_SAMLP);
        $xpath->registerNamespace('saml', OneLogin_Saml2_Constants::NS_SAML);
        $xpath->registerNamespace('ds', OneLogin_Saml2_Constants::NS_DS);
        $xpath->registerNamespace('xenc', OneLogin_Saml2_Constants::NS_XENC);
        $xpath->registerNamespace('xsi', OneLogin_Saml2_Constants::NS_XSI);
        $xpath->registerNamespace('xs', OneLogin_Saml2_Constants::NS_XS);
        $xpath->registerNamespace('md', OneLogin_Saml2_Constants::NS_MD);

        if (isset($context)) {
            $res = $xpath->query($query, $context);
        } else {
            $res = $xpath->query($query);
        }
        return $res;
    }
19178
public function beforeTraverse(array $nodes)
    {
        if (empty($nodes)) {
            return $nodes;
        }

        $last = \end($nodes);

        if ($last instanceof Namespace_) {
            $kind = $last->getAttribute('kind');

            // Treat all namespace statements pre-PHP-Parser v3.1.2 as "open",
            // even though we really have no way of knowing.
            if ($kind === null || $kind === Namespace_::KIND_SEMICOLON) {
                // Save the current namespace for open namespaces
                $this->setNamespace($last->name);
            } else {
                // Clear the current namespace after a braced namespace
                $this->setNamespace(null);
            }

            return $nodes;
        }

        return $this->namespace ? [new Namespace_($this->namespace, $nodes)] : $nodes;
    }
116644
private function loadFromExtensions(\DOMDocument $xml)
    {
        foreach ($xml->documentElement->childNodes as $node) {
            if (!$node instanceof \DOMElement || self::NS === $node->namespaceURI) {
                continue;
            }

            $values = static::convertDomElementToArray($node);
            if (!is_array($values)) {
                $values = array();
            }

            $this->container->loadFromExtension($node->namespaceURI, $values);
        }
    }
102809
public static NodeList xpathNodeList(Node document, String xpathExpression, Map<String, String> namespaceMapping) throws XPathException,
                MarshallingException
    {
        return (NodeList) executeXPath(document, xpathExpression, namespaceMapping, XPathConstants.NODESET);
    }