PHP XML Classes (downloads)

XqueryLite (class_xquery_lite.php)

Description: This is a PHP implementation of the Xquery Lite language for XML query. This implementation is based on the PHP DOM extension (it uses Xpath). It is 100% compatible with Xquery Lite.

NEWS:
  • First release of this class
This class code as well as documentation are hosted at SourceForge please visit our SourceForge page for releases, documentation, bug-tracking, support forums and mailing lists.

Features To-dos
  • Xquery Lite 1.0 supported.
  • Support the new release of the Xquery Lite language adding if statements, sortby statements and others.
  • Queries may be "unsafe" if the user may enter a query, so a small security check is needed in order to allow user-entered queries without a problem.

Contact: Luis Argerich (lrargerich@yahoo.com)

Detailed description and usage: The usage of this class is easy, basically you just create an object and then use init to initialize the object and the evaluate_xqueryl method to process a qery:
$xq=new XqueryLite();

$xq->init();

$result=$xq->evaluate_xqueryl($query);
If you need to process a second query call the init method to reset object internal state to the initial state. In order to learn more about Xquery Lite and how to write a query please visit the Xquery Lite home page

Documentation

Classes

XqueryLite

Extends: None
Description: This class implements the Xquery Lite 1.0 language, a querying language for XML based on Xquery 1.0 using Xpath and FLWR expressions as the foundations.

Method Summary
 void XqueryLite()
          Constructor
 void init()
          Initializes the object internal state to the initial state
 string evaluate_xqueryl(string $query)
          Evaluates an Xquery Lite 1.0 query
 array _tokenize(string $exp)
          (PRIVATE) Tokenizes a Xquery Lite expression and returns statements in an array
 string _get_root_name(object $node)
          (PRIVATE) Returns the root tagname from a node
 string _parse_for(string $expr)
          (PRIVATE) Parses a for statement
 array _distinct(array $nodeset)
          (PRIVATE) Filters a nodeset returning a nodeset without duplicate nodes
 string _normalize_elements(object $node)
          (PRIVATE) Normalize a dom subtree removing whitespace
 string _parse_var(string $expr, boolean $norm)
          (PRIVATE) Parses an Xquery $a/xpath_expr expression
 int _count_var(string $expr)
          (PRIVATE) counts the number of elements in a $a/xpath_expr expression
 boolean _parse_where(string $expr)
          (PRIVATE) Parses a where statement
 string _parse_return(string $expr)
          (PRIVATE) Parses a return statement
 void _parse_let(string $expr)
          (PRIVATE) Parses a let statement
 array _split_fors(string $exprs)
          (PRIVATE) Split a for statement in several if it has to
 string _parse_query(string $query)
          (PRIVATE) Parses a Xquery Lite expression
 

Method Detail

XqueryLite


void XqueryLite()
This is the constructor receives nothing, does nothing, return nothing.
 
Parameters:
Returns:
Nothing
Throws:
None

init


void init()
This is an important method since it must be called before a query is evaluated (before each query) to initialize the interpreter internal state
 
Parameters:
Returns:
Nothing
Throws:
None

evaluate_xqueryl


string evaluate_xqueryl(string $query)
This method is used to evaluate an Xquery Lite 1.0 query, just pass the query as an argument and receive the result.
 
Parameters:
$query - The Xquery Lite 1.0 query
Returns:
The result of the query whatever it is
Throws:
Several exceptions can be reaised as warnings if an error occurs when parsing the query, tipically the query has a syntax error or the XML source can't be located.

_tokenize


array _tokenize(string $exp)
This private method receives a Xquery Lite expression (whatever is found between curly brackets) and returns an array of statements, for example v[0]=a for statament, v[1]=a let statement, v[2]=a where statement, v[3]=return statement.
 
Parameters:
$exp - An Xquery expression (without starting and ending curly brackets). Sentences must be sepperated by whitespace (blank, newline or tabs)
Returns:
An array where each array position contains an statement
Throws:
None

_get_root_name


string _get_root_name(object $node)
This method returns the name of the root element from an XML fragment passed as a node.
 
Parameters:
$node - A dom node
Returns:
The name of the root element
Throws:
None

_parse_for


string _parse_for(string $expr)
This method parses a for statement and puts the nodeset resulting from the for statement in the object result_sets member.
 
Parameters:
$expr - The for statement it must be in extended notation (can't admit several fors sepparated by commas)
Returns:
The name of the Xquery variable corresponding to the for statement.
Throws:
It can raise an error if the XML source is not found, if it is not well formed or if the Xpath expression in the for statement is wrong

_distinct


array _distinct(array $nodeset)
Filters a nodeset returning a nodeset without duplicate nodes, the normalize_elements function is used in order to compare nodes and find duplicate nodes in the nodeset.
 
Parameters:
$nodeset - Array of dom nodes
Returns:
An array of nodes (another nodeset) without duplicate nodes
Throws:
None

_normalize_elements


string _normalize_elements(object $node)
This function normalizes a dom subtree removing all whitespace elements thus producing a normalized representation of the subtree that can be used in comparisons.
 
Parameters:
$node - A dom node that is the root of the subtree to be normalized
Returns:
An string containing a normalized dump of the subtree
Throws:
None

_parse_var


string _parse_var(string $expr, boolean $norm)
This method parses an Xquery expression in the form $a/xpath_expr where $a is an Xquery Lite variable previously assigned by a for or let statement
 
Parameters:
$expr - The expression to be parsed
$norm - If this parameter is true the method returns a normalized representation of the result.
Returns:
A string representing the result, if the result is a nodeset then all nodes are concatenated one after the other.
Throws:
It can raise an exception if the xpath expression has an error or if the variable is not well formed XML

_count_var


int _count_var(string $expr)
This method parses a $a/xpath_expr expression and returns the number of elements in the nodeset, it is used to process count(expr) constructions in the query.
 
Parameters:
$expr - The expression
Returns:
The number of elements in the nodeset if no xpath expression is passed returns 1 if the variable exists if the nodeset is empty returns 0
Throws:
It can raise an error if the variable is not XML if it doesn't exist or it the Xpath expression is wrong.

_parse_where


boolean _parse_where(string $expr)
This method parses a where statement returning true/false after parsing the expresion
 
Parameters:
$expr - The where statement
Returns:
The result of the where expression
Throws:
If there's an error in the expression it raises an error

_parse_return


string _parse_return(string $expr)
This method parses a return statement returning the result of the return statement as a string if the return statement contains sub-queries then this method calls the evaluate_xqueryl recursively to process them
 
Parameters:
$expr - The return statement, everything found from the return word to the end of the query, including the return word.
Returns:
The result of the return statement
Throws:
Several errors can be raised since this can potentially call evaluate_xqueryl

_parse_let


void _parse_let(string $expr)
This parses a let statement updating the "bindings" object variable with the value of the expression for the designated variable.
 
Parameters:
$expr - The let statement
Returns:
nothing
Throws:
None

_split_fors


array _split_fors(string $exprs)
A for statement may include several for statements sepparated with commas (compact notation) this method splits the compact for notation in an array of extended for statements
 
Parameters:
$exprs - A for statement in compact or extended notation
Returns:
An array where each element is a for statement
Throws:
None

_parse_query


string _parse_query(string $query)
This method parses an Xquery Lite expression and returns the result, the expresion is a FLWR lite expression defined in the Xquery Lite 1.0 language this method identifies FOR, LET, WHERE and RETURN statements, and process them accordingly.
 
Parameters:
$query - The Xquery Lite expression to be parsed
Returns:
The result of the query
Throws:
If there's an error when parsing the query