eZXMLInputParser::processAttributesBySchema PHP Method

processAttributesBySchema() public method

public processAttributesBySchema ( $element )
    function processAttributesBySchema($element)
    {
        // Remove attributes that don't match schema
        $schemaAttributes = $this->XMLSchema->attributes($element);
        $schemaCustomAttributes = $this->XMLSchema->customAttributes($element);
        $attributes = $element->attributes;
        for ($i = $attributes->length - 1; $i >= 0; $i--) {
            $attr = $attributes->item($i);
            if ($attr->prefix == 'tmp') {
                $element->removeAttributeNode($attr);
                continue;
            }
            $allowed = false;
            $removeAttr = false;
            $fullName = $attr->prefix ? $attr->prefix . ':' . $attr->localName : $attr->nodeName;
            // check for allowed custom attributes (3.9)
            if ($attr->prefix == 'custom' && in_array($attr->localName, $schemaCustomAttributes)) {
                $allowed = true;
            } else {
                if (in_array($fullName, $schemaAttributes)) {
                    $allowed = true;
                } elseif (in_array($fullName, $schemaCustomAttributes)) {
                    // add 'custom' prefix if it is not given
                    $allowed = true;
                    $removeAttr = true;
                    $element->setAttributeNS($this->Namespaces['custom'], 'custom:' . $fullName, $attr->value);
                }
            }
            if (!$allowed) {
                $removeAttr = true;
                $this->handleError(self::ERROR_SCHEMA, ezpI18n::tr('kernel/classes/datatypes/ezxmltext', "Attribute '%1' is not allowed in <%2> element.", false, array($fullName, $element->nodeName)));
            } elseif ($this->RemoveDefaultAttrs) {
                // Remove attributes having default values
                $default = $this->XMLSchema->attrDefaultValue($element->nodeName, $fullName);
                if ($attr->value == $default) {
                    $removeAttr = true;
                }
            }
            if ($removeAttr) {
                $element->removeAttributeNode($attr);
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * processAttributesBySchema
  * Parses customattributes attribute and splits it into actual
  * custom: xml attributes, passes processing of normal attributes
  * to parent class.
  *
  * @param DOMElement $element
  */
 function processAttributesBySchema($element)
 {
     // custom attributes conversion
     $attr = $element->getAttribute('customattributes');
     if ($attr) {
         $attrArray = explode('attribute_separation', $attr);
         foreach ($attrArray as $attr) {
             if ($attr !== '' && strpos($attr, '|') !== false) {
                 list($attrName, $attrValue) = explode('|', $attr);
                 $element->setAttributeNS('http://ez.no/namespaces/ezpublish3/custom/', 'custom:' . $attrName, $attrValue);
             }
         }
     }
     parent::processAttributesBySchema($element);
 }