Source for file Fragment.php
Documentation is available at Fragment.php
* Created on 05.01.2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
* PHP versions 5.2 or better.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the GPL. For more information please see
* <http://opendocumentphp.org>.
* $Id: Fragment.php 159 2007-03-16 14:12:15Z nmarkgraf $
* Abstract Fragment class.
* @author Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
* @copyright Copyright in 2006, 2007 by The OpenDocumentPHP Team
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
* @version $Revision: 159 $
* @package OpenDocumentPHP
* @since 0.5.0 - 08.02.2007
* @deprecated 0.5.2 - 05.03.2007 Use ODPElement instead of Fragment or ElementFragment!
* namespace OpenDocument meta
const META = 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
* namespace OpenDocument office
const OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
* namespace OpenDocument manifest
const MANIFEST = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0';
* namespace OpenDocument style
const STYLE = 'urn:oasis:names:tc:opendocument:xmlns:style:1.0';
* namespace OpenDocument text
const TEXT = 'urn:oasis:names:tc:opendocument:xmlns:text:1.0';
* namespace OpenDocument draw
const DRAW = 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0';
* namespace OpenDocument table
const TABLE = 'urn:oasis:names:tc:opendocument:xmlns:table:1.0';
* namespace OpenDocument number
const NUMBER = 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0';
* namespace OpenDocument chart
const CHART = 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0';
* namespace OpenDocument form
const FORM = 'urn:oasis:names:tc:opendocument:xmlns:form:1.0';
* namespace OpenDocument config
const CONFIG = 'urn:oasis:names:tc:opendocument:xmlns:config:1.0';
* namespace OpenDocument presentation
const PRESENTATION = 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0';
* namespace OpenDocument dr3d
const DR3D = 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0';
* namespace OpenDocument animation
const ANIM = 'urn:oasis:names:tc:opendocument:xmlns:animation:1.0';
* namespace OpenDocument script
const SCRIPT = 'urn:oasis:names:tc:opendocument:xmlns:script:1.0';
* namespace OpenDocument svg
const SVG = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0';
* namespace OpenDocument fo (formation objects)
const FO = 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0';
* namespace OpenDocument smil
const SMIL = 'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0';
const DC = 'http://purl.org/dc/elements/1.1/';
const XLINK = 'http://www.w3.org/1999/xlink';
const XFORMS = 'http://www.w3.org/2002/xforms';
const MATHML = 'http://www.w3.org/1998/Math/MathML';
* @param DOMDocument $domFragment Basic DOM document.
* @since 0.5.0 - 08.02.2007
if (isset ($root) && ($root !== 0)) {
* Returns the root DOMElement of this DOM fragment.
* @return DOMElement Root node of this DOM fragment.
* @since 0.5.0 - 08.02.2007
* @since 0.5.0 - 08.02.2007
protected function getTag($query, $namespace, $tag, $defaultValue) {
//echo 'Query:' . $query;
$result = $this->xpath->query($query);
if ($result->length <= 0) {
// We did not find a node in the document, so we need to add a new tag:
if ($defaultValue !== - 1) {
//echo "-> create new node!";
$ret = $this->domFragment->createElementNS($namespace, $tag, $defaultValue);
$this->root->appendChild($ret);
//echo "-> no node found!";
// There where nodes in the list. So we grab the first one in the nodelist.
* Set (which means append or replace an existing node) a new node.
* @param DOMElement $node Old node which should be replaced
* @param DOMElement $newNode New node which should be appended or replace the <b>$node</b> node
* @since 0.5.0 - 08.02.2007
protected function setTag($node, $newNode) {
$tmp = $node->parentNode;
//$tmp->replaceNode($newNode, $node);
//$tmp->replaceChild($node, $newNode);
//$tmp->replaceChild($newNode, $node);
$tmp->removeChild($node);
$tmp->appendChild($newNode);
$this->root->appendChild($newNode);
* Check if the current element has a child $tag with $namespace.
* @return bool True, if there is such an element else false.
* @since 0.5.2 - 04.03.2007
// Get the list and if it has more than one Element return TRUE:
return ($this->root->getElementByTagNameNS($namespace, $tag)->length > 0);
* Retrieve a child by its namespace and tag from the current node.
* @return bool True, if there is such an element else false.
* @since 0.5.2 - 05.03.2007
// read all possible nodes with this tag and namespace in to a node list
$nodeList = $this->root->getElementByTagNameNS($namespace, $tag);
if ($nodelist->length != 1 ) {
// There is no or to many nodes in the node list.
// Get first (and only) item of the node list.
$ret = $nodeList->item(0);
* Initialize xpath. You must do this before using xpath.
* @since 0.5.0 - 08.02.2007
if (!isset ($this->xpath)) {
$this->xpath->registerNamespace('office', self :: OFFICE);
$this->xpath->registerNamespace('meta', self :: META);
$this->xpath->registerNamespace('text', self :: TEXT);
$this->xpath->registerNamespace('dc', self :: DC);
$this->xpath->registerNamespace('xlink', self :: XLINK);
$this->xpath->registerNamespace('style', self :: STYLE);
$this->xpath->registerNamespace('draw', self :: DRAW);
$this->xpath->registerNamespace('table', self :: TABLE);
$this->xpath->registerNamespace('mathml', self :: MATHML);
* Create an DOMElement in the Office namespace with tag $tag.
* $this->createOfficeElement('document');
* will create an DOMElement like:
* <office:document xmlns:office="" />
* @param string $tag Tag of the new DOMElement
* @since 0.5.0 - 08.02.2007
* @deprecated 0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
return $this->domFragment->createElementNS(self :: OFFICE, 'office:' . $tag);
* Create an DOMElement in the Meta namespace with tag $tag.
* @param $string $tag Tag of the new DOMElement
* @since 0.5.0 - 08.02.2007
* @deprecated 0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
return $this->domFragment->createElementNS(self :: META, 'meta:' . $tag);
* Create an DOMElement in the Meta namespace with tag $tag.
* @param $string $tag Tag of the new DOMElement
* @since 0.5.0 - 08.02.2007
* @deprecated 0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
return $this->domFragment->createElementNS(self :: SYTLE, 'style:' . $tag);
* Create an DOMElement in the DC namespace with tag $tag.
* @param $string $tag Tag of the new DOMElement
* @since 0.5.0 - 08.02.2007
* @deprecated 0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
return $this->domFragment->createElementNS(self :: DC, 'dc:' . $tag);
* Set attribute with namespace.
* @since 0.5.2 - 16.03.2007
$uc_child = $child->getElement();
$ret = $this->root->appendChild($uc_child);
$this->root->appendChild($child);
|