FluidXml\FluidXml::setAttribute PHP Method

setAttribute() public method

public setAttribute ( $name, $value = null )
    public function setAttribute($name, $value = null)
    {
        $this->context()->setAttribute($name, $value);
        return $this;
    }

Usage Example

Beispiel #1
0
/*****************************
 * Creating An XML Document. *
******************************/
$book = new FluidXml('book');
// or
$book = new FluidXml(null, ['root' => 'book']);
// $book is an XML document with 'book' as root node.
// The default options are:
// [ 'root'       => 'doc',      // The root node of the document.
//   'version'    => '1.0',      // The version for the XML header.
//   'encoding'   => 'UTF-8',    // The encoding for the XML header.
//   'stylesheet' => null ];     // An url pointing to an XSL file.
$booksheet = new FluidXml('book', ['stylesheet' => 'http://domain.com/style.xsl']);
// With PHP 7 this is valid too:
// $booksheet = FluidXml::new('book', ['stylesheet' => 'http://domain.com/style.xsl']);
$book->setAttribute('type', 'science')->addChild(['title' => 'The Theory Of Everything', 'author' => 'S. Hawking']);
// It creates two nodes, each one with some text inside.
echo $book->xml();
// Exports the xml document as a string.
echo "————————————————————————————————————————————————————————————————————————————————\n";
/**********************
 * Context Switching. *
***********************/
/*
* Passing a 'true' boolean value to any method that performs an insertion of a node,
* returns the newly created node instead of the parent.
* This operation is called Context Switch.
* Methods that support this context switch are:
* - addChild($child, ...$optionals);
* - prependSibling($sibling, ...$optionals);
* - appendSibling($sibling, ...$optionals);
All Usage Examples Of FluidXml\FluidXml::setAttribute