Skip to content
Browse files

Create XML_Util2 package

git-svn-id: http://svn.php.net/repository/pear/packages/XML_Util2/trunk@313959 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
0 parents commit 6fa13692d721c2cc996e2471def740dbb6cb6bf9 @CloCkWeRX CloCkWeRX committed Jul 30, 2011
911 Util.php
@@ -0,0 +1,911 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * XML_Util
+ *
+ * XML Utilities package
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2003-2008 Stephan Schmidt <schst@php.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * 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.
+ *
+ * @category XML
+ * @package XML_Util
+ * @author Stephan Schmidt <schst@php.net>
+ * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
+ * @license http://opensource.org/licenses/bsd-license New BSD License
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/XML_Util
+ */
+
+/**
+ * error code for invalid chars in XML name
+ */
+define('XML_UTIL_ERROR_INVALID_CHARS', 51);
+
+/**
+ * error code for invalid chars in XML name
+ */
+define('XML_UTIL_ERROR_INVALID_START', 52);
+
+/**
+ * error code for non-scalar tag content
+ */
+define('XML_UTIL_ERROR_NON_SCALAR_CONTENT', 60);
+
+/**
+ * error code for missing tag name
+ */
+define('XML_UTIL_ERROR_NO_TAG_NAME', 61);
+
+/**
+ * replace XML entities
+ */
+define('XML_UTIL_REPLACE_ENTITIES', 1);
+
+/**
+ * embedd content in a CData Section
+ */
+define('XML_UTIL_CDATA_SECTION', 5);
+
+/**
+ * do not replace entitites
+ */
+define('XML_UTIL_ENTITIES_NONE', 0);
+
+/**
+ * replace all XML entitites
+ * This setting will replace <, >, ", ' and &
+ */
+define('XML_UTIL_ENTITIES_XML', 1);
+
+/**
+ * replace only required XML entitites
+ * This setting will replace <, " and &
+ */
+define('XML_UTIL_ENTITIES_XML_REQUIRED', 2);
+
+/**
+ * replace HTML entitites
+ * @link http://www.php.net/htmlentities
+ */
+define('XML_UTIL_ENTITIES_HTML', 3);
+
+/**
+ * Collapse all empty tags.
+ */
+define('XML_UTIL_COLLAPSE_ALL', 1);
+
+/**
+ * Collapse only empty XHTML tags that have no end tag.
+ */
+define('XML_UTIL_COLLAPSE_XHTML_ONLY', 2);
+
+/**
+ * utility class for working with XML documents
+ *
+
+ * @category XML
+ * @package XML_Util
+ * @author Stephan Schmidt <schst@php.net>
+ * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
+ * @license http://opensource.org/licenses/bsd-license New BSD License
+ * @version Release: @version@
+ * @link http://pear.php.net/package/XML_Util
+ */
+class XML_Util
+{
+ /**
+ * return API version
+ *
+ * @return string $version API version
+ * @access public
+ * @static
+ */
+ function apiVersion()
+ {
+ return '1.1';
+ }
+
+ /**
+ * replace XML entities
+ *
+ * With the optional second parameter, you may select, which
+ * entities should be replaced.
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // replace XML entites:
+ * $string = XML_Util::replaceEntities('This string contains < & >.');
+ * </code>
+ *
+ * With the optional third parameter, you may pass the character encoding
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // replace XML entites in UTF-8:
+ * $string = XML_Util::replaceEntities(
+ * 'This string contains < & > as well as ä, ö, ß, à and ê',
+ * XML_UTIL_ENTITIES_HTML,
+ * 'UTF-8'
+ * );
+ * </code>
+ *
+ * @param string $string string where XML special chars
+ * should be replaced
+ * @param int $replaceEntities setting for entities in attribute values
+ * (one of XML_UTIL_ENTITIES_XML,
+ * XML_UTIL_ENTITIES_XML_REQUIRED,
+ * XML_UTIL_ENTITIES_HTML)
+ * @param string $encoding encoding value (if any)...
+ * must be a valid encoding as determined
+ * by the htmlentities() function
+ *
+ * @return string string with replaced chars
+ * @access public
+ * @static
+ * @see reverseEntities()
+ */
+ function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML,
+ $encoding = 'ISO-8859-1')
+ {
+ switch ($replaceEntities) {
+ case XML_UTIL_ENTITIES_XML:
+ return strtr($string, array(
+ '&' => '&amp;',
+ '>' => '&gt;',
+ '<' => '&lt;',
+ '"' => '&quot;',
+ '\'' => '&apos;' ));
+ break;
+ case XML_UTIL_ENTITIES_XML_REQUIRED:
+ return strtr($string, array(
+ '&' => '&amp;',
+ '<' => '&lt;',
+ '"' => '&quot;' ));
+ break;
+ case XML_UTIL_ENTITIES_HTML:
+ return htmlentities($string, ENT_COMPAT, $encoding);
+ break;
+ }
+ return $string;
+ }
+
+ /**
+ * reverse XML entities
+ *
+ * With the optional second parameter, you may select, which
+ * entities should be reversed.
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // reverse XML entites:
+ * $string = XML_Util::reverseEntities('This string contains &lt; &amp; &gt;.');
+ * </code>
+ *
+ * With the optional third parameter, you may pass the character encoding
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // reverse XML entites in UTF-8:
+ * $string = XML_Util::reverseEntities(
+ * 'This string contains &lt; &amp; &gt; as well as'
+ * . ' &auml;, &ouml;, &szlig;, &agrave; and &ecirc;',
+ * XML_UTIL_ENTITIES_HTML,
+ * 'UTF-8'
+ * );
+ * </code>
+ *
+ * @param string $string string where XML special chars
+ * should be replaced
+ * @param int $replaceEntities setting for entities in attribute values
+ * (one of XML_UTIL_ENTITIES_XML,
+ * XML_UTIL_ENTITIES_XML_REQUIRED,
+ * XML_UTIL_ENTITIES_HTML)
+ * @param string $encoding encoding value (if any)...
+ * must be a valid encoding as determined
+ * by the html_entity_decode() function
+ *
+ * @return string string with replaced chars
+ * @access public
+ * @static
+ * @see replaceEntities()
+ */
+ function reverseEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML,
+ $encoding = 'ISO-8859-1')
+ {
+ switch ($replaceEntities) {
+ case XML_UTIL_ENTITIES_XML:
+ return strtr($string, array(
+ '&amp;' => '&',
+ '&gt;' => '>',
+ '&lt;' => '<',
+ '&quot;' => '"',
+ '&apos;' => '\'' ));
+ break;
+ case XML_UTIL_ENTITIES_XML_REQUIRED:
+ return strtr($string, array(
+ '&amp;' => '&',
+ '&lt;' => '<',
+ '&quot;' => '"' ));
+ break;
+ case XML_UTIL_ENTITIES_HTML:
+ return html_entity_decode($string, ENT_COMPAT, $encoding);
+ break;
+ }
+ return $string;
+ }
+
+ /**
+ * build an xml declaration
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // get an XML declaration:
+ * $xmlDecl = XML_Util::getXMLDeclaration('1.0', 'UTF-8', true);
+ * </code>
+ *
+ * @param string $version xml version
+ * @param string $encoding character encoding
+ * @param bool $standalone document is standalone (or not)
+ *
+ * @return string xml declaration
+ * @access public
+ * @static
+ * @uses attributesToString() to serialize the attributes of the XML declaration
+ */
+ function getXMLDeclaration($version = '1.0', $encoding = null,
+ $standalone = null)
+ {
+ $attributes = array(
+ 'version' => $version,
+ );
+ // add encoding
+ if ($encoding !== null) {
+ $attributes['encoding'] = $encoding;
+ }
+ // add standalone, if specified
+ if ($standalone !== null) {
+ $attributes['standalone'] = $standalone ? 'yes' : 'no';
+ }
+
+ return sprintf('<?xml%s?>',
+ XML_Util::attributesToString($attributes, false));
+ }
+
+ /**
+ * build a document type declaration
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // get a doctype declaration:
+ * $xmlDecl = XML_Util::getDocTypeDeclaration('rootTag','myDocType.dtd');
+ * </code>
+ *
+ * @param string $root name of the root tag
+ * @param string $uri uri of the doctype definition
+ * (or array with uri and public id)
+ * @param string $internalDtd internal dtd entries
+ *
+ * @return string doctype declaration
+ * @access public
+ * @static
+ * @since 0.2
+ */
+ function getDocTypeDeclaration($root, $uri = null, $internalDtd = null)
+ {
+ if (is_array($uri)) {
+ $ref = sprintf(' PUBLIC "%s" "%s"', $uri['id'], $uri['uri']);
+ } elseif (!empty($uri)) {
+ $ref = sprintf(' SYSTEM "%s"', $uri);
+ } else {
+ $ref = '';
+ }
+
+ if (empty($internalDtd)) {
+ return sprintf('<!DOCTYPE %s%s>', $root, $ref);
+ } else {
+ return sprintf("<!DOCTYPE %s%s [\n%s\n]>", $root, $ref, $internalDtd);
+ }
+ }
+
+ /**
+ * create string representation of an attribute list
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // build an attribute string
+ * $att = array(
+ * 'foo' => 'bar',
+ * 'argh' => 'tomato'
+ * );
+ *
+ * $attList = XML_Util::attributesToString($att);
+ * </code>
+ *
+ * @param array $attributes attribute array
+ * @param bool|array $sort sort attribute list alphabetically,
+ * may also be an assoc array containing
+ * the keys 'sort', 'multiline', 'indent',
+ * 'linebreak' and 'entities'
+ * @param bool $multiline use linebreaks, if more than
+ * one attribute is given
+ * @param string $indent string used for indentation of
+ * multiline attributes
+ * @param string $linebreak string used for linebreaks of
+ * multiline attributes
+ * @param int $entities setting for entities in attribute values
+ * (one of XML_UTIL_ENTITIES_NONE,
+ * XML_UTIL_ENTITIES_XML,
+ * XML_UTIL_ENTITIES_XML_REQUIRED,
+ * XML_UTIL_ENTITIES_HTML)
+ *
+ * @return string string representation of the attributes
+ * @access public
+ * @static
+ * @uses replaceEntities() to replace XML entities in attribute values
+ * @todo allow sort also to be an options array
+ */
+ function attributesToString($attributes, $sort = true, $multiline = false,
+ $indent = ' ', $linebreak = "\n", $entities = XML_UTIL_ENTITIES_XML)
+ {
+ /*
+ * second parameter may be an array
+ */
+ if (is_array($sort)) {
+ if (isset($sort['multiline'])) {
+ $multiline = $sort['multiline'];
+ }
+ if (isset($sort['indent'])) {
+ $indent = $sort['indent'];
+ }
+ if (isset($sort['linebreak'])) {
+ $multiline = $sort['linebreak'];
+ }
+ if (isset($sort['entities'])) {
+ $entities = $sort['entities'];
+ }
+ if (isset($sort['sort'])) {
+ $sort = $sort['sort'];
+ } else {
+ $sort = true;
+ }
+ }
+ $string = '';
+ if (is_array($attributes) && !empty($attributes)) {
+ if ($sort) {
+ ksort($attributes);
+ }
+ if ( !$multiline || count($attributes) == 1) {
+ foreach ($attributes as $key => $value) {
+ if ($entities != XML_UTIL_ENTITIES_NONE) {
+ if ($entities === XML_UTIL_CDATA_SECTION) {
+ $entities = XML_UTIL_ENTITIES_XML;
+ }
+ $value = XML_Util::replaceEntities($value, $entities);
+ }
+ $string .= ' ' . $key . '="' . $value . '"';
+ }
+ } else {
+ $first = true;
+ foreach ($attributes as $key => $value) {
+ if ($entities != XML_UTIL_ENTITIES_NONE) {
+ $value = XML_Util::replaceEntities($value, $entities);
+ }
+ if ($first) {
+ $string .= ' ' . $key . '="' . $value . '"';
+ $first = false;
+ } else {
+ $string .= $linebreak . $indent . $key . '="' . $value . '"';
+ }
+ }
+ }
+ }
+ return $string;
+ }
+
+ /**
+ * Collapses empty tags.
+ *
+ * @param string $xml XML
+ * @param int $mode Whether to collapse all empty tags (XML_UTIL_COLLAPSE_ALL)
+ * or only XHTML (XML_UTIL_COLLAPSE_XHTML_ONLY) ones.
+ *
+ * @return string XML
+ * @access public
+ * @static
+ * @todo PEAR CS - unable to avoid "space after open parens" error
+ * in the IF branch
+ */
+ function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL)
+ {
+ if ($mode == XML_UTIL_COLLAPSE_XHTML_ONLY) {
+ return preg_replace(
+ '/<(area|base(?:font)?|br|col|frame|hr|img|input|isindex|link|meta|'
+ . 'param)([^>]*)><\/\\1>/s',
+ '<\\1\\2 />',
+ $xml);
+ } else {
+ return preg_replace('/<(\w+)([^>]*)><\/\\1>/s', '<\\1\\2 />', $xml);
+ }
+ }
+
+ /**
+ * create a tag
+ *
+ * This method will call XML_Util::createTagFromArray(), which
+ * is more flexible.
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // create an XML tag:
+ * $tag = XML_Util::createTag('myNs:myTag',
+ * array('foo' => 'bar'),
+ * 'This is inside the tag',
+ * 'http://www.w3c.org/myNs#');
+ * </code>
+ *
+ * @param string $qname qualified tagname (including namespace)
+ * @param array $attributes array containg attributes
+ * @param mixed $content the content
+ * @param string $namespaceUri URI of the namespace
+ * @param int $replaceEntities whether to replace XML special chars in
+ * content, embedd it in a CData section
+ * or none of both
+ * @param bool $multiline whether to create a multiline tag where
+ * each attribute gets written to a single line
+ * @param string $indent string used to indent attributes
+ * (_auto indents attributes so they start
+ * at the same column)
+ * @param string $linebreak string used for linebreaks
+ * @param bool $sortAttributes Whether to sort the attributes or not
+ *
+ * @return string XML tag
+ * @access public
+ * @static
+ * @see createTagFromArray()
+ * @uses createTagFromArray() to create the tag
+ */
+ function createTag($qname, $attributes = array(), $content = null,
+ $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
+ $multiline = false, $indent = '_auto', $linebreak = "\n",
+ $sortAttributes = true)
+ {
+ $tag = array(
+ 'qname' => $qname,
+ 'attributes' => $attributes
+ );
+
+ // add tag content
+ if ($content !== null) {
+ $tag['content'] = $content;
+ }
+
+ // add namespace Uri
+ if ($namespaceUri !== null) {
+ $tag['namespaceUri'] = $namespaceUri;
+ }
+
+ return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline,
+ $indent, $linebreak, $sortAttributes);
+ }
+
+ /**
+ * create a tag from an array
+ * this method awaits an array in the following format
+ * <pre>
+ * array(
+ * // qualified name of the tag
+ * 'qname' => $qname
+ *
+ * // namespace prefix (optional, if qname is specified or no namespace)
+ * 'namespace' => $namespace
+ *
+ * // local part of the tagname (optional, if qname is specified)
+ * 'localpart' => $localpart,
+ *
+ * // array containing all attributes (optional)
+ * 'attributes' => array(),
+ *
+ * // tag content (optional)
+ * 'content' => $content,
+ *
+ * // namespaceUri for the given namespace (optional)
+ * 'namespaceUri' => $namespaceUri
+ * )
+ * </pre>
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * $tag = array(
+ * 'qname' => 'foo:bar',
+ * 'namespaceUri' => 'http://foo.com',
+ * 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ * 'content' => 'I\'m inside the tag',
+ * );
+ * // creating a tag with qualified name and namespaceUri
+ * $string = XML_Util::createTagFromArray($tag);
+ * </code>
+ *
+ * @param array $tag tag definition
+ * @param int $replaceEntities whether to replace XML special chars in
+ * content, embedd it in a CData section
+ * or none of both
+ * @param bool $multiline whether to create a multiline tag where each
+ * attribute gets written to a single line
+ * @param string $indent string used to indent attributes
+ * (_auto indents attributes so they start
+ * at the same column)
+ * @param string $linebreak string used for linebreaks
+ * @param bool $sortAttributes Whether to sort the attributes or not
+ *
+ * @return string XML tag
+ * @access public
+ * @static
+ * @see createTag()
+ * @uses attributesToString() to serialize the attributes of the tag
+ * @uses splitQualifiedName() to get local part and namespace of a qualified name
+ * @uses createCDataSection()
+ * @uses raiseError()
+ */
+ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
+ $multiline = false, $indent = '_auto', $linebreak = "\n",
+ $sortAttributes = true)
+ {
+ if (isset($tag['content']) && !is_scalar($tag['content'])) {
+ return XML_Util::raiseError('Supplied non-scalar value as tag content',
+ XML_UTIL_ERROR_NON_SCALAR_CONTENT);
+ }
+
+ if (!isset($tag['qname']) && !isset($tag['localPart'])) {
+ return XML_Util::raiseError('You must either supply a qualified name '
+ . '(qname) or local tag name (localPart).',
+ XML_UTIL_ERROR_NO_TAG_NAME);
+ }
+
+ // if no attributes hav been set, use empty attributes
+ if (!isset($tag['attributes']) || !is_array($tag['attributes'])) {
+ $tag['attributes'] = array();
+ }
+
+ if (isset($tag['namespaces'])) {
+ foreach ($tag['namespaces'] as $ns => $uri) {
+ $tag['attributes']['xmlns:' . $ns] = $uri;
+ }
+ }
+
+ if (!isset($tag['qname'])) {
+ // qualified name is not given
+
+ // check for namespace
+ if (isset($tag['namespace']) && !empty($tag['namespace'])) {
+ $tag['qname'] = $tag['namespace'] . ':' . $tag['localPart'];
+ } else {
+ $tag['qname'] = $tag['localPart'];
+ }
+ } elseif (isset($tag['namespaceUri']) && !isset($tag['namespace'])) {
+ // namespace URI is set, but no namespace
+
+ $parts = XML_Util::splitQualifiedName($tag['qname']);
+
+ $tag['localPart'] = $parts['localPart'];
+ if (isset($parts['namespace'])) {
+ $tag['namespace'] = $parts['namespace'];
+ }
+ }
+
+ if (isset($tag['namespaceUri']) && !empty($tag['namespaceUri'])) {
+ // is a namespace given
+ if (isset($tag['namespace']) && !empty($tag['namespace'])) {
+ $tag['attributes']['xmlns:' . $tag['namespace']] =
+ $tag['namespaceUri'];
+ } else {
+ // define this Uri as the default namespace
+ $tag['attributes']['xmlns'] = $tag['namespaceUri'];
+ }
+ }
+
+ // check for multiline attributes
+ if ($multiline === true) {
+ if ($indent === '_auto') {
+ $indent = str_repeat(' ', (strlen($tag['qname'])+2));
+ }
+ }
+
+ // create attribute list
+ $attList = XML_Util::attributesToString($tag['attributes'],
+ $sortAttributes, $multiline, $indent, $linebreak, $replaceEntities);
+ if (!isset($tag['content']) || (string)$tag['content'] == '') {
+ $tag = sprintf('<%s%s />', $tag['qname'], $attList);
+ } else {
+ switch ($replaceEntities) {
+ case XML_UTIL_ENTITIES_NONE:
+ break;
+ case XML_UTIL_CDATA_SECTION:
+ $tag['content'] = XML_Util::createCDataSection($tag['content']);
+ break;
+ default:
+ $tag['content'] = XML_Util::replaceEntities($tag['content'],
+ $replaceEntities);
+ break;
+ }
+ $tag = sprintf('<%s%s>%s</%s>', $tag['qname'], $attList, $tag['content'],
+ $tag['qname']);
+ }
+ return $tag;
+ }
+
+ /**
+ * create a start element
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // create an XML start element:
+ * $tag = XML_Util::createStartElement('myNs:myTag',
+ * array('foo' => 'bar') ,'http://www.w3c.org/myNs#');
+ * </code>
+ *
+ * @param string $qname qualified tagname (including namespace)
+ * @param array $attributes array containg attributes
+ * @param string $namespaceUri URI of the namespace
+ * @param bool $multiline whether to create a multiline tag where each
+ * attribute gets written to a single line
+ * @param string $indent string used to indent attributes (_auto indents
+ * attributes so they start at the same column)
+ * @param string $linebreak string used for linebreaks
+ * @param bool $sortAttributes Whether to sort the attributes or not
+ *
+ * @return string XML start element
+ * @access public
+ * @static
+ * @see createEndElement(), createTag()
+ */
+ function createStartElement($qname, $attributes = array(), $namespaceUri = null,
+ $multiline = false, $indent = '_auto', $linebreak = "\n",
+ $sortAttributes = true)
+ {
+ // if no attributes hav been set, use empty attributes
+ if (!isset($attributes) || !is_array($attributes)) {
+ $attributes = array();
+ }
+
+ if ($namespaceUri != null) {
+ $parts = XML_Util::splitQualifiedName($qname);
+ }
+
+ // check for multiline attributes
+ if ($multiline === true) {
+ if ($indent === '_auto') {
+ $indent = str_repeat(' ', (strlen($qname)+2));
+ }
+ }
+
+ if ($namespaceUri != null) {
+ // is a namespace given
+ if (isset($parts['namespace']) && !empty($parts['namespace'])) {
+ $attributes['xmlns:' . $parts['namespace']] = $namespaceUri;
+ } else {
+ // define this Uri as the default namespace
+ $attributes['xmlns'] = $namespaceUri;
+ }
+ }
+
+ // create attribute list
+ $attList = XML_Util::attributesToString($attributes, $sortAttributes,
+ $multiline, $indent, $linebreak);
+ $element = sprintf('<%s%s>', $qname, $attList);
+ return $element;
+ }
+
+ /**
+ * create an end element
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // create an XML start element:
+ * $tag = XML_Util::createEndElement('myNs:myTag');
+ * </code>
+ *
+ * @param string $qname qualified tagname (including namespace)
+ *
+ * @return string XML end element
+ * @access public
+ * @static
+ * @see createStartElement(), createTag()
+ */
+ function createEndElement($qname)
+ {
+ $element = sprintf('</%s>', $qname);
+ return $element;
+ }
+
+ /**
+ * create an XML comment
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // create an XML start element:
+ * $tag = XML_Util::createComment('I am a comment');
+ * </code>
+ *
+ * @param string $content content of the comment
+ *
+ * @return string XML comment
+ * @access public
+ * @static
+ */
+ function createComment($content)
+ {
+ $comment = sprintf('<!-- %s -->', $content);
+ return $comment;
+ }
+
+ /**
+ * create a CData section
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // create a CData section
+ * $tag = XML_Util::createCDataSection('I am content.');
+ * </code>
+ *
+ * @param string $data data of the CData section
+ *
+ * @return string CData section with content
+ * @access public
+ * @static
+ */
+ function createCDataSection($data)
+ {
+ return sprintf('<![CDATA[%s]]>',
+ preg_replace('/\]\]>/', ']]]]><![CDATA[>', strval($data)));
+
+ }
+
+ /**
+ * split qualified name and return namespace and local part
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // split qualified tag
+ * $parts = XML_Util::splitQualifiedName('xslt:stylesheet');
+ * </code>
+ * the returned array will contain two elements:
+ * <pre>
+ * array(
+ * 'namespace' => 'xslt',
+ * 'localPart' => 'stylesheet'
+ * );
+ * </pre>
+ *
+ * @param string $qname qualified tag name
+ * @param string $defaultNs default namespace (optional)
+ *
+ * @return array array containing namespace and local part
+ * @access public
+ * @static
+ */
+ function splitQualifiedName($qname, $defaultNs = null)
+ {
+ if (strstr($qname, ':')) {
+ $tmp = explode(':', $qname);
+ return array(
+ 'namespace' => $tmp[0],
+ 'localPart' => $tmp[1]
+ );
+ }
+ return array(
+ 'namespace' => $defaultNs,
+ 'localPart' => $qname
+ );
+ }
+
+ /**
+ * check, whether string is valid XML name
+ *
+ * <p>XML names are used for tagname, attribute names and various
+ * other, lesser known entities.</p>
+ * <p>An XML name may only consist of alphanumeric characters,
+ * dashes, undescores and periods, and has to start with a letter
+ * or an underscore.</p>
+ *
+ * <code>
+ * require_once 'XML/Util.php';
+ *
+ * // verify tag name
+ * $result = XML_Util::isValidName('invalidTag?');
+ * if (is_a($result, 'PEAR_Error')) {
+ * print 'Invalid XML name: ' . $result->getMessage();
+ * }
+ * </code>
+ *
+ * @param string $string string that should be checked
+ *
+ * @return mixed true, if string is a valid XML name, PEAR error otherwise
+ * @access public
+ * @static
+ * @todo support for other charsets
+ * @todo PEAR CS - unable to avoid 85-char limit on second preg_match
+ */
+ function isValidName($string)
+ {
+ // check for invalid chars
+ if (!preg_match('/^[[:alpha:]_]\\z/', $string{0})) {
+ return XML_Util::raiseError('XML names may only start with letter '
+ . 'or underscore', XML_UTIL_ERROR_INVALID_START);
+ }
+
+ // check for invalid chars
+ if (!preg_match('/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?[[:alpha:]_]([[:alnum:]\_\-\.]+)?\\z/',
+ $string)
+ ) {
+ return XML_Util::raiseError('XML names may only contain alphanumeric '
+ . 'chars, period, hyphen, colon and underscores',
+ XML_UTIL_ERROR_INVALID_CHARS);
+ }
+ // XML name is valid
+ return true;
+ }
+
+ /**
+ * replacement for XML_Util::raiseError
+ *
+ * Avoids the necessity to always require
+ * PEAR.php
+ *
+ * @param string $msg error message
+ * @param int $code error code
+ *
+ * @return PEAR_Error
+ * @access public
+ * @static
+ * @todo PEAR CS - should this use include_once instead?
+ */
+ function raiseError($msg, $code)
+ {
+ require_once 'PEAR.php';
+ return PEAR::raiseError($msg, $code);
+ }
+}
+?>
299 examples/example.php
@@ -0,0 +1,299 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Examples (file #1)
+ *
+ * several examples for the methods of XML_Util
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2003-2008 Stephan Schmidt <schst@php.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * 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.
+ *
+ * @category XML
+ * @package XML_Util
+ * @subpackage Examples
+ * @author Stephan Schmidt <schst@php.net>
+ * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
+ * @license http://opensource.org/licenses/bsd-license New BSD License
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/XML_Util
+ */
+
+ /**
+ * set error level
+ */
+ error_reporting(E_ALL);
+
+ require_once 'XML/Util.php';
+
+ /**
+ * replacing XML entities
+ */
+ print 'replace XML entities:<br>';
+ print XML_Util::replaceEntities('This string contains < & >.');
+ print "\n<br><br>\n";
+
+ /**
+ * reversing XML entities
+ */
+ print 'replace XML entities:<br>';
+ print XML_Util::reverseEntities('This string contains &lt; &amp; &gt;.');
+ print "\n<br><br>\n";
+
+ /**
+ * building XML declaration
+ */
+ print 'building XML declaration:<br>';
+ print htmlspecialchars(XML_Util::getXMLDeclaration());
+ print "\n<br><br>\n";
+
+ print 'building XML declaration with additional attributes:<br>';
+ print htmlspecialchars(XML_Util::getXMLDeclaration('1.0', 'UTF-8', true));
+ print "\n<br><br>\n";
+
+ /**
+ * building document type declaration
+ */
+ print 'building DocType declaration:<br>';
+ print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
+ 'http://pear.php.net/dtd/package-1.0'));
+ print "\n<br><br>\n";
+
+ print 'building DocType declaration with public ID (does not exist):<br>';
+ print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
+ array('uri' => 'http://pear.php.net/dtd/package-1.0',
+ 'id' => '-//PHP//PEAR/DTD PACKAGE 0.1')));
+ print "\n<br><br>\n";
+
+ print 'building DocType declaration with internal DTD:<br>';
+ print '<pre>';
+ print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
+ 'http://pear.php.net/dtd/package-1.0',
+ '<!ELEMENT additionalInfo (#PCDATA)>'));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+ /**
+ * creating an attribute string
+ */
+ $att = array(
+ 'foo' => 'bar',
+ 'argh' => 'tomato'
+ );
+
+ print 'converting array to string:<br>';
+ print XML_Util::attributesToString($att);
+ print "\n<br><br>\n";
+
+
+ /**
+ * creating an attribute string with linebreaks
+ */
+ $att = array(
+ 'foo' => 'bar',
+ 'argh' => 'tomato'
+ );
+
+ print 'converting array to string (including line breaks):<br>';
+ print '<pre>';
+ print XML_Util::attributesToString($att, true, true);
+ print '</pre>';
+ print "\n<br><br>\n";
+
+
+ /**
+ * splitting a qualified tag name
+ */
+ print 'splitting qualified tag name:<br>';
+ print '<pre>';
+ print_r(XML_Util::splitQualifiedName('xslt:stylesheet'));
+ print '</pre>';
+ print "\n<br>\n";
+
+
+ /**
+ * splitting a qualified tag name (no namespace)
+ */
+ print 'splitting qualified tag name (no namespace):<br>';
+ print '<pre>';
+ print_r(XML_Util::splitQualifiedName('foo'));
+ print '</pre>';
+ print "\n<br>\n";
+
+ /**
+ * splitting a qualified tag name (no namespace, but default namespace specified)
+ */
+ print 'splitting qualified tag name '
+ . '(no namespace, but default namespace specified):<br>';
+ print '<pre>';
+ print_r(XML_Util::splitQualifiedName('foo', 'bar'));
+ print '</pre>';
+ print "\n<br>\n";
+
+ /**
+ * verifying XML names
+ */
+ print 'verifying \'My private tag\':<br>';
+ print '<pre>';
+ print_r(XML_Util::isValidname('My Private Tag'));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+ print 'verifying \'-MyTag\':<br>';
+ print '<pre>';
+ print_r(XML_Util::isValidname('-MyTag'));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag
+ */
+ $tag = array(
+ 'namespace' => 'foo',
+ 'localPart' => 'bar',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ 'content' => 'I\'m inside the tag'
+ );
+
+ print 'creating a tag with namespace and local part:<br>';
+ print htmlentities(XML_Util::createTagFromArray($tag));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag
+ */
+ $tag = array(
+ 'qname' => 'foo:bar',
+ 'namespaceUri' => 'http://foo.com',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ 'content' => 'I\'m inside the tag'
+ );
+
+ print 'creating a tag with qualified name and namespaceUri:<br>';
+ print htmlentities(XML_Util::createTagFromArray($tag));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag
+ */
+ $tag = array(
+ 'qname' => 'bar',
+ 'namespaceUri' => 'http://foo.com',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable')
+ );
+
+ print 'creating an empty tag without namespace but namespace Uri:<br>';
+ print htmlentities(XML_Util::createTagFromArray($tag));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag with more namespaces
+ */
+ $tag = array(
+ 'namespace' => 'foo',
+ 'localPart' => 'bar',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ 'content' => 'I\'m inside the tag',
+ 'namespaces' => array(
+ 'bar' => 'http://bar.com',
+ 'pear' => 'http://pear.php.net',
+ )
+ );
+
+ print 'creating an XML tag with more namespaces:<br />';
+ print htmlentities(XML_Util::createTagFromArray($tag));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag with a CData Section
+ */
+ $tag = array(
+ 'qname' => 'foo',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ 'content' => 'I\'m inside the tag'
+ );
+
+ print 'creating a tag with CData section:<br>';
+ print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_CDATA_SECTION));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag with a CData Section
+ */
+ $tag = array(
+ 'qname' => 'foo',
+ 'attributes' => array('key' => 'value', 'argh' => 'tütü'),
+ 'content' =>
+ 'Also XHTML-tags can be created '
+ . 'and HTML entities can be replaced Ä ä Ü ö <>.'
+ );
+
+ print 'creating a tag with HTML entities:<br>';
+ print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag with createTag
+ */
+ print 'creating a tag with createTag:<br>';
+ print htmlentities(XML_Util::createTag('myNs:myTag',
+ array('foo' => 'bar'),
+ 'This is inside the tag',
+ 'http://www.w3c.org/myNs#'));
+ print "\n<br><br>\n";
+
+
+ /**
+ * trying to create an XML tag with an array as content
+ */
+ $tag = array(
+ 'qname' => 'bar',
+ 'content' => array('foo' => 'bar')
+ );
+ print 'trying to create an XML tag with an array as content:<br>';
+ print '<pre>';
+ print_r(XML_Util::createTagFromArray($tag));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+ /**
+ * trying to create an XML tag without a name
+ */
+ $tag = array(
+ 'attributes' => array('foo' => 'bar'),
+ );
+ print 'trying to create an XML tag without a name:<br>';
+ print '<pre>';
+ print_r(XML_Util::createTagFromArray($tag));
+ print '</pre>';
+ print "\n<br><br>\n";
+?>
145 examples/example2.php
@@ -0,0 +1,145 @@
+<?php
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Examples (file #2)
+ *
+ * several examples for the methods of XML_Util
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2003-2008 Stephan Schmidt <schst@php.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * 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.
+ *
+ * @category XML
+ * @package XML_Util
+ * @subpackage Examples
+ * @author Stephan Schmidt <schst@php.net>
+ * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
+ * @license http://opensource.org/licenses/bsd-license New BSD License
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/XML_Util
+ */
+
+ /**
+ * set error level
+ */
+ error_reporting(E_ALL);
+
+ require_once 'XML/Util.php';
+
+ /**
+ * creating a start element
+ */
+ print 'creating a start element:<br>';
+ print htmlentities(XML_Util::createStartElement('myNs:myTag',
+ array('foo' => 'bar'), 'http://www.w3c.org/myNs#'));
+ print "\n<br><br>\n";
+
+
+ /**
+ * creating a start element
+ */
+ print 'creating a start element:<br>';
+ print htmlentities(XML_Util::createStartElement('myTag',
+ array(), 'http://www.w3c.org/myNs#'));
+ print "\n<br><br>\n";
+
+ /**
+ * creating a start element
+ */
+ print 'creating a start element:<br>';
+ print '<pre>';
+ print htmlentities(XML_Util::createStartElement('myTag',
+ array('foo' => 'bar', 'argh' => 'tomato'),
+ 'http://www.w3c.org/myNs#', true));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+
+ /**
+ * creating an end element
+ */
+ print 'creating an end element:<br>';
+ print htmlentities(XML_Util::createEndElement('myNs:myTag'));
+ print "\n<br><br>\n";
+
+ /**
+ * creating a CData section
+ */
+ print 'creating a CData section:<br>';
+ print htmlentities(XML_Util::createCDataSection('I am content.'));
+ print "\n<br><br>\n";
+
+ /**
+ * creating a comment
+ */
+ print 'creating a comment:<br>';
+ print htmlentities(XML_Util::createComment('I am a comment.'));
+ print "\n<br><br>\n";
+
+ /**
+ * creating an XML tag with multiline mode
+ */
+ $tag = array(
+ 'qname' => 'foo:bar',
+ 'namespaceUri' => 'http://foo.com',
+ 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
+ 'content' => 'I\'m inside the tag & contain dangerous chars'
+ );
+
+ print 'creating a tag with qualified name and namespaceUri:<br>';
+ print '<pre>';
+ print htmlentities(XML_Util::createTagFromArray($tag,
+ XML_UTIL_REPLACE_ENTITIES, true));
+ print '</pre>';
+ print "\n<br><br>\n";
+
+ /**
+ * create an attribute string without replacing the entities
+ */
+ $atts = array('series' => 'Starsky &amp; Hutch', 'channel' => 'ABC');
+ print 'creating a attribute string, '
+ . 'entities in values already had been replaced:<br>';
+ print htmlentities(XML_Util::attributesToString($atts,
+ true, false, false, false, XML_UTIL_ENTITIES_NONE));
+ print "\n<br><br>\n";
+
+ /**
+ * using the array-syntax for attributesToString()
+ */
+ $atts = array('series' => 'Starsky &amp; Hutch', 'channel' => 'ABC');
+ print 'using the array-syntax for attributesToString()<br>';
+ print htmlentities(XML_Util::attributesToString($atts,
+ array('entities' => XML_UTIL_ENTITIES_NONE)));
+ print "\n<br><br>\n";
+
+
+?>
64 package.php
@@ -0,0 +1,64 @@
+<?php
+
+require_once 'PEAR/PackageFileManager2.php';
+PEAR::setErrorHandling(PEAR_ERROR_DIE);
+
+$desc =
+ "Selection of methods that are often needed when working with XML documents. "
+ . "Functionality includes creating of attribute lists from arrays, "
+ . "creation of tags, validation of XML names and more."
+;
+
+$version = '1.2.1';
+$apiver = '1.2.0';
+$state = 'stable';
+
+$notes = <<<EOT
+Fixed Bug #14760: Bug in getDocTypeDeclaration() [ashnazg|fpospisil]
+EOT;
+
+$package = PEAR_PackageFileManager2::importOptions(
+ 'package.xml',
+ array(
+ 'filelistgenerator' => 'cvs',
+ 'changelogoldtonew' => false,
+ 'simpleoutput' => true,
+ 'baseinstalldir' => 'XML',
+ 'packagefile' => 'package.xml',
+ 'packagedirectory' => '.'));
+
+if (PEAR::isError($result)) {
+ echo $result->getMessage();
+ die();
+}
+
+$package->clearDeps();
+
+$package->setPackage('XML_Util');
+$package->setPackageType('php');
+$package->setSummary('XML utility class');
+$package->setDescription($desc);
+$package->setChannel('pear.php.net');
+$package->setLicense('BSD License', 'http://opensource.org/licenses/bsd-license');
+$package->setAPIVersion($apiver);
+$package->setAPIStability($state);
+$package->setReleaseVersion($version);
+$package->setReleaseStability($state);
+$package->setNotes($notes);
+$package->setPhpDep('4.3.0');
+$package->setPearinstallerDep('1.4.3');
+$package->addExtensionDep('required', 'pcre');
+$package->addIgnore(array('package.php', 'package.xml'));
+$package->addReplacement('Util.php', 'package-info', '@version@', 'version');
+$package->generateContents();
+
+if ($_SERVER['argv'][1] == 'make') {
+ $result = $package->writePackageFile();
+} else {
+ $result = $package->debugPackageFile();
+}
+
+if (PEAR::isError($result)) {
+ echo $result->getMessage();
+ die();
+}
407 package.xml
@@ -0,0 +1,407 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.7.2" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+ <name>XML_Util</name>
+ <channel>pear.php.net</channel>
+ <summary>XML utility class</summary>
+ <description>Selection of methods that are often needed when working with XML documents. Functionality includes creating of attribute lists from arrays, creation of tags, validation of XML names and more.</description>
+ <lead>
+ <name>Chuck Burgess</name>
+ <user>ashnazg</user>
+ <email>ashnazg@php.net</email>
+ <active>yes</active>
+ </lead>
+ <lead>
+ <name>Stephan Schmidt</name>
+ <user>schst</user>
+ <email>schst@php-tools.net</email>
+ <active>no</active>
+ </lead>
+ <helper>
+ <name>Davey Shafik</name>
+ <user>davey</user>
+ <email>davey@php.net</email>
+ <active>no</active>
+ </helper>
+ <date>2008-12-07</date>
+ <time>19:40:27</time>
+ <version>
+ <release>1.2.1</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Fixed Bug #14760: Bug in getDocTypeDeclaration() [ashnazg|fpospisil]</notes>
+ <contents>
+ <dir baseinstalldir="XML" name="/">
+ <dir name="examples">
+ <file name="example.php" role="doc" />
+ <file name="example2.php" role="doc" />
+ </dir> <!-- /examples -->
+ <dir name="tests">
+ <file name="AllTests.php" role="test" />
+ <file name="testBasic_apiVersion.phpt" role="test" />
+ <file name="testBasic_attributesToString.phpt" role="test" />
+ <file name="testBasic_collapseEmptyTags.phpt" role="test" />
+ <file name="testBasic_createCDataSection.phpt" role="test" />
+ <file name="testBasic_createComment.phpt" role="test" />
+ <file name="testBasic_createEndElement.phpt" role="test" />
+ <file name="testBasic_createStartElement.phpt" role="test" />
+ <file name="testBasic_createTag.phpt" role="test" />
+ <file name="testBasic_createTagFromArray.phpt" role="test" />
+ <file name="testBasic_getDocTypeDeclaration.phpt" role="test" />
+ <file name="testBasic_getXmlDeclaration.phpt" role="test" />
+ <file name="testBasic_isValidName.phpt" role="test" />
+ <file name="testBasic_raiseError.phpt" role="test" />
+ <file name="testBasic_replaceEntities.phpt" role="test" />
+ <file name="testBasic_reverseEntities.phpt" role="test" />
+ <file name="testBasic_splitQualifiedName.phpt" role="test" />
+ <file name="testBug_4950.phpt" role="test" />
+ <file name="testBug_5392.phpt" role="test" />
+ </dir> <!-- /tests -->
+ <file name="Util.php" role="php">
+ <tasks:replace from="@version@" to="version" type="package-info" />
+ </file>
+ </dir> <!-- / -->
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>4.3.0</min>
+ </php>
+ <pearinstaller>
+ <min>1.4.3</min>
+ </pearinstaller>
+ <extension>
+ <name>pcre</name>
+ </extension>
+ </required>
+ </dependencies>
+ <phprelease />
+ <changelog>
+ <release>
+ <version>
+ <release>1.2.1</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2008-12-07</date>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Fixed Bug #14760: Bug in getDocTypeDeclaration() [ashnazg|fpospisil]</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.2.0</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2008-07-26</date>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Changed license to New BSD License (Req #13826 [ashnazg])
+Added a test suite against all API methods [ashnazg]
+Switch to package.xml v2 [ashnazg]
+Added Req #13839: Missing XHTML empty tags to collapse [ashnazg|drry]
+Fixed Bug #5392: encoding of ISO-8859-1 is the only supported encoding [ashnazg]
+Fixed Bug #4950: Incorrect CDATA serializing [ashnazg|drry]
+-- (this fix differs from the one in v1.2.0a1)</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.2.0RC1</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <date>2008-07-12</date>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Changed license to New BSD License (Req #13826 [ashnazg])
+Added a test suite against all API methods [ashnazg]
+Switch to package.xml v2 [ashnazg]
+Added Req #13839: Missing XHTML empty tags to collapse [ashnazg|drry]
+Fixed Bug #5392: encoding of ISO-8859-1 is the only supported encoding [ashnazg]
+Fixed Bug #4950: Incorrect CDATA serializing [ashnazg|drry]
+-- (this fix differs from the one in v1.2.0a1)</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.2.0a2</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <date>2008-05-22</date>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Changed license to New BSD License (Req #13826 [ashnazg])
+Added a test suite against all API methods [ashnazg]
+Switch to package.xml v2 [ashnazg]
+Added Req #13839: Missing XHTML empty tags to collapse [ashnazg|drry]
+Fixed Bug #5392: encoding of ISO-8859-1 is the only supported encoding [ashnazg]
+Fixed Bug #4950: Incorrect CDATA serializing [ashnazg|drry]
+-- (this fix differs from the one in v1.2.0a1)</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.2.0a1</release>
+ <api>1.2.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <date>2008-05-04</date>
+ <license uri="http://opensource.org/licenses/bsd-license">BSD License</license>
+ <notes>Changed license to New BSD License (Req #13826 [ashnazg])
+Added a test suite against all API methods [ashnazg]
+Switch to package.xml v2 [ashnazg]
+Fixed Bug #4950: Incorrect CDATA serializing [ashnazg|ja.doma]</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.1.4</release>
+ <api>1.1.4</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2006-12-16</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Fixed bug #9561: Not allowing underscores in middle of tags</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.1.2</release>
+ <api>1.1.2</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2006-12-01</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- fixed bug #5419: isValidName() now checks for character classes
+- implemented request #8196: added optional parameter to influence array sorting to createTag() createTagFromArray() and createStartElement()</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.1.1</release>
+ <api>1.1.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2004-12-23</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- fixed bug in replaceEntities() and reverseEntities() in conjunction with XML_UTIL_ENTITIES_HTML
+- createTag() and createTagFromArray() now accept XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML, XML_UTIL_ENTITIES_NONE and XML_UTIL_CDATA_SECTION as $replaceEntities parameter</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.1.0</release>
+ <api>1.1.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2004-11-19</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Added collapseEmptyTags (patch by Sebastian Bergmann and Thomas Duffey)</notes>
+ </release>
+ <release>
+ <version>
+ <release>1.0.0</release>
+ <api>1.0.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2004-10-28</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Added reverseEntities() (request #2639)</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.6.1</release>
+ <api>0.6.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2004-10-28</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Added check for tag name (either as local part or qualified name) in createTagFromArray() (bug #1083)</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.6.0</release>
+ <api>0.6.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2004-06-07</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Fixed bug 1438 (namespaces not accepted for isValidName()) (thanks to davey)
+- added optional parameter to replaceEntities() to define the set of entities to replace
+- added optional parameter to attributesToString() to define, whether entities should be replaced (requested by Sebastian Bergmann)
+- allowed second parameter to XML_Util::attributesToString() to be an array containing options (easier to use, if you only need to set the last parameter)
+- introduced XML_Util::raiseError() to avoid the necessity of including PEAR.php, will only be included on error</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.6.0beta1</release>
+ <api>0.6.0beta1</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <date>2004-05-24</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- Fixed bug 1438 (namespaces not accepted for isValidName()) (thanks to davey)
+- added optional parameter to replaceEntities() to define the set of entities to replace
+- added optional parameter to attributesToString() to define, whether entities should be replaced (requested by Sebastian Bergmann)
+- allowed second parameter to XML_Util::attributesToString() to be an array containing options (easier to use, if you only need to set the last parameter)
+- introduced XML_Util::raiseError() to avoid the necessity of including PEAR.php, will only be included on error</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.5.2</release>
+ <api>0.5.2</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-11-22</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>now creates XHTML compliant empty tags (Davey),
+minor whitespace fixes (Davey)</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.5.1</release>
+ <api>0.5.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-09-26</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>added default namespace parameter (optional) in splitQualifiedName() (requested by Sebastian Bergmann)</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.5</release>
+ <api>0.5</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-09-23</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>added support for multiline attributes in attributesToString(), createTag*() and createStartElement (requested by Yavor Shahpasov for XML_Serializer),
+added createComment</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.4</release>
+ <api>0.4</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-09-21</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>added createCDataSection(),
+added support for CData sections in createTag* methods,
+fixed bug #23,
+fixed bug in splitQualifiedName()</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.3</release>
+ <api>0.3</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-09-12</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>added createStartElement() and createEndElement()</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.2.1</release>
+ <api>0.2.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-09-05</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>fixed bug with zero as tag content in createTagFromArray and createTag</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.2</release>
+ <api>0.2</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-08-12</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>added XML_Util::getDocTypeDeclaration()</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.1.1</release>
+ <api>0.1.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-08-02</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>bugfix: removed bug in createTagFromArray</notes>
+ </release>
+ <release>
+ <version>
+ <release>0.1</release>
+ <api>0.1</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <date>2003-08-01</date>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>inital release</notes>
+ </release>
+ </changelog>
+</package>
132 tests/AllTests.php
@@ -0,0 +1,132 @@
+<?php
+
+/**
+ * Master Unit Test Suite file for XML_Util
+ *
+ * This top-level test suite file organizes
+ * all class test suite files,
+ * so that the full suite can be run
+ * by PhpUnit or via "pear run-tests -u".
+ *
+ * PHP version 5
+ *
+ * @category XML
+ * @package XML_Util
+ * @subpackage UnitTesting
+ * @author Chuck Burgess <ashnazg@php.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/XML_Util
+ * @since 1.2.0a1
+ */
+
+
+/**
+ * Check PHP version... PhpUnit v3+ requires at least PHP v5.1.4
+ */
+if (version_compare(PHP_VERSION, "5.1.4") < 0) {
+ // Cannnot run test suites
+ echo 'Cannot run test suite via PhpUnit... requires at least PHP v5.1.4.' . PHP_EOL;
+ echo 'Use "pear run-tests -p xml_util" to run the PHPT tests directly.' . PHP_EOL
+;
+ exit(1);
+}
+
+
+/**
+ * Derive the "main" method name
+ * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD
+ * to make this usage meet the PEAR CS... we cannot rename it here.
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'XML_Util_AllTests::main');
+}
+
+
+/*
+ * Files needed by PhpUnit
+ */
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+require_once 'PHPUnit/Extensions/PhptTestSuite.php';
+
+/*
+ * You must add each additional class-level test suite file here
+ */
+// there are no PhpUnit test files... only PHPTs.. so nothing is listed here
+
+/**
+ * directory where PHPT tests are located
+ */
+define('XML_UTIL_DIR_PHPT', dirname(__FILE__));
+
+/**
+ * Master Unit Test Suite class for XML_Util
+ *
+ * This top-level test suite class organizes
+ * all class test suite files,
+ * so that the full suite can be run
+ * by PhpUnit or via "pear run-tests -up xml_util".
+ *
+ * @category XML
+ * @package XML_Util
+ * @subpackage UnitTesting
+ * @author Chuck Burgess <ashnazg@php.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/XML_Util
+ * @since 1.2.0a1
+ */
+class XML_Util_AllTests
+{
+
+ /**
+ * Launches the TextUI test runner
+ *
+ * @return void
+ * @uses PHPUnit_TextUI_TestRunner
+ */
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+
+ /**
+ * Adds all class test suites into the master suite
+ *
+ * @return PHPUnit_Framework_TestSuite a master test suite
+ * containing all class test suites
+ * @uses PHPUnit_Framework_TestSuite
+ */
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite(
+ 'XML_Util Full Suite of Unit Tests');
+
+ /*
+ * You must add each additional class-level test suite name here
+ */
+ // there are no PhpUnit test files... only PHPTs.. so nothing is listed here
+
+ /*
+ * add PHPT tests
+ */
+ $phpt = new PHPUnit_Extensions_PhptTestSuite(XML_UTIL_DIR_PHPT);
+ $suite->addTestSuite($phpt);
+
+ return $suite;
+ }
+}
+
+/**
+ * Call the main method if this file is executed directly
+ * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD
+ * to make this usage meet the PEAR CS... we cannot rename it here.
+ */
+if (PHPUnit_MAIN_METHOD == 'XML_Util_AllTests::main') {
+ XML_Util_AllTests::main();
+}
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+?>
18 tests/testBasic_apiVersion.phpt
@@ -0,0 +1,18 @@
+--TEST--
+XML_Util::apiVersion() basic tests
+--CREDITS--
+Chuck Burgess <ashnazg@php.net>
+# created for v1.2.0a1 2008-05-04
+--FILE--
+<?php
+require_once 'XML' . DIRECTORY_SEPARATOR . 'Util.php';
+echo '=====XML_Util::apiVersion() basic tests=====' . PHP_EOL . PHP_EOL;
+
+echo "TEST: basic apiVersion() call" . PHP_EOL;
+echo XML_Util::apiVersion() . PHP_EOL;
+?>
+--EXPECT--
+=====XML_Util::apiVersion() basic tests=====
+
+TEST: basic apiVersion() call
+1.1
118 tests/testBasic_attributesToString.phpt
@@ -0,0 +1,118 @@
+--TEST--
+XML_Util::attributesToString() basic tests
+--CREDITS--
+Chuck Burgess <ashnazg@php.net>
+# created for v1.2.0a1 2008-05-04
+--FILE--
+<?php
+require_once 'XML' . DIRECTORY_SEPARATOR . 'Util.php';
+echo '=====XML_Util::attributesToString() basic tests=====' . PHP_EOL . PHP_EOL;
+
+$att = array("foo" => "bar", "boo" => "baz");
+$sort1 = array(
+ 'multiline' => true,
+ 'indent' => '----',
+ 'linebreak' => "^",
+ 'entities' => XML_UTIL_ENTITIES_XML,
+ 'sort' => true
+);
+$sort2 = array(
+ 'multiline' => true,
+ 'indent' => '----',
+ 'linebreak' => "^",
+ 'entities' => XML_UTIL_ENTITIES_XML,
+);
+
+echo "TEST: basic usage" . PHP_EOL;
+echo XML_Util::attributesToString($att) . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$sort = true" . PHP_EOL;
+echo XML_Util::attributesToString($att, true) . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$sort = false" . PHP_EOL;
+echo XML_Util::attributesToString($att, false) . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$multiline = false" . PHP_EOL;
+echo XML_Util::attributesToString($att, true, false) . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$multiline = true" . PHP_EOL;
+echo XML_Util::attributesToString($att, true, true) . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$indent = ' ' (8 spaces)" . PHP_EOL;
+echo XML_Util::attributesToString($att, true, true, ' ') . PHP_EOL . PHP_EOL;
+
+echo "TEST: explicit \$linebreak = '^' (some dummy char)" . PHP_EOL;
+echo XML_Util::attributesToString($att, true, true, '^') . PHP_EOL . PHP_EOL;
+
+echo "TEST: passing \$sort array of options that includes 'sort'" . PHP_EOL;
+echo XML_Util::attributesToString($att, $sort1) . PHP_EOL . PHP_EOL;
+
+echo "TEST: passing \$sort array of options that doesn't include 'sort'" . PHP_EOL;
+echo XML_Util::attributesToString($att, $sort2) . PHP_EOL . PHP_EOL;
+
+echo "TEST: do not replace entities" . PHP_EOL;
+$arr = array("foo" => "b@&r", "boo" => "b><z");
+echo XML_Util::attributesToString($arr, true, false, ' ', PHP_EOL,
+ XML_UTIL_ENTITIES_NONE) . PHP_EOL . PHP_EOL;
+
+echo "TEST: replace all XML entities" . PHP_EOL;
+$arr = array("foo" => "b@&r", "boo" => "b><z");
+echo XML_Util::attributesToString($arr, true, false, ' ', PHP_EOL,
+ XML_UTIL_ENTITIES_XML) . PHP_EOL . PHP_EOL;
+
+echo "TEST: replace only required XML entities" . PHP_EOL;
+$arr = array("foo" => "b@&r", "boo" => "b><z");
+echo XML_Util::attributesToString($arr, true, false, ' ', PHP_EOL,
+ XML_UTIL_ENTITIES_XML_REQUIRED) . PHP_EOL . PHP_EOL;
+
+echo "TEST: replace HTML entities" . PHP_EOL;
+$arr = array("foo" => "b@&r", "boo" => "b><z");
+echo XML_Util::attributesToString($arr, true, false, ' ', PHP_EOL,
+ XML_UTIL_ENTITIES_HTML) . PHP_EOL . PHP_EOL;
+?>
+--EXPECT--
+=====XML_Util::attributesToString() basic tests=====
+
+TEST: basic usage
+ boo="baz" foo="bar"
+
+TEST: explicit $sort = true
+ boo="baz" foo="bar"
+
+TEST: explicit $sort = false
+ foo="bar" boo="baz"
+
+TEST: explicit $multiline = false
+ boo="baz" foo="bar"
+
+TEST: explicit $multiline = true
+ boo="baz"
+ foo="bar"
+
+TEST: explicit $indent = ' ' (8 spaces)
+ boo="baz"
+ foo="bar"
+
+TEST: explicit $linebreak = '^' (some dummy char)
+ boo="baz"
+^foo="bar"
+
+TEST: passing $sort array of options that includes 'sort'
+ boo="baz"
+----foo="bar"
+
+TEST: passing $sort array of options that doesn't include 'sort'
+ boo="baz"
+----foo="bar"
+
+TEST: do not replace entities
+ boo="b><z" foo="b@&r"
+
+TEST: replace all XML entities
+ boo="b&gt;&lt;z" foo="b@&amp;r"
+
+TEST: replace only required XML entities
+ boo="b>&lt;z" foo="b@&amp;r"
+
+TEST: replace HTML entities
+ boo="b&gt;&lt;z" foo="b@&amp;r"
52 tests/testBasic_collapseEmptyTags.phpt
@@ -0,0 +1,52 @@
+--TEST--
+XML_Util::collapseEmptyTags() basic tests
+--CREDITS--
+Chuck Burgess <ashnazg@php.net>
+# created for v1.2.0a1 2008-05-04
+--FILE--
+<?php
+require_once 'XML' . DIRECTORY_SEPARATOR . 'Util.php';
+echo '=====XML_Util::collapseEmptyTags() basic tests=====' . PHP_EOL . PHP_EOL;
+
+$emptyTag = "<foo></foo>";
+$otherTag = "<bar>baz</bar>";
+$xhtmlTag = "<b></b>";
+
+echo "TEST: basic usage" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag) . PHP_EOL . PHP_EOL;
+
+echo "TEST: basic usage alongside non-empty tag" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag . $otherTag) . PHP_EOL . PHP_EOL;
+
+echo "TEST: one empty tag, with COLLAPSE_ALL set" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag, XML_UTIL_COLLAPSE_ALL) . PHP_EOL . PHP_EOL;
+
+echo "TEST: one empty tag alongside non-empty tag, with COLLAPSE_ALL set" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag . $otherTag, XML_UTIL_COLLAPSE_ALL) . PHP_EOL . PHP_EOL;
+
+echo "TEST: one empty tag, with COLLAPSE_XHTML_ONLY set" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag, XML_UTIL_COLLAPSE_XHTML_ONLY) . PHP_EOL . PHP_EOL;
+
+echo "TEST: one empty tag alongside non-empty tag, with COLLAPSE_XHTML_ONLY set" . PHP_EOL;
+echo XML_Util::collapseEmptyTags($emptyTag . $xhtmlTag . $otherTag, XML_UTIL_COLLAPSE_XHTML_ONLY) . PHP_EOL . PHP_EOL;
+?>
+--EXPECT--
+=====XML_Util::collapseEmptyTags() basic tests=====
+
+TEST: basic usage
+<foo />
+
+TEST: basic usage alongside non-empty tag
+<foo /><bar>baz</bar>
+
+TEST: one empty tag, with COLLAPSE_ALL set
+<foo />
+
+TEST: one empty tag alongside non-empty tag, with COLLAPSE_ALL set
+<foo /><bar>baz</bar>
+
+TEST: one empty tag, with COLLAPSE_XHTML_ONLY set
+<foo></foo>
+
+TEST: one empty tag alongside non-empty tag, with COLLAPSE_XHTML_ONLY set
+<foo></foo><b></b><bar>baz</bar>
18 tests/testBasic_createCDataSection.phpt
@@ -0,0 +1,18 @@
+--TEST--
+XML_Util::createCDataSection() basic tests
+--CREDITS--
+Chuck Burgess <ashnazg@php.net>
+# created for v1.2.0a1 2008-05-04
+--FILE--
+<?php
+require_once 'XML' . DIRECTORY_SEPARATOR . 'Util.php';
+echo '=====XML_Util::createCDataSection() basic tests=====' . PHP_EOL . PHP_EOL;
+
+echo "TEST: basic usage" . PHP_EOL;
+echo XML_Util::createCDataSection("I am content.") . PHP_EOL;
+?>
+--EXPECT--
+=====XML_Util::createCDataSection() basic tests=====
+
+TEST: basic usage
+<![CDATA[I am content.]]>
18 tests/testBasic_createComment.phpt
@@ -0,0 +1,18 @@
+--TEST--
+XML_Util::createComment() basic tests
+--CREDITS--
+Chuck Burgess <ashnazg@php.net>
+# created for v1.2.0a1 2008-05-04
+--FILE--
+<?php
+require_once 'XML' . DIRECTORY_SEPARATOR . 'Util.php';
+echo '=====XML_Util::createComment() basic tests=====' . PHP_EOL . PHP_EOL;
+
+echo "TEST: basic usage" . PHP_EOL;
+echo XML_Util::createComment("I am comment.") . PHP_EOL;
+?>
+--EXPECT--
+=====XML_Util::createComment() basic tests=====
+
+TEST: basic usage
+<!-- I am comment. -->
24 tests/testBasic_createEndElement.phpt
@@ -0,0 +1,24 @@
+--TEST--