Home » XML » XML_Statistics » Manual
Package to statistically analyze an XML document.
Introduction
Introduction – Introduction to XML_Statistics
Introduction to XML_Statistics
XML_Statistics is a package that allows you to analyze XML documents, which can be files or strings.
XML_Statistics is able to count tags, attributes, processing instructions, external entities and cdata blocks. Furthermore you can apply filters so you are able to count only the tags in a specific depth or only the attributes of a specific tag.
Example
Example – Example for the usage of XML_Statistics
Usage example
The following examples shows how XML_Statistics can be used to analyze a document.
Basic example
<?php
require_once "XML/Statistics.php";
$stat = new XML_Statistics(array("ignoreWhitespace" => true));
$result = $stat->analyzeFile("example.xml");
if ($stat->isError($result)) {
die("Error: " . $result->getMessage());
}
// total amount of tags:
echo "Total tags: " . $stat->countTag() . "<br />";
// count amount of 'title' attribute, in all tags
echo "Occurences of attribute title: " . $stat->countAttribute("title") . "<br />";
// count amount of 'title' attribute, only in <section> tags
echo "Occurences of attribute title in tag section: " . $stat->countAttribute("title", "section") . "<br />";
// count total number of tags in depth 4
echo "Amount of Tags in depth 4: " . $stat->countTagsInDepth(4) . "<br />";
echo "Occurences of PHP Blocks: " . $stat->countPI("PHP") . "<br />";
echo "Occurences of external entity 'bar': " . $stat->countExternalEntity("bar") . "<br />";
echo "Data chunks: " . $stat->countDataChunks() . "<br />";
echo "Length of all data chunks: " . $stat->getCDataLength() . "<br />";
?>
XML_Statistics::apiVersion
XML_Statistics::apiVersion() – return API version
Synopsis
require_once 'XML/Statistics.php';
string XML_Statistics::apiVersion (
)
Description
Returns API version of XML_Statistics.
Return value
string API version
Note
This function can be called statically.
XML_Statistics::analyzeFile
XML_Statistics::analyzeFile() – Analyze an XML file
Synopsis
require_once 'XML/Statistics.php';
string XML_Statistics::analyzeFile (
string $filename
)
Description
Analyzes an XML document by reading from a file. You have to analyze a document before you can extract any statistical information.
Return value
Returns TRUE on success, PEAR_Error on failure.
Note
This function can not be called statically.
See
XML_Statistics::analyzeString
XML_Statistics::analyzeString() – Analyze an XML string
Synopsis
require_once 'XML/Statistics.php';
string XML_Statistics::analyzeString (
string $string
)
Description
Analyzes an XML document directly from a string. This can be useful if your documents are created on the fly by any other application. You have to analyze a document before you can extract any statistical information.
Return value
Returns TRUE on success, PEAR_Error on failure.
Note
This function can not be called statically.
See
XML_Statistics::countTag
XML_Statistics::countTag() – count the occurences of a tag
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::countTag (
string $tagname = null
)
Description
Counts how often a certain tag is used in the document. If no tagname is specified ot will count the total number of tags.
Parameter
-
string $tagname- name of the tag to count
Return value
integer occurences of the tag
Note
This function can not be called statically.
See
XML_Statistics::countTagsInDepth(), XML_Statistics::countAttribute()
XML_Statistics::countPI
XML_Statistics::countPI() – count the occurences of a processing instruction
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::countPI (
string $target = null
)
Description
Counts how often a certain processing instruction (e.g. <?PHP) is used in the document. The target is the 'language' of the processing instruction. You only need to specify the name, without the leading question mark. If no target is specified it will count the total number of processing instructions.
Parameter
-
string $target- target of the processing instruction
Return value
integer occurences of the processing instruction
Note
This function can not be called statically.
XML_Statistics::countExternalEntity
XML_Statistics::countExternalEntity() – count the occurences of an external entitity
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::countExternalEntity (
string $name = null
)
Description
Counts how often a certain external is used in the document. If no name is specified ot will count the total number of external entities.
Parameter
-
string $name- name of the external entity, without '&' and ';'
Return value
integer occurences of the external entity
Note
This function can not be called statically.
XML_Statistics::getMaxDepth
XML_Statistics::getMaxDepth() – get the maximum nesting level
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::getMaxDepth (
)
Description
The nesting level of a document tells you the maximum depth of the tags in the document.
Parameter
This method does not accept any parameters.
Return value
integer maximum nesting level.
Note
This function can not be called statically.
XML_Statistics::countAttribute
XML_Statistics::countAttribute() – count the occurences of an attribute
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::countAttribute (
string $attribute = null
, string $tagname = null
)
Description
Counts how often a certain attribute is used in the document. If no attribute is specified ot will count the total number of tags.
With the second parameter you may limit the search to a special tag.
Parameter
-
string $attribute- if no attribute name is supplied, all attributes are counted -
string $tagname- this allows you to limit your search to a tag
Return value
integer occurences of the attribute or a PEAR_Error if the attribute could not be found.
Note
This function can not be called statically.
See
XML_Statistics::countDataChunks
XML_Statistics::countDataChunks() – count the occurences of a data blocks
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::countDataChunks (
)
Description
This is influenced by the 'ignoreWhitespace' option. Furthermore a new chunk is counted when the parser find a linebreak or internal entity.
Parameter
This method does not accept any parameters.
Return value
integer number of data chunks in the document
Note
This function can not be called statically.
See
XML_Statistics::getCDataLength
XML_Statistics::getCDataLength() – get the combined length of all CData sections
Synopsis
require_once 'XML/Statistics.php';
integer XML_Statistics::getCDataLength (
)
Description
If you need to know how many chars you have between your tags, this is the method that gets it.
Parameter
This method does not accept any parameters.
Return value
integer total length of all CData blocks in the XML document
Note
This function can not be called statically.