Remove namespace (and attached nodes) from a DOMDocument

@param \DOMDocument $dom Object.
@param string $namespace Namespace.
@return bool True/False.
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;
	}
657684
public static function isNameSpaceAllowed(string $namespace): bool
    {
        $fixedNS = [
            'http://www.w3.org/2001/XMLSchema-instance',
            'http://www.w3.org/XML/1998/namespace',
        ];
        foreach ($fixedNS as $ns) {
            if (0 === strcasecmp($ns, $namespace)) {
                return true;
            }
        }
        $willcardNS = [
            'http://www.sat.gob.mx/',
        ];
        foreach ($willcardNS as $ns) {
            if (0 === strpos($namespace, $ns)) {
                return true;
            }
        }
        return false;
    }
1003384
private function ExtractEnumerations(\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='enum']", 
                $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")
                    )
                );

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

                // Just extract named enumerations
                // Anonymous enumerations go on constants.json
                if($enum_name{0} != "@")
                {
                    $enum_values = $file_xpath->evaluate(
                        "enumvalue", 
                        $file_members->item($member)
                    );
                    
                    $enum_options = array();

                    for(
                        $enum_value = 0; 
                        $enum_value < $enum_values->length; 
                        $enum_value++
                    )
                    {
                        $enum_options[] = $file_xpath->evaluate(
                            "name", 
                            $enum_values->item($enum_value)
                        )->item(0)->nodeValue;
                    }
                    
                    $enumeration = new \Peg\Lib\Definitions\Element\Enumeration(
                        $enum_name,
                        $enum_options,
                        $enum_description
                    );
                    
                    $this->symbols->AddEnumeration(
                        $enumeration, 
                        $location, 
                        $namespace
                    );
                }
            }
        }
    }
372513
protected function namespaceAttrs($ele) {
        if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) {
            $this->xpath = new \DOMXPath($ele->ownerDocument);
        }

        foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {
            if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {
                $this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"');
            }
        }
    }
392526
public static function stripNamespacesFromXml($xml)
    {
        $xml = preg_replace('/<[a-zA-Z0-9]+:/','<',$xml);
        $xml = preg_replace('/<\/[a-zA-Z0-9]+:/','</',$xml);
        $xml = preg_replace('/ xmlns:[a-zA-Z0-9]+=".*">/','>',$xml);

        return $xml;
    }
1010763
protected function isXsl(DOMNode $node, $localName = null)
	{
		return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
	}
484357
private function ExtractConstants(\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='define'] | //memberdef[@kind='enum']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $kind = $file_members->item($member)->getAttribute("kind");

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

                if($kind == "define")
                {
                    $define_name = $file_xpath->evaluate(
                        "name", 
                        $file_members->item($member)
                    )->item(0)->nodeValue;
                    
                    $define_description = $file_xpath->evaluate(
                        "briefdescription", 
                        $file_members->item($member)
                    )->item(0)->nodeValue;

                    $define_initializer = true;

                    if(is_object($file_xpath->evaluate("initializer", $file_members->item($member))->item(0)))
                        $define_initializer = $file_xpath->evaluate(
                            "initializer", 
                            $file_members->item($member)
                        )->item(0)->nodeValue;
                    
                    else
                        $this->SendMessage(
                            t("Warning:") . " " 
                            . t("no initializer value for") 
                            . " #" . $define_name 
                        );

                    // Skip macro function defines
                    if($file_xpath->evaluate("param", $file_members->item($member))->length > 0)
                    {
                        continue;
                    }

                    // Skip defines used for compiler
                    if($define_name{0} == "_" && $define_name{1} == "_")
                    {
                        continue;
                    }

                    $constant = new \Peg\Lib\Definitions\Element\Constant(
                        $define_name,
                        $define_initializer,
                        $define_description
                    );
                    
                    $this->symbols->AddConstant(
                        $constant, 
                        $location, 
                        $namespace
                    );
                }

                // Also add anonymous enumerations as constants
                elseif($kind == "enum")
                {
                    $enum_name = $file_xpath->evaluate(
                        "name", 
                        $file_members->item($member)
                    )->item(0)->nodeValue;
                    
                    $enum_description = $file_xpath->evaluate(
                        "briefdescription", 
                        $file_members->item($member)
                    )->item(0)->nodeValue;

                    // Skip non anonymous enumerations
                    if($enum_name{0} != "@")
                        continue;

                    $enum_values = $file_xpath->evaluate(
                        "enumvalue", 
                        $file_members->item($member)
                    );

                    for($enum_value = 0; $enum_value < $enum_values->length; $enum_value++)
                    {
                        $enum_option = $file_xpath->evaluate(
                            "name", 
                            $enum_values->item($enum_value)
                        )->item(0)->nodeValue;
                        
                        $constant = new \Peg\Lib\Definitions\Element\Constant(
                            $enum_option,
                            1,
                            $enum_description
                        );

                        $this->symbols->AddConstant(
                            $constant, 
                            $location, 
                            $namespace
                        );
                    }
                }
            }
        }
    }
1049900
public void clearNamespaces( String elementName ) {
      if( StringUtils.isBlank( elementName ) ){
         rootNamespace.clear();
      }else{
         namespacesPerElement.remove( elementName );
      }
   }
1264124
public function removeChild( $nodeName )
	{
		$found		= false;
		$children	= array();
		foreach( $this->children as $child )
		{
			if( !$found && $child->getNodeName() == $nodeName )
			{
				$found	= TRUE;
				continue;
			}
			$children[] = $child;
		}
		if( $children == $this->children )
			return FALSE;
		$this->children = $children;
		return TRUE;
	}
1061283
public function removeNodeFromPublicWorkspace(RemoveEvent $event)
    {
        $document = $event->getDocument();
        $metadata = $this->metadataFactory->getMetadataForClass(get_class($document));
        if (!$metadata->getSyncRemoveLive()) {
            return;
        }

        $this->getLiveNode($document)->remove();
    }
385002
public void removeNamespace( String prefix, String elementName ) {
      if( prefix == null ){
         prefix = "";
      }
      if( StringUtils.isBlank( elementName ) ){
         rootNamespace.remove( prefix.trim() );
      }else{
         Map nameSpaces = (Map) namespacesPerElement.get( elementName );
         nameSpaces.remove( prefix );
      }
   }
1037266
public function isInsideNamespace($namespace)
    {
        $orig = preg_replace($search = ['!^\\\\!', '!\\\\$!'], ['', ''], $this->rc->getNamespaceName());
        $namespace = preg_replace($search, ['', ''], $namespace);

        return strpos($orig, $namespace) !== false;
    }
195485
def delete_namespace(self, namespace):
        

        # if not self.has_namespace(namespace):
        #     return "Namespace does not exists"

        # log = logging.getLogger("%s.%s" % (self.log_name,
        #                                    inspect.stack()[0][3]))
        # log.setLevel(self.log_level)

        url = self._make_url(namespace).replace("/sparql", "")
        result = requests.delete(url=url)
        if result.status_code == 200:
            log.critical(result.text)
            return result.text
        raise RuntimeError(result.text)
98268
final public function remove($varname = '', $namespace = 'default')
    {
        //if the registry is empty and the namespace is not default
        //delete the registry;
        //stores a value to a varname in a namespace of this session
        $session = $this;


        //echo $namespace;
        //@TODO, check if the regitry is not locked before adding
        if (!$session->isLocked($namespace)) {

            $registry = $session->getNamespace($namespace);
            $registry->delete($varname);
        }
        //If auth is locked

        return $session;
    }
1154951
public function canHandle(\DOMDocument $dom)
    {
        $result = false;
        $rootNodes = $dom->getElementsByTagName('rss');
        if ($rootNodes->length == 1) {
            $version = $rootNodes->item(0)->attributes->getNamedItem('version');
            $result = $version && fnmatch($this->getVersionPattern(), $version->nodeValue);
        }
        return $result;
    }
780596
protected function addXmlElement(\DOMNode $parentNode, string $nodeName, string $namespace): \DOMNode
    {
        $node = $parentNode->appendChild(
            ($parentNode->ownerDocument ?? $parentNode)->createElementNS($namespace, $nodeName)
        );

        return $node;
    }
343301
def del_ns(self, namespace):
        
        # remove the item from the namespace dict
        namespace = str(namespace)
        attr_name = None
        if hasattr(self, namespace):
            delattr(self, namespace)