Login   Register  
PHP Classes
elePHPant
Icontem

File: myXML/xpath_test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tereshchenko Andrey  >  myXML  >  myXML/xpath_test.php  >  Download  
File: myXML/xpath_test.php
Role: Example script
Content type: text/plain
Description: XPath examples
Class: myXML
DOM/XPath/XSLT implemented in php.
Author: By
Last change: -
Date: 2004-07-02 00:33
Size: 3,968 bytes
 

Contents

Class file image Download
<?php

function getmicrotime() { 
   list(
$usec$sec) = explode(" "microtime()); 
   return ((float)
$usec + (float)$sec); 
}

$root $_SERVER['DOCUMENT_ROOT'];
$path1 $root.'/myXML;';
$path2 $root.'/PEAR;';
$searchPath $path1.$path2;
// Set the search path.
ini_set('include_path'$searchPath);

require_once(
'XML_Preprocessor.php');
require_once(
'myDOM/myDOM.php');
require_once(
'myXPath/myXPath.php');

PEAR::setErrorHandling(PEAR_ERROR_RETURNE_USER_ERROR);

// Create new DOM document.
$oDocument = new Document();
$oDocument->setOption('method''xml');
$oDocument->setOption('indent'true);

// Initialize global variable for using in attribute value (see sample.xml file)
$id 1;

// Create object of class XML_Preprocessor.
$oXml XML_Preprocessor::create(&$oDocument);

// As in a file sample.xml is used PHP code, for its parsing the object of class
// XML_Preprocessor will be used.
$oXml->parseFile('sample.xml');

// Set the selecting path.
$path '/books/*';
if (
$_GET['xpath']) {
    
$path stripslashes($_GET['xpath']);
}
// Create object myXPath.
$oXPath =& myXPath::create(&$oDocument);
$time_start getmicrotime();
$nodeSet $oXPath->evaluate($path);
$time_end getmicrotime();
$time $time_end $time_start;

?>

<html>
    <head>
        <title>myXPath example</title>
    </head>
    <body style="margin: 10%; font-family: Courier New;">
        <center>
            <h1>myXPath example</h1>
        </center>
        <div>
            <ul>
        <li>Now it is possible to not put white space between operators and operands.</li>
          <ul>
            <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/books/book/*[name()!="price"]
          </ul>
        </li>
        <li>It is possible to use the nested predicates.
          <ul>
            <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title[parent::book[@id>=5]]</li>
          </ul>
        </li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[price > 10]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[(@id=1)or(@id=7)]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[author="Plato" or author="Sophocles"]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[5]/following-sibling::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[position()=5]/preceding-sibling::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//*[self::book or parent::book[@id=4]]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title/../author[.="Sophocles"]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//price/ancestor::book[@id!=5]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/processing-instruction()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/processing-instruction("xml")</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/processing-instruction("xml-stylesheet")</li>
      </ul>
      <script language="php">
        
echo "<br>Time: $time s.";
      
</script>
            <form name="pathForm">
                <input type="text" name="xpath" style="width: 400;">
                <input type="submit" name="submit" value="Submit">
            </form>
        </div>
        <div style="color: blue; background-color: silver; padding: 1%;">
            <script language="php">
                
print('NodeSet:<br>');
        foreach (
$nodeSet as $node) {
                  print(
'<br>'.htmlentities($node->toString($deep false)));
        }
            
</script>
        </div>
    </body>
</html>