clean empty nodes from tree

@param object $dom         dom object
@param string $parentXPath parent xpath
@param string $childXPath  child xpath from parent
432116
protected function _cleanEmptyParents($dom, $parentXPath, $childXPath)
    {
        $xPath = new DomXPath($dom);
        $nodeList = $xPath->query($parentXPath);

        foreach ($nodeList as $node) {
            $id = $node->getAttribute('id');
            $childList = $xPath->query("{$parentXPath}[@id='$id']/$childXPath");
            if (!$childList->length) {
                $node->parentNode->removeChild($node);
            }
        }
    }
1061999
private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
    {
        foreach ($importedParent->childNodes as $childNode) {
            $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
        }
    }
51071
private function loadTag(\DOMXPath $xpath, \DOMNode $node)
    {
        $tag = [
            'name' => null,
            'priority' => null,
            'attributes' => [],
        ];

        foreach ($node->attributes as $key => $attr) {
            if (in_array($key, ['name', 'priority'])) {
                $tag[$key] = $attr->value;
            } else {
                $tag['attributes'][$key] = $attr->value;
            }
        }

        return $tag;
    }
485163
public static function xpath(DOMDocument $dom, $path) {
    $xpath = new DOMXPath($dom);
    
    $ret = array();
    foreach ($xpath->query($path) as $node) {
      $ret[] = $node;
    }
    return $ret;
  }
114890
public function toXML(DOMNode $parentElement = null) : DOMElement
    {
        if ($parentElement === null) {
            $document = DOMDocumentFactory::create();
            $parentElement = $document;
        } else {
            $document = $parentElement->ownerDocument;
        }

        $root = $document->createElementNS(Constants::NS_SAML, 'saml:'.'EncryptedAssertion');
        $parentElement->appendChild($root);

        $root->appendChild($document->importNode($this->encryptedData, true));

        return $root;
    }
139613
protected function loadElementsFromXmlFileWithPath($xmlFile, $nodePath)
    {
        $xml = file_get_contents($xmlFile);
        // Remove whitespaces between closing tag and opening tag.
        $xml = preg_replace('/>\s+</', '><', $xml);

        $doc = new \DOMDocument();
        $doc->loadXML($xml);

        $xpath = new \DOMXpath($doc);
        $elements = $xpath->query($nodePath);

        return $elements;
    }
900533
protected function startClean(\DOMNodeList $elements)
    {
        // loop through all elements
        // we do this backwards so we don't skip anything if we delete a node
        // see comments at: http://php.net/manual/en/class.domnamednodemap.php
        for ($i = $elements->length - 1; $i >= 0; $i--) {
            $currentElement = $elements->item($i);

            // If the tag isn't in the whitelist, remove it and continue with next iteration
            if (!in_array(strtolower($currentElement->tagName), $this->allowedTags)) {
                $currentElement->parentNode->removeChild($currentElement);
                $this->xmlIssues[] = array(
                    'message' => 'Suspicious tag \'' . $currentElement->tagName . '\'',
                    'line' => $currentElement->getLineNo(),
		);
                continue;
            }

            $this->cleanAttributesOnWhitelist($currentElement);

            $this->cleanXlinkHrefs($currentElement);

            $this->cleanHrefs($currentElement);

            if (strtolower($currentElement->tagName) === 'use') {
                if ($this->isUseTagDirty($currentElement)) {
                    $currentElement->parentNode->removeChild($currentElement);
                    $this->xmlIssues[] = array(
                        'message' => 'Suspicious \'' . $currentElement->tagName . '\'',
			'line' => $currentElement->getLineNo(),
                    );
                    continue;
                }
            }
        }
    }
826532
private function filterRelativeXPath($xpath)
    {
        $prefixes = $this->findNamespacePrefixes($xpath);

        $crawler = $this->createSubCrawler(null);

        foreach ($this->nodes as $node) {
            $domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);
            $crawler->add($domxpath->query($xpath, $node));
        }

        return $crawler;
    }
316843
static function xpathQuery($html, $xpath) {
		$ddoc = new \DOMDocument();
		@$ddoc->loadHTML($html);
	
		$dxpath = new \DOMXPath($ddoc);
		return $dxpath->query($xpath);
	}
1629
function _cleanNodes (nodes) {
  nodes.forEach(function (tag) {
    // avoiding circular structure
    delete tag._parent;

    // removing temporary tester
    delete tag.match_closer;

    // cleaning empty attributes
    if( tag.attrs && Object.keys(tag.attrs).length === 0 ) delete tag.attrs;

    // cleaning empty children
    if( tag._ instanceof Array ) {
      if( !tag._.length ) delete tag._;
      else _cleanNodes(tag._);
    }

  });
  return nodes;
}
442267
protected function _deserializeDateTimeExtractionPaths(DOMXPath $xpath)
	{
		foreach ($this->_dateTimeExtractionPaths as $setter => $path) {
			$value = $xpath->evaluate($path);
			if ($value) {
				$this->$setter(new DateTime($value));
			}
		}
		return $this;
	}
1220385
protected static function getNodes(DOMDocument $dom, $type)
	{
		$nodes  = [];
		$prefix = ($type === 'attribute') ? '@' : '';
		$xpath  = new DOMXPath($dom);

		// Get natural nodes
		foreach ($xpath->query('//' . $prefix . '*') as $node)
		{
			$nodes[] = [$node, $node->nodeName];
		}

		// Get XSL-generated nodes
		foreach ($xpath->query('//xsl:' . $type) as $node)
		{
			$nodes[] = [$node, $node->getAttribute('name')];
		}

		// Get xsl:copy-of nodes
		foreach ($xpath->query('//xsl:copy-of') as $node)
		{
			if (preg_match('/^' . $prefix . '(\\w+)$/', $node->getAttribute('select'), $m))
			{
				$nodes[] = [$node, $m[1]];
			}
		}

		return $nodes;
	}
485714
public function toXML(DOMElement $parent = null) : DOMElement
    {
        if ($parent === null) {
            $doc = DOMDocumentFactory::create();
            $e = $doc->createElementNS(Constants::NS_MD, 'md:EntitiesDescriptor');
            $doc->appendChild($e);
        } else {
            $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:EntitiesDescriptor');
            $parent->appendChild($e);
        }

        if ($this->ID !== null) {
            $e->setAttribute('ID', $this->ID);
        }

        if ($this->validUntil !== null) {
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
        }

        if ($this->cacheDuration !== null) {
            $e->setAttribute('cacheDuration', $this->cacheDuration);
        }

        if ($this->Name !== null) {
            $e->setAttribute('Name', $this->Name);
        }

        Extensions::addList($e, $this->Extensions);

        /** @var \SAML2\XML\md\EntityDescriptor|\SAML2\XML\md\EntitiesDescriptor $node */
        foreach ($this->children as $node) {
            $node->toXML($e);
        }

        $this->signElement($e, $e->firstChild);

        return $e;
    }
843514
private void recursivelyRemoveNodesFromPage(final DOMNode parent, final SecurityContext securityContext) throws FrameworkException {

		// Remove node from page
		final PropertyMap changedProperties = new PropertyMap();

		changedProperties.put(StructrApp.key(DOMNode.class, "syncedNodes"), Collections.EMPTY_LIST);
		changedProperties.put(StructrApp.key(DOMNode.class, "pageId"),      null);

		parent.setProperties(securityContext, changedProperties);

		// recurse
		for (final DOMNode child : parent.getChildren()) {

			recursivelyRemoveNodesFromPage(child, securityContext);
		}
	}
321177
def remove_elements(parent_to_parse, element_paths, clear_empty=False):
    

    element = get_element(parent_to_parse)
    removed = []

    if element is None or not element_paths:
        return removed

    if isinstance(element_paths, string_types):
        rem = remove_element(element, element_paths, clear_empty)
        removed.extend(rem if isinstance(rem, list) else [rem])
    else:
        for xpath in element_paths:
            rem = remove_element(element, xpath, clear_empty)
            removed.extend(rem if isinstance(rem, list) else [rem])

    return removed
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;
	}
822217
function( selector, keepData ) {
		if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
			if ( !keepData && this.nodeType === 1 ) {
				jQuery.cleanData( this.getElementsByTagName("*") );
				jQuery.cleanData( [ this ] );
			}

			if ( this.parentNode ) {
				 this.parentNode.removeChild( this );
			}
		}
	}