Permalink
Browse files

PEAR CS cleanup

git-svn-id: http://svn.php.net/repository/pear/packages/XML_Util/trunk@259069 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 4022a9c commit 9c551106487b5a51b90290fe712fdfb6888b8448 Chuck Burgess committed May 4, 2008
Showing with 689 additions and 555 deletions.
  1. +529 −426 Util.php
  2. +107 −90 examples/example.php
  3. +53 −39 examples/example2.php
View
955 Util.php
@@ -37,6 +37,14 @@
* 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
*/
/**
@@ -88,7 +96,7 @@
/**
* replace HTML entitites
- * @link http://www.php.net/htmlentities
+ * @link http://www.php.net/htmlentities
*/
define("XML_UTIL_ENTITIES_HTML", 3);
@@ -105,137 +113,153 @@
/**
* utility class for working with XML documents
*
- * @category XML
- * @package XML_Util
- * @version 1.1.0
- * @author Stephan Schmidt <schst@php.net>
+
+ * @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: 1.1.0
+ * @link http://pear.php.net/package/XML_Util
*/
-class XML_Util {
-
- /**
- * return API version
- *
- * @access public
- * @static
- * @return string $version API version
- */
+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>
- *
- * @access public
- * @static
- * @param string string where XML special chars should be replaced
- * @param integer setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
- * @return string string with replaced chars
- * @see reverseEntities()
- */
+ /**
+ * 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>
+ *
+ * @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)
+ *
+ * @return string string with replaced chars
+ * @access public
+ * @static
+ * @see reverseEntities()
+ */
function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML)
{
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);
- break;
+ 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);
+ 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>
- *
- * @access public
- * @static
- * @param string string where XML special chars should be replaced
- * @param integer setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
- * @return string string with replaced chars
- * @see replaceEntities()
- */
+ /**
+ * 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>
+ *
+ * @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)
+ *
+ * @return string string with replaced chars
+ * @access public
+ * @static
+ * @see replaceEntities()
+ */
function reverseEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML)
{
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:
- $arr = array_flip(get_html_translation_table(HTML_ENTITIES));
- return strtr($string, $arr);
- break;
+ 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:
+ $arr = array_flip(get_html_translation_table(HTML_ENTITIES));
+ return strtr($string, $arr);
+ 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>
- *
- * @access public
- * @static
- * @param string $version xml version
- * @param string $encoding character encoding
- * @param boolean $standAlone document is standalone (or not)
- * @return string $decl xml declaration
- * @uses XML_Util::attributesToString() to serialize the attributes of the XML declaration
- */
- function getXMLDeclaration($version = "1.0", $encoding = null, $standalone = null)
+ /**
+ * 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,
- );
+ "version" => $version,
+ );
// add encoding
if ($encoding !== null) {
$attributes["encoding"] = $encoding;
@@ -245,33 +269,36 @@ function getXMLDeclaration($version = "1.0", $encoding = null, $standalone = nul
$attributes["standalone"] = $standalone ? "yes" : "no";
}
- return sprintf("<?xml%s?>", XML_Util::attributesToString($attributes, false));
+ 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>
- *
- * @access public
- * @static
- * @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 $decl doctype declaration
- * @since 0.2
- */
+ /**
+ * 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"] );
+ $ref = sprintf(' PUBLIC "%s" "%s"', $uri["id"], $uri["uri"]);
} elseif (!empty($uri)) {
- $ref = sprintf( ' SYSTEM "%s"', $uri );
+ $ref = sprintf(' SYSTEM "%s"', $uri);
} else {
$ref = "";
}
@@ -283,36 +310,48 @@ function getDocTypeDeclaration($root, $uri = null, $internalDtd = null)
}
}
- /**
- * 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>
- *
- * @access public
- * @static
- * @param array $attributes attribute array
- * @param boolean|array $sort sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities'
- * @param boolean $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 integer $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
- * @uses XML_Util::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)
+ /**
+ * 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)) {
@@ -339,15 +378,15 @@ function attributesToString($attributes, $sort = true, $multiline = false, $inde
if ($sort) {
ksort($attributes);
}
- if( !$multiline || count($attributes) == 1) {
+ 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;
+ $entities = XML_UTIL_ENTITIES_XML;
}
$value = XML_Util::replaceEntities($value, $entities);
}
- $string .= ' '.$key.'="'.$value.'"';
+ $string .= ' ' . $key . '="' . $value . '"';
}
} else {
$first = true;
@@ -356,76 +395,88 @@ function attributesToString($attributes, $sort = true, $multiline = false, $inde
$value = XML_Util::replaceEntities($value, $entities);
}
if ($first) {
- $string .= " ".$key.'="'.$value.'"';
- $first = false;
+ $string .= " " . $key . '="' . $value . '"';
+ $first = false;
} else {
- $string .= $linebreak.$indent.$key.'="'.$value.'"';
+ $string .= $linebreak . $indent . $key . '="' . $value . '"';
}
}
}
}
return $string;
}
- /**
- * Collapses empty tags.
- *
- * @access public
- * @static
- * @param string $xml XML
- * @param integer $mode Whether to collapse all empty tags (XML_UTIL_COLLAPSE_ALL) or only XHTML (XML_UTIL_COLLAPSE_XHTML_ONLY) ones.
- * @return string $xml XML
- */
- function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL) {
+ /**
+ * 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|br|col|hr|img|input|link|meta|param)([^>]*)><\/\\1>/s',
- '<\\1\\2 />',
- $xml
- );
+ '/<(area|base|br|col|hr|img|input|link|meta|param)([^>]*)><\/\\1>/s',
+ '<\\1\\2 />',
+ $xml);
} else {
- return preg_replace(
- '/<(\w+)([^>]*)><\/\\1>/s',
- '<\\1\\2 />',
- $xml
- );
+ 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>
- *
- * @access public
- * @static
- * @param string $qname qualified tagname (including namespace)
- * @param array $attributes array containg attributes
- * @param mixed $content
- * @param string $namespaceUri URI of the namespace
- * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both
- * @param boolean $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 boolean $sortAttributes Whether to sort the attributes or not
- * @return string $string XML tag
- * @see XML_Util::createTagFromArray()
- * @uses XML_Util::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)
+ /**
+ * 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
- );
+ "qname" => $qname,
+ "attributes" => $attributes
+ );
// add tag content
if ($content !== null) {
@@ -437,57 +488,82 @@ function createTag($qname, $attributes = array(), $content = null, $namespaceUri
$tag["namespaceUri"] = $namespaceUri;
}
- return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $sortAttributes);
+ 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(
- * "qname" => $qname // qualified name of the tag
- * "namespace" => $namespace // namespace prefix (optional, if qname is specified or no namespace)
- * "localpart" => $localpart, // local part of the tagname (optional, if qname is specified)
- * "attributes" => array(), // array containing all attributes (optional)
- * "content" => $content, // tag content (optional)
- * "namespaceUri" => $namespaceUri // namespaceUri for the given namespace (optional)
- * )
- * </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>
- *
- * @access public
- * @static
- * @param array $tag tag definition
- * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both
- * @param boolean $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 boolean $sortAttributes Whether to sort the attributes or not
- * @return string $string XML tag
- * @see XML_Util::createTag()
- * @uses XML_Util::attributesToString() to serialize the attributes of the tag
- * @uses XML_Util::splitQualifiedName() to get local part and namespace of a qualified name
- */
- function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $sortAttributes = true)
+ /**
+ * 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 );
+ 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 );
+ 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
@@ -496,22 +572,25 @@ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
}
if (isset($tag['namespaces'])) {
- foreach ($tag['namespaces'] as $ns => $uri) {
- $tag['attributes']['xmlns:'.$ns] = $uri;
- }
+ foreach ($tag['namespaces'] as $ns => $uri) {
+ $tag['attributes']['xmlns:' . $ns] = $uri;
+ }
}
- // qualified name is not given
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"];
+ $tag["qname"] = $tag["namespace"] . ":" . $tag["localPart"];
} else {
$tag["qname"] = $tag["localPart"];
}
- // namespace URI is set, but no namespace
} 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"];
@@ -521,7 +600,8 @@ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
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"];
+ $tag["attributes"]["xmlns:" . $tag["namespace"]] =
+ $tag["namespaceUri"];
} else {
// define this Uri as the default namespace
$tag["attributes"]["xmlns"] = $tag["namespaceUri"];
@@ -536,48 +616,57 @@ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
}
// create attribute list
- $attList = XML_Util::attributesToString($tag['attributes'], $sortAttributes, $multiline, $indent, $linebreak, $replaceEntities );
+ $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);
+ $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;
+ 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'] );
+ $tag = sprintf('<%s%s>%s</%s>', $tag['qname'], $attList, $tag['content'],
+ $tag['qname']);
}
- return $tag;
+ 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>
- *
- * @access public
- * @static
- * @param string $qname qualified tagname (including namespace)
- * @param array $attributes array containg attributes
- * @param string $namespaceUri URI of the namespace
- * @param boolean $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 boolean $sortAttributes Whether to sort the attributes or not
- * @return string $string XML start element
- * @see XML_Util::createEndElement(), XML_Util::createTag()
- */
- function createStartElement($qname, $attributes = array(), $namespaceUri = null, $multiline = false, $indent = '_auto', $linebreak = "\n", $sortAttributes = true)
+ /**
+ * 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)) {
@@ -598,173 +687,187 @@ function createStartElement($qname, $attributes = array(), $namespaceUri = null,
if ($namespaceUri != null) {
// is a namespace given
if (isset($parts["namespace"]) && !empty($parts["namespace"])) {
- $attributes["xmlns:".$parts["namespace"]] = $namespaceUri;
+ $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);
+ $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>
- *
- * @access public
- * @static
- * @param string $qname qualified tagname (including namespace)
- * @return string $string XML end element
- * @see XML_Util::createStartElement(), XML_Util::createTag()
- */
+ /**
+ * 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;
+ $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>
- *
- * @access public
- * @static
- * @param string $content content of the comment
- * @return string $comment XML comment
- */
+ /**
+ * 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;
+ $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>
- *
- * @access public
- * @static
- * @param string $data data of the CData section
- * @return string $string CData section with content
- */
+ /**
+ * 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('/\]\]>/', "]]>]]&gt;<![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>
- *
- * @access public
- * @static
- * @param string $qname qualified tag name
- * @param string $defaultNs default namespace (optional)
- * @return array $parts array containing namespace and local part
- */
+ /**
+ * 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]
- );
+ "namespace" => $tmp[0],
+ "localPart" => $tmp[1]
+ );
}
return array(
- "namespace" => $defaultNs,
- "localPart" => $qname
- );
+ "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>
- *
- * @access public
- * @static
- * @param string $string string that should be checked
- * @return mixed $valid true, if string is a valid XML name, PEAR error otherwise
- * @todo support for other charsets
- */
+ /**
+ * 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:]_]$/', $string{0})) {
- return XML_Util::raiseError('XML names may only start with letter or underscore', XML_UTIL_ERROR_INVALID_START);
+ 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:]\_\-\.]+)?$/', $string)) {
- return XML_Util::raiseError('XML names may only contain alphanumeric chars, period, hyphen, colon and underscores', XML_UTIL_ERROR_INVALID_CHARS);
- }
+ if (!preg_match('/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?[[:alpha:]_]([[:alnum:]\_\-\.]+)?$/',
+ $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
- *
- * @access public
- * @param string error message
- * @param integer error code
- * @return object PEAR_Error
- */
+ /**
+ * 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';
View
197 examples/example.php
@@ -38,34 +38,40 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * $Id$
- *
- * @author Stephan Schmidt
- * @package XML_Util
- * @subpackage examples
- * @category XML
+ * @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
- */
+ * replacing XML entities
+ */
print "replace XML entities:<br>\n";
print XML_Util::replaceEntities("This string contains < & >.");
print "\n<br><br>\n";
/**
- * reversing XML entities
- */
+ * reversing XML entities
+ */
print "replace XML entities:<br>\n";
print XML_Util::reverseEntities("This string contains &lt; &amp; &gt;.");
print "\n<br><br>\n";
/**
- * building XML declaration
- */
+ * building XML declaration
+ */
print "building XML declaration:<br>\n";
print htmlspecialchars(XML_Util::getXMLDeclaration());
print "\n<br><br>\n";
@@ -75,42 +81,47 @@
print "\n<br><br>\n";
/**
- * building document type declaration
- */
+ * building document type declaration
+ */
print "building DocType declaration:<br>\n";
- print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', 'http://pear.php.net/dtd/package-1.0'));
+ 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>\n";
- print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', array('uri' => 'http://pear.php.net/dtd/package-1.0', 'id' => '-//PHP//PEAR/DTD PACKAGE 0.1')));
+ 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>\n";
print "<pre>";
- print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', 'http://pear.php.net/dtd/package-1.0', '<!ELEMENT additionalInfo (#PCDATA)>'));
+ 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
- */
+ * creating an attribute string
+ */
$att = array(
- "foo" => "bar",
- "argh" => "tomato"
- );
+ "foo" => "bar",
+ "argh" => "tomato"
+ );
print "converting array to string:<br>\n";
print XML_Util::attributesToString($att);
print "\n<br><br>\n";
/**
- * creating an attribute string with linebreaks
- */
+ * creating an attribute string with linebreaks
+ */
$att = array(
- "foo" => "bar",
- "argh" => "tomato"
- );
+ "foo" => "bar",
+ "argh" => "tomato"
+ );
print "converting array to string (including line breaks):<br>\n";
print "<pre>";
@@ -120,8 +131,8 @@
/**
- * splitting a qualified tag name
- */
+ * splitting a qualified tag name
+ */
print "splitting qualified tag name:<br>\n";
print "<pre>";
print_r(XML_Util::splitQualifiedName("xslt:stylesheet"));
@@ -130,26 +141,27 @@
/**
- * splitting a qualified tag name (no namespace)
- */
+ * splitting a qualified tag name (no namespace)
+ */
print "splitting qualified tag name (no namespace):<br>\n";
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>\n";
+ * splitting a qualified tag name (no namespace, but default namespace specified)
+ */
+ print "splitting qualified tag name "
+ . "(no namespace, but default namespace specified):<br>\n";
print "<pre>";
print_r(XML_Util::splitQualifiedName("foo", "bar"));
print "</pre>";
print "\n<br>\n";
/**
- * verifying XML names
- */
+ * verifying XML names
+ */
print "verifying 'My private tag':<br>\n";
print "<pre>";
print_r(XML_Util::isValidname('My Private Tag'));
@@ -163,85 +175,87 @@
print "\n<br><br>\n";
/**
- * creating an XML tag
- */
+ * creating an XML tag
+ */
$tag = array(
- "namespace" => "foo",
- "localPart" => "bar",
- "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
- "content" => "I'm inside the tag"
- );
+ "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
- */
+ * 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"
- );
+ "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>\n";
print htmlentities(XML_Util::createTagFromArray($tag));
print "\n<br><br>\n";
/**
- * creating an XML tag
- */
+ * creating an XML tag
+ */
$tag = array(
- "qname" => "bar",
- "namespaceUri" => "http://foo.com",
- "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" )
- );
+ "qname" => "bar",
+ "namespaceUri" => "http://foo.com",
+ "attributes" => array("key" => "value", "argh" => "fruit&vegetable")
+ );
print "creating an empty tag without namespace but namespace Uri:<br>\n";
print htmlentities(XML_Util::createTagFromArray($tag));
print "\n<br><br>\n";
/**
- * creating an XML tag with more namespaces
- */
+ * 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",
- )
- );
+ "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
- */
+ * 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"
- );
+ "qname" => "foo",
+ "attributes" => array("key" => "value", "argh" => "fruit&vegetable"),
+ "content" => "I'm inside the tag"
+ );
print "creating a tag with CData section:<br>\n";
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_CDATA_SECTION));
print "\n<br><br>\n";
/**
- * creating an XML tag with a CData Section
- */
+ * 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 Ä ä Ü ö <>."
- );
+ "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>\n";
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML));
@@ -251,32 +265,35 @@
* 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 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
- */
+ * trying to create an XML tag with an array as content
+ */
$tag = array(
- "qname" => "bar",
- "content" => array( "foo" => "bar" )
- );
+ "qname" => "bar",
+ "content" => array("foo" => "bar")
+ );
print "trying to create an XML tag with an array as content:<br>\n";
print "<pre>";
print_r(XML_Util::createTagFromArray($tag));
print "</pre>";
print "\n<br><br>\n";
/**
- * trying to create an XML tag without a name
- */
+ * trying to create an XML tag without a name
+ */
$tag = array(
- "attributes" => array( "foo" => "bar" ),
- );
+ "attributes" => array("foo" => "bar"),
+ );
print "trying to create an XML tag without a name:<br>\n";
print "<pre>";
print_r(XML_Util::createTagFromArray($tag));
print "</pre>";
print "\n<br><br>\n";
-?>
+?>
View
92 examples/example2.php
@@ -38,94 +38,108 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * $Id$
- *
- * @author Stephan Schmidt
- * @package XML_Util
- * @subpackage examples
- * @category XML
+ * @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
- */
+ * 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 htmlentities(XML_Util::createStartElement("myNs:myTag",
+ array("foo" => "bar"), "http://www.w3c.org/myNs#"));
print "\n<br><br>\n";
/**
- * creating a start element
- */
+ * creating a start element
+ */
print "creating a start element:<br>";
- print htmlentities(XML_Util::createStartElement("myTag", array(), "http://www.w3c.org/myNs#"));
+ print htmlentities(XML_Util::createStartElement("myTag",
+ array(), "http://www.w3c.org/myNs#"));
print "\n<br><br>\n";
/**
- * creating a start element
- */
+ * 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 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
- */
+ * 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
- */
+ * 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
- */
+ * 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
- */
+ * 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"
- );
+ "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>\n";
print "<pre>";
- print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_REPLACE_ENTITIES, true));
+ 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));
+ * 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' );
+ * 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 htmlentities(XML_Util::attributesToString($atts,
+ array('entities' => XML_UTIL_ENTITIES_NONE)));
print "\n<br><br>\n";
-?>
+?>

0 comments on commit 9c55110

Please sign in to comment.