XML_Util2
[ class tree: XML_Util2 ] [ index: XML_Util2 ] [ all elements ]

Class: XML_Util2

Source Location: /XML_Util2-0.2.0/XML/Util2.php

Class Overview


utility class for working with XML documents


Author(s):

Version:

  • Release: 0.2.0

Copyright:

  • 2003-2008 Stephan Schmidt <schst@php.net>

Methods


Inherited Variables

Inherited Methods


Class Details

[line 64]
utility class for working with XML documents


[ Top ]


Method Detail

apiVersion   [line 135]

string apiVersion( )

return API version
  • Return: API version
  • Access: public

[ Top ]

attributesToString   [line 395]

string attributesToString( array $attributes, [bool|array $sort = true], [bool $multiline = false], [string $indent = ' '], [string $linebreak = "\n"], [int $entities = XML_Util2::ENTITIES_XML])

create string representation of an attribute list

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // build an attribute string
  6.  $att = array(
  7.               'foo'   =>  'bar',
  8.               'argh'  =>  'tomato'
  9.             );
  10.  
  11.  $util = new XML_Util2();
  12.  $attList $util->attributesToString($att);


Parameters:

array   $attributes   —  attribute array
bool|array   $sort   —  sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities'
bool   $multiline   —  use linebreaks, if more than one attribute is given
string   $indent   —  string used for indentation of multiline attributes
string   $linebreak   —  string used for linebreaks of multiline attributes
int   $entities   —  setting for entities in attribute values (one of XML_Util2::ENTITIES_NONE, XML_Util2::ENTITIES_XML, XML_Util2::ENTITIES_XML_REQUIRED, XML_Util2::ENTITIES_HTML)

[ Top ]

collapseEmptyTags   [line 465]

string collapseEmptyTags( string $xml, [int $mode = XML_Util2::COLLAPSE_ALL])

Collapses empty tags.
  • Return: XML
  • Todo: PEAR CS - unable to avoid "space after open parens" error in the IF branch
  • Access: public

Parameters:

string   $xml   —  XML
int   $mode   —  Whether to collapse all empty tags (XML_Util2::COLLAPSE_ALL) or only XHTML (XML_Util2::COLLAPSE_XHTML_ONLY) ones.

[ Top ]

createCDataSection   [line 815]

string createCDataSection( string $data)

create a CData section

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // create a CData section
  6.  $tag $util->createCDataSection('I am content.');


Parameters:

string   $data   —  data of the CData section

[ Top ]

createComment   [line 792]

string createComment( string $content)

create an XML comment

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // create an XML start element:
  6.  $tag $util->createComment('I am a comment');

  • Return: XML comment
  • Access: public

Parameters:

string   $content   —  content of the comment

[ Top ]

createEndElement   [line 769]

string createEndElement( string $qname)

create an end element

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // create an XML start element:
  6.  $tag $util->createEndElement('myNs:myTag');


Parameters:

string   $qname   —  qualified tagname (including namespace)

[ Top ]

createStartElement   [line 714]

string createStartElement( string $qname, [array $attributes = array()], [string $namespaceUri = null], [bool $multiline = false], [string $indent = '_auto'], [string $linebreak = "\n"], [bool $sortAttributes = true])

create a start element

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // create an XML start element:
  6.  $tag $util->createStartElement('myNs:myTag',
  7.      array('foo' => 'bar','http://www.w3c.org/myNs#');


Parameters:

string   $qname   —  qualified tagname (including namespace)
array   $attributes   —  array containg attributes
string   $namespaceUri   —  URI of the namespace
bool   $multiline   —  whether to create a multiline tag where each attribute gets written to a single line
string   $indent   —  string used to indent attributes (_auto indents attributes so they start at the same column)
string   $linebreak   —  string used for linebreaks
bool   $sortAttributes   —  Whether to sort the attributes or not

[ Top ]

createTag   [line 516]

string createTag( string $qname, [array $attributes = array()], [mixed $content = null], [string $namespaceUri = null], [int $replaceEntities = XML_Util2::REPLACE_ENTITIES], [bool $multiline = false], [string $indent = '_auto'], [string $linebreak = "\n"], [bool $sortAttributes = true])

create a tag

This method will call XML_Util2::createTagFromArray(), which is more flexible.

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // create an XML tag:
  6.  $tag $util->createTag('myNs:myTag',
  7.      array('foo' => 'bar'),
  8.      'This is inside the tag',
  9.      'http://www.w3c.org/myNs#');


Parameters:

string   $qname   —  qualified tagname (including namespace)
array   $attributes   —  array containg attributes
mixed   $content   —  the content
string   $namespaceUri   —  URI of the namespace
int   $replaceEntities   —  whether to replace XML special chars in content, embedd it in a CData section or none of both
bool   $multiline   —  whether to create a multiline tag where each attribute gets written to a single line
string   $indent   —  string used to indent attributes (_auto indents attributes so they start at the same column)
string   $linebreak   —  string used for linebreaks
bool   $sortAttributes   —  Whether to sort the attributes or not

[ Top ]

createTagFromArray   [line 600]

string createTagFromArray( array $tag, [int $replaceEntities = XML_Util2::REPLACE_ENTITIES], [bool $multiline = false], [string $indent = '_auto'], [string $linebreak = "\n"], [bool $sortAttributes = true])

create a tag from an array

this method awaits an array in the following format

 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
 )

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  $tag = array(
  6.      'qname'        => 'foo:bar',
  7.      'namespaceUri' => 'http://foo.com',
  8.      'attributes'   => array('key' => 'value''argh' => 'fruit&vegetable'),
  9.      'content'      => 'I\'m inside the tag',
  10.  );
  11.  // creating a tag with qualified name and namespaceUri
  12.  $string $util->createTagFromArray($tag);


Parameters:

array   $tag   —  tag definition
int   $replaceEntities   —  whether to replace XML special chars in content, embedd it in a CData section or none of both
bool   $multiline   —  whether to create a multiline tag where each attribute gets written to a single line
string   $indent   —  string used to indent attributes (_auto indents attributes so they start at the same column)
string   $linebreak   —  string used for linebreaks
bool   $sortAttributes   —  Whether to sort the attributes or not

[ Top ]

getDocTypeDeclaration   [line 338]

string getDocTypeDeclaration( string $root, [string $uri = null], [string $internalDtd = null])

build a document type declaration

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // get a doctype declaration:
  6.  $util = new XML_Util2();
  7.  $xmlDecl $util->getDocTypeDeclaration('rootTag','myDocType.dtd');

  • Return: doctype declaration
  • Since: 0.2
  • Access: public

Parameters:

string   $root   —  name of the root tag
string   $uri   —  uri of the doctype definition (or array with uri and public id)
string   $internalDtd   —  internal dtd entries

[ Top ]

getXMLDeclaration   [line 297]

string getXMLDeclaration( [string $version = '1.0'], [string $encoding = null], [bool $standalone = null])

build an xml declaration

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // get an XML declaration:
  6.  $xmlDecl $util->getXMLDeclaration('1.0''UTF-8'true);


Parameters:

string   $version   —  xml version
string   $encoding   —  character encoding
bool   $standalone   —  document is standalone (or not)

[ Top ]

isValidName   [line 890]

mixed isValidName( string $string)

check, whether string is valid XML name

XML names are used for tagname, attribute names and various other, lesser known entities.

An XML name may only consist of alphanumeric characters, dashes, undescores and periods, and has to start with a letter or an underscore.

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // verify tag name
  6.  $result $util->isValidName('invalidTag?');
  7.  if (is_a($result'PEAR_Error')) {
  8.     print 'Invalid XML name: ' $result->getMessage();
  9.  }

  • Return: true, if string is a valid XML name, PEAR error otherwise
  • Todo: support for other charsets
  • Todo: PEAR CS - unable to avoid 85-char limit on second preg_match
  • Access: public

Parameters:

string   $string   —  string that should be checked

[ Top ]

raiseError   [line 920]

PEAR_Error raiseError( string $msg, int $code)


Parameters:

string   $msg   —  error message
int   $code   —  error code

[ Top ]

replaceEntities   [line 183]

string replaceEntities( string $string, [int $replaceEntities = XML_Util2::ENTITIES_XML], [string $encoding = 'ISO-8859-1'])

replace XML entities

With the optional second parameter, you may select, which entities should be replaced.

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // replace XML entites:
  6.  $string $util->replaceEntities('This string contains < & >.');

With the optional third parameter, you may pass the character encoding

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // replace XML entites in UTF-8:
  6.  $string $util->replaceEntities(
  7.      'This string contains < & > as well as ä, ö, ß, à and ê',
  8.      XML_Util2::ENTITIES_HTML,
  9.      'UTF-8'
  10.  );


Parameters:

string   $string   —  string where XML special chars should be replaced
int   $replaceEntities   —  setting for entities in attribute values (one of XML_Util2::ENTITIES_XML, XML_Util2::ENTITIES_XML_REQUIRED, XML_Util2::ENTITIES_HTML)
string   $encoding   —  encoding value (if any)... must be a valid encoding as determined by the htmlentities() function

[ Top ]

reverseEntities   [line 252]

string reverseEntities( string $string, [int $replaceEntities = XML_Util2::ENTITIES_XML], [string $encoding = 'ISO-8859-1'])

reverse XML entities

With the optional second parameter, you may select, which entities should be reversed.

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // reverse XML entites:
  6.  $string $util->reverseEntities('This string contains &lt; &amp; &gt;.');

With the optional third parameter, you may pass the character encoding

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // reverse XML entites in UTF-8:
  6.  $string $util->reverseEntities(
  7.      'This string contains &lt; &amp; &gt; as well as'
  8.      . ' &auml;, &ouml;, &szlig;, &agrave; and &ecirc;',
  9.      XML_Util2::ENTITIES_HTML,
  10.      'UTF-8'
  11.  );


Parameters:

string   $string   —  string where XML special chars should be replaced
int   $replaceEntities   —  setting for entities in attribute values (one of XML_Util2::ENTITIES_XML, XML_Util2::ENTITIES_XML_REQUIRED, XML_Util2::ENTITIES_HTML)
string   $encoding   —  encoding value (if any)... must be a valid encoding as determined by the html_entity_decode() function

[ Top ]

splitQualifiedName   [line 847]

array splitQualifiedName( string $qname, [string $defaultNs = null])

split qualified name and return namespace and local part

  1.  require_once 'XML/Util2.php';
  2.  
  3.  $util = new XML_Util2();
  4.  
  5.  // split qualified tag
  6.  $parts $util->splitQualifiedName('xslt:stylesheet');
the returned array will contain two elements:
 array(
     'namespace' => 'xslt',
     'localPart' => 'stylesheet'
 );

  • Return: array containing namespace and local part
  • Access: public
  • Usedby: XML_Util2::createTagFromArray() - to get local part and namespace of a qualified name

Parameters:

string   $qname   —  qualified tag name
string   $defaultNs   —  default namespace (optional)

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:57:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.