hasAttributes example

$elements = $xpath->query('body//' . $tag);
        if ($this->encode_instead_of_strip)
        {
            foreach ($elements as $element)
            {
                $fragment = $document->createDocumentFragment();

                // For elements which aren't script or style, include the tag itself                 if (!in_array($tag, array('script', 'style')))
                {
                    $text = '<' . $tag;
                    if ($element->hasAttributes())
                    {
                        $attrs = array();
                        foreach ($element->attributes as $name => $attr)
                        {
                            $value = $attr->value;

                            // In XHTML, empty values should never exist, so we repeat the value                             if (empty($value) && ($type & SIMPLEPIE_CONSTRUCT_XHTML))
                            {
                                $value = $name;
                            }
                            
return self::cleanArray(self::xmlToArray($elements->item(0)));
    }

    /** * @see https://stackoverflow.com/questions/14553547/what-is-the-best-php-dom-2-array-function */
    private static function xmlToArray(DOMNode $root)
    {
        $result = [];

        if ($root->hasAttributes()) {
            foreach ($root->attributes as $attr) {
                $result['@attributes'][$attr->name] = $attr->value;
            }
        }

        if ($root->hasChildNodes()) {
            $children = $root->childNodes;
            if ($children->length == 1) {
                $child = $children->item(0);
                if ($child->nodeType == XML_TEXT_NODE) {
                    $result['_value'] = $child->nodeValue;

                    


            unset($data['@xmlns:xml']);

            if (empty($data)) {
                return $this->parseXml($rootNode$context);
            }

            return array_merge($data(array) $this->parseXml($rootNode$context));
        }

        if (!$rootNode->hasAttributes()) {
            return $rootNode->nodeValue;
        }

        $data = [];

        foreach ($rootNode->attributes as $attrKey => $attr) {
            $data['@'.$attrKey] = $attr->nodeValue;
        }

        $data['#'] = $rootNode->nodeValue;

        


            unset($data['@xmlns:xml']);

            if (empty($data)) {
                return $this->parseXml($rootNode$context);
            }

            return array_merge($data(array) $this->parseXml($rootNode$context));
        }

        if (!$rootNode->hasAttributes()) {
            return $rootNode->nodeValue;
        }

        $data = [];

        foreach ($rootNode->attributes as $attrKey => $attr) {
            $data['@'.$attrKey] = $attr->nodeValue;
        }

        $data['#'] = $rootNode->nodeValue;

        
$this->wr('>');
            }             // If there are no children this is self closing.             else {
                $this->wr(' />');
            }
        }
    }

    protected function attrs($ele)
    {
        // FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.         if (!$ele->hasAttributes()) {
            return $this;
        }

        // TODO: Currently, this always writes name="value", and does not do         // value-less attributes.         $map = $ele->attributes;
        $len = $map->length;
        for ($i = 0; $i < $len; ++$i) {
            $node = $map->item($i);
            $val = $this->enc($node->value, true);

            
// Skip the empty text nodes inside tags.         continue;
      }
      $tag = $node->tagName;

      // All attributes are already allowed on this tag, this is the most       // permissive configuration, no additional processing is required.       if (isset($restrictions['allowed'][$tag]) && $restrictions['allowed'][$tag] === TRUE) {
        continue;
      }

      if ($node->hasAttributes()) {
        // If the tag is not yet present, prepare to add attribute restrictions.         // Otherwise, check if a more restrictive configuration (FALSE, meaning         // no attributes were allowed) is present: then override the existing         // value to prepare to add attribute restrictions.         if (!isset($restrictions['allowed'][$tag]) || $restrictions['allowed'][$tag] === FALSE) {
          $restrictions['allowed'][$tag] = [];
        }

        // Iterate over any attributes, and mark them as allowed.         foreach ($node->attributes as $name => $attribute) {
          // Only add specific attribute values if all values are not already
Home | Imprint | This part of the site doesn't use cookies.