My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Links

VXP is a PHP 5 extension that parses an XPath 1.0 string and returns the Abstract Syntax Tree as a PHP Array.

By default, the AST contains nodes named after the rules specified by the XPath 1.0 grammar. However, VXP can generate ASTs that conform to the more recent XPath 2.1 grammar. This is useful for either further processing or simply for testing against the reference Java XPath 2.1 parser provided by the W3C (see the W3C Grammar Test Page for XPath 2.1).

VXP passes all 1028 XPath 1.0 tests provided by the W3C. In addition, the XPath 2.1 compatible ASTs generated by VXP are identical to the ones generated by the W3C reference parser (for an XPath 1.0 string).

Please feel free to check out the Live Demo!

Example XPath: /foo[@bar='baz']/text()

VXP Usage:

<?php
$xpath="/foo[@bar='baz']/text()";

try
{
    $result=vxp_parse($xpath);  //parse the XPath
    $AST=vxp_tree($result);     //generate the AST for PHP
    
    printTree($AST);            //PHP function to walk the AST and print the nodes
}
catch (XPathException $e)
{
    print $e->getMessage();
}

Result (XPath 1.0, includes tokens):

START
   XPath
      Expr
         PathExpr
            LocationPath
               AbsoluteLocationPath
                  /
                  RelativeLocationPath
                     Step
                        NodeTest
                           NameTest
                              QName
                                 NCName foo
                        Predicates
                           Predicate
                              [
                              PredicateExpr
                                 Expr
                                    EqualityExpr
                                       PathExpr
                                          LocationPath
                                             RelativeLocationPath
                                                Step
                                                   AxisSpecifier
                                                      @
                                                   NodeTest
                                                      NameTest
                                                         QName
                                                            NCName bar
                                       =
                                       PathExpr
                                          PrimaryExpr
                                             Literal 'baz'
                              ]
                  /
                  RelativeLocationPath
                     Step
                        NodeTest
                           text
                           (
                           )

Result (XPath 2.1 compatible, omits tokens):

START
   XPath
      Expr
         PathExpr
            Slash /
            StepExpr
               AbbrevForwardStep
                  NodeTest
                     NameTest
                        QName foo
               PredicateList
                  Predicate
                     Expr
                        ComparisonExpr =
                           StepExpr
                              AbbrevForwardStep @
                              NodeTest
                                 NameTest
                                    QName bar
                           StringLiteral 'baz'
            StepExpr
               AbbrevForwardStep
                  NodeTest
                     TextTest

Documentation:

Powered by Google Project Hosting